Matching Engine for a Stock Trading Application
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 —
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.
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.
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.
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.
⭐ 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.
Case of stopping order matching
The matching engine stops matching orders in three cases:
Challenges
The project had its fair share of hiccups. However, there were two that stood out:
💡 For queries on how to customize it for your business, schedule a call with our Geeks.