Match Orders from a Market Data Feed
You receive a market data feed showing the current order book state. You must determine which price levels a market order will hit and calculate the resulting fills.
Order Book Basics
The ask side (sell orders) is sorted from lowest to highest price. The bid side (buy orders) is sorted from highest to lowest price.
A market buy order sweeps up through asks (lowest ask first). A market sell order sweeps down through bids (highest bid first).
Example
Asks: Bids:
100.10 × 200 100.03 × 100
100.06 × 200 100.02 × 50
100.05 × 30 100.01 × 75A market buy of 50 shares:
- Fills 30 shares at 100.05 (all of level 1)
- Fills 20 shares at 100.06 (partial fill of level 2)
- Remaining: 180 shares at 100.06
Key Concepts
- FIFO matching: orders at the same price level are filled in time order.
- Partial fills: large orders may consume multiple price levels.
- Slippage: the difference between expected and actual fill price.