Research collaborate build

Feb 24, 2023

Matching Engine for a Stock Trading Application

Through this article, we explore how and why we made a Matching Engine while developing our Stock Market application.
Rohit Prajapati
Rohit PrajapatiSenior Software Engineer - I
lines

Introduction

Is making a stock market application without a stock exchange possible? This is the question we wanted to test out. After experimenting with various options, Matching Engine proved to be the best solution. Let us dig deeper. 

What is a Matching Engine?

A Matching Engine is an electronic system that matches buy and sell orders for various markets — stock market, commodity market, and financial exchanges. The order-matching system forms the core of all electronic exchanges and executes orders from market users.

Why Matching Engine?

Two common questions may arise: Why is it necessary to make a matching engine in a stock application? Is matching the order something to be handled by the central regulatory bodies i.e. the Stock Exchanges, and not by any other party?

The answer will be all positive.

To let the stock exchange handle your trade orders, you'd need to be a certified broker. And here, we're trying to make a clone application where we don't have access to the stock exchange. So, to let the trade happen on our platform, we need to have our order-matching engine. This engine will handle the process.

Algorithms behind the Matching Engine

Algorithms vary from system to system depending on the requirement. Various algorithms can be used for matching the orders, but there are a few preferred over others —

  • First in, First Out (FIFO)

    FIFO is also known as a price-time algorithm. All orders in the FIFO algorithm are filled according to time priority. The first order at a price level is the first order executed.

    Example: Suppose a buy order for 200 shares of a stock at 400 INR per share is followed by another buy order of 100 shares of the same stock at the same price. The first order of 200 shares is executed first, followed by the order for 100 shares.

    According to the FIFO algorithm, the 200 shares buy order will be matched to one or more sell orders. After the 200 shares buy order is executed and matched, the 100 shares buy order matching will start. The algorithm does not aim to fill maximum orders. It tries to complete a particular order.

  • Pro Rata

    In the Pro Rata algorithm, priority is given to the high-price buy orders followed by the low-price orders. When matching existing orders with an incoming order, the pro rata matching algorithm considers each book order at the inside market price according to its percentage in the overall volume of the incoming buy or sell order at this price, regardless of its timestamp. The pro rata principle thus avoids priority conflicts between large and small orders.

    Example: Suppose buy orders of 300 and 100 shares of the same stock are active in the system. At the same time, a compatible sell order of 300 shares becomes active. The sell order will not be able to fulfill both orders.

    The Pro-Rata algorithm will match 225 shares to the 300-share buy order and 75 shares to the 100-share buy order. Hence, both buy orders are partially filled. The Pro-Rata algorithm fills 75% of both buy orders.

Finding OUR Approach

We had the choice between FIFO or Pro Rata. But in our initial system, the order volume will be less. So implementing FIFO would delay the matching of orders. However, if we wanted to match an order completely, going only with Pro Rata did not suit our requirements.

We could not isolate and decide on one algorithm. The best route was to merge both approaches — FIFO and Pro Rata.

Creating the Engine

As soon as the order enters the engine, the order is placed in an order queue based on the type of order. For example, if the order type is ‘buy’, then the particular order is placed in the order queue in a descending series. If the order type is ‘sell,’ it is placed in an ascending series.

image (4).png

The matching automatically starts when the order queue gets an order.

The next task is to start matching them. Now, when the order matching starts, there can be three cases:

1. Buy order = Sell order

If the buy order and the sell order are equal, the order is matched. In this case, both the orders i.e. the sell and the buy orders get fulfilled, and the engine starts matching the next order.

image (5).png

2. Buy order > Sell order

When the buy order is greater than the sell order, the sell order is completely fulfilled, and the buy order is partially filled. For the leftover buy quantity, an order is made and pushed into the order queue for matching. The engine then starts looking for the next order in the queue.

image (6).png

3. Buy order < Sell order

In case the sell order exceeds the buy order, the buy order is completely fulfilled, and the sell order remains pending. So, with the leftover sell quantity, an order is made and pushed into the order queue for matching. The engine starts looking for the next order in the queue.

image (7).png

 NOTE: Whatever case there is for a particular order, the final trade price of the stock is the price at which an order is executed. The latest price is being updated through a pubsub.

image (8).png

Case of stopping order matching

The matching engine stops matching orders in three cases:

  • If the sell orders in the queue are no longer available or are executed completely.
  • If the buy orders in the queue are no longer available or are executed completely.
  • If the market is closed. (The application follows the schedule of the Indian Stock Market.)

Challenges

The project had its fair share of hiccups. However, there were two that stood out:

  • Lack of legitimate resources to research how the actual matching engine of the Indian Stock Market works.
  • Generating orders similar to real scenarios was a tough challenge due to the volume and volatility of the market.

💡  For queries on how to customize it for your business, schedule a call with our Geeks.

Hire our Development experts.