Order Matching Engine
Design the core of a stock exchange, a limit order book that matches buy and sell orders by price then arrival time.
Requirements
A limit order is an instruction to buy or sell up to a quantity at a price no worse than a limit: a buy limit of 101 means "pay 101 or less", a sell limit of 101 means "receive 101 or more".
| Question | Decision |
|---|---|
| Which orders? | Limit orders only. Market orders are an extension |
| Which side matches first? | The best price: the highest buy (bid) and the lowest sell (ask) |
| Same price? | The earlier order goes first. This is price-time priority |
| At what price does a trade happen? | The price of the order that was already resting on the book |
| Partial fills? | Yes. An order can trade in several pieces and the unfilled rest stays on the book |
| Cancel? | Yes, by id, while the order is still resting |
Out of scope: market orders, many symbols, persistence, fees, auctions.
1 of 9