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.
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.
That’s very helpful to know. Thanks for giving more insights!
What happens for pre market orders?
Some average price is decided there
It uses the last known bid/ask prices but the actual price may turn out differently because of slippage.
Low level design of a stock exchange very well explained.