5 Comments
User's avatar
Tarun Telang's avatar

First filter out, invalid requests such as requests placed outside stock exchange working hours, requests from clients without sufficient funds, etc. Next, we can first separate the buy and sell orders into separate heap (or priority queue) data structure, with the highest buying price at the top of the buy queue and the lowest selling price at the top of the sell queue. Then, we can iterate through both queues and match orders based on price until one of the queues is empty.

Expand full comment
Hoa's avatar

That’s very helpful to know. Thanks for giving more insights!

Expand full comment
Keshav Maheshwari's avatar

What happens for pre market orders?

Some average price is decided there

Expand full comment
neatleap's avatar

It uses the last known bid/ask prices but the actual price may turn out differently because of slippage.

Expand full comment
Tarun Telang's avatar

Low level design of a stock exchange very well explained.

Expand full comment