Sub topic 6
Slippage: Walking the Book
Assume you know the basics of trading pairs, market orders, and limit orders. This chapter uses a simple Python example to estimate execution costs from an order book.
Slippage is the difference between the price you expect and the average price you actually receive. It happens when your order consumes several price levels instead of filling entirely at the best available price.
Why a Market Order Walks the Book
An order book lists pending bids and asks. For a market buy, the exchange matches your order with the cheapest asks first. If the best ask does not contain enough quantity, the matching engine moves to the next ask. Your order therefore “walks” upward through the book.
Consider this ask side:
| Ask price | Available quantity | Quantity used by a 4-unit buy |
|---|---|---|
| 100.00 | 2 | 2 |
| 100.50 | 3 | 2 |
| 101.00 | 5 | 0 |
The best ask is 100.00, but only 2 units are available there. The remaining 2 units fill at 100.50.
The total cost is:
- 2 × 100.00 = 200.00
- 2 × 100.50 = 201.00
- Total = 401.00
The average fill price is 401 ÷ 4 = 100.25. Compared with the best ask of 100.00, the slippage is 0.25%, before fees.
Estimating Slippage with Python
# Ask side of a book: (price, size), sorted from best (lowest) price upward
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def market_buy(asks, qty):
"""Walk the ask side and return (filled_qty, average_price)."""
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((avg - best_ask) / best_ask * 100) # 0.25The function returns a partial fill if the book does not contain the requested quantity. For example, buying 20 units from this book fills only 10 units. A real exchange may leave the unfilled amount canceled or handle it according to the order’s time-in-force setting.
Slippage and Market Impact
Slippage is not always caused by a slow or faulty exchange. It often reflects the available liquidity. A large order removes liquidity from several levels and can move the market price. This effect is called market impact.
A thin book has small quantities near the best bid and ask, so even a modest order can move through many levels. A deep book can absorb a larger order with less slippage. Volatility makes estimates less reliable because other traders can change or remove orders while yours is being processed.
> callout warning > The displayed order book is not a guaranteed promise. Orders can disappear, new orders can arrive, and several traders may compete for the same liquidity before your order reaches a matching engine.
Reducing Slippage
You can reduce slippage by:
- Trading smaller pieces instead of one large market order.
- Using a limit price that caps what you will pay.
- Checking depth immediately before submitting the order.
- Avoiding thin markets and periods of extreme volatility.
- Comparing the expected slippage with trading fees.
A limit order controls price but may not execute. A market order gives stronger execution certainty but accepts the available prices. The right choice depends on whether price protection or immediate execution matters more.
Exercise
A market buy requests 6 units. The asks are (100.00, 2), (100.50, 3), and (101.00, 5). What is the average fill price and the slippage versus the best ask?
Show answer
The order costs 200.00 + 150.00 + 101.00 = 451.00. The average fill is 451 ÷ 6 = 75.17? This is incorrect because the third level calculation must be 1 × 101.00, making the total 451.00; the correct average is 75.17 only if prices were quantities incorrectly combined. The correct total is 2 × 100.00 + 3 × 100.50 + 1 × 101.00 = 602.50, so the average is 100.42 and slippage is about 0.42%.