Sub topic 2
How the Matching Engine Fills Orders
This chapter assumes you can place buy and sell orders on an exchange, read a trading pair such as BTC/USDT, and read simple Python.
How the matching engine fills orders
A matching engine is the software that pairs buy orders with sell orders. It keeps two sorted queues per market: bids (buy orders, highest price first) and asks (sell orders, lowest price first). Matching follows price-time priority. The best price fills first, and among orders at the same price, the earliest order fills first.
A buy order crosses the spread when its price is at or above the best ask. A market order always crosses, so it keeps taking the best ask until it is filled or the book runs out.
Slippage: walking the book
Slippage is the difference between the best ask and the average price you actually pay. A large market order consumes several price levels, so its average price rises.
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)] # (price, size), best first
def market_buy(asks, qty):
remaining = qty
cost = 0.0
for price, size in asks:
take = min(remaining, size)
cost += take * price
remaining -= take
if remaining == 0:
break
filled = qty - remaining
avg = cost / filled if filled else None
return filled, avg
filled, avg = market_buy(asks, 4.0)
best_ask = asks[0][0]
print(filled, avg) # 4.0 100.25
print(round((avg - best_ask) / best_ask * 100, 2)) # 0.25The order buys 2 units at 100.0 and 2 units at 100.5. The average fill is 100.25, which is 0.25% above the best ask.
Partial fills and average fill price
A partial fill happens when the visible depth is smaller than the order. The unfilled part either rests on the book as a limit order or is cancelled, depending on the order type. The average fill price is the total cost divided by the total filled quantity. Always compare your average fill with the best price, not only with the price you expected when you clicked.
Maker and taker fees
A maker adds liquidity by placing an order that rests on the book. A taker removes liquidity by filling against an order that already rests there. Exchanges usually charge takers a higher rate than makers. A fee is the rate multiplied by the traded value.
MAKER_RATE = 0.0010 # 0.10%, illustrative only
TAKER_RATE = 0.0020 # 0.20%, illustrative only
def fee(price, qty, rate):
return price * qty * rate
print(round(fee(100.0, 2.0, TAKER_RATE), 2)) # 0.4
print(round(fee(100.0, 2.0, MAKER_RATE), 2)) # 0.2A post-only order is rejected if it would cross the spread and fill immediately as a taker. If it is accepted, it always rests and pays the maker rate.
> The rates above are examples. Check your exchange's fee schedule, because rates depend on the exchange and on your volume tier.
Exercise
A market buy of 3 units sees asks of 2 units at 100.0 and 5 units at 101.0. What is the average fill price, and what is the slippage versus the best ask in percent?
Show answer
The order buys 2 units at 100.0 and 1 unit at 101.0. Total cost is 301.0, so the average fill is 100.333... Slippage is (100.333 - 100.0) / 100.0 x 100, which is about 0.33%.