Sub topic 5
How the Matching Engine Fills Orders
Assume you already understand trading pairs, bids, asks, and the difference between market and limit orders. This chapter focuses on what happens after an exchange receives an order.
The matching engine's job
A matching engine is the exchange software that pairs buy orders with sell orders. It continually compares the highest bid with the lowest ask.
A trade can occur when:
- A buy order offers a price at least as high as a sell order's price.
- A sell order accepts a price at most as low as a buy order's price.
The engine follows price-time priority:
- The best price gets priority.
- Among orders at the same price, the order submitted first gets priority.
For buy orders, a higher price is better because the buyer is willing to pay more. For sell orders, a lower price is better because the seller is willing to accept less.
| Order side | Best price | Why |
|---|---|---|
| Buy bid | Highest bid | It offers sellers the most money |
| Sell ask | Lowest ask | It offers buyers the lowest cost |
How a limit order crosses the book
Consider this order book:
- Bids: 99.5 for 4 BTC, then 99.0 for 3 BTC
- Asks: 100.0 for 2 BTC, then 100.5 for 5 BTC
A limit buy at 100.5 crosses the spread. It can match with the 100.0 ask first because that is the best available sell price. If the buyer requests 4 BTC, the engine fills:
- 2 BTC at 100.0
- 2 BTC at 100.5
The remaining ask quantity at 100.5 stays available. The buyer's limit price is a maximum, not necessarily the price paid for every unit.
A limit buy at 99.8 does not cross the spread. Its price is below the best ask, so it joins the bid side and waits for a seller willing to accept 99.8 or less.
Price-time priority in practice
Suppose three buy orders wait at 100.0:
| Order | Quantity | Submission time |
|---|---|---|
| A | 1.0 BTC | 10:00:01 |
| B | 2.0 BTC | 10:00:02 |
| C | 0.5 BTC | 10:00:03 |
If a seller offers 2.5 BTC at 100.0, the engine fills A completely, then B completely, then C partially. C has 0.5 BTC remaining. The engine does not normally choose C simply because it is smaller.
# A small price-time priority example.
from collections import deque
orders = deque([
{"id": "A", "qty": 1.0, "time": 1},
{"id": "B", "qty": 2.0, "time": 2},
{"id": "C", "qty": 0.5, "time": 3},
])
incoming_sell = 2.5
fills = []
while incoming_sell > 0 and orders:
resting = orders[0]
amount = min(incoming_sell, resting["qty"])
fills.append((resting["id"], amount, 100.0))
resting["qty"] -= amount
incoming_sell -= amount
if resting["qty"] == 0:
orders.popleft()
print(fills)
print(list(orders))
# [('A', 1.0, 100.0), ('B', 1.5, 100.0)]
# [{'id': 'B', 'qty': 0.5, 'time': 2}, {'id': 'C', 'qty': 0.5, 'time': 3}]Matching sequence
The engine may produce several fills for one submitted order. Your exchange interface might display them as one completed trade, but the execution report can contain each individual fill and price.
Exercise
A book has asks of 1 BTC at 50,000 and 3 BTC at 50,100. A market buy requests 2 BTC. Which asks does the engine use?
Show answer
It fills 1 BTC at 50,000 and 1 BTC at 50,100. The average fill price is 50,050.