5 Comments

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

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

Expand full comment

What happens for pre market orders?

Some average price is decided there

Expand full comment

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

Expand full comment

Low level design of a stock exchange very well explained.

Expand full comment