Sub topic 6
Slippage: walking the book
What this chapter assumes
You should know how an order book lists bids and asks, what the best bid, best ask and spread are, and how a market order differs from a limit order. You should also be able to read short Python functions. The chapters on the spread and on market orders cover the first two. This chapter covers what happens when an order is bigger than the first price level.
What slippage is
Slippage is the gap between the price you expected and the average price you got. On a centralized exchange it comes from two places:
- Depth. Your order is larger than the quantity available at the best price, so it consumes the next levels, then the ones after that.
- Time. The book changed between the moment you looked and the moment your order reached the matching engine.
Slippage is not a fee. The exchange does not charge it. It is a cost built into the prices of the resting orders you traded against.
Walking the book
A market order has no price limit, so the matching engine fills it against the best opposing price first. When that level is used up, it moves to the next level. This is called walking the book.
Here is the ask side used throughout this chapter:
| Price | Size |
|---|---|
| 100.0 | 2.0 |
| 100.5 | 3.0 |
| 101.0 | 5.0 |
A market buy of 4 units fills like this:
- 2.0 units at 100.0, which empties the first level.
- 2.0 units at 100.5, which leaves 1.0 unit at that level.
The cost is 2 × 100.0 + 2 × 100.5 = 401.0, so the average price is 401.0 / 4 = 100.25. The best ask was 100.0, so you paid 0.25% more than the price you saw at the top of the book.
# 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.25 (percent slippage vs best ask)Choosing a reference price
"Slippage" always needs a reference price, and different tools use different ones. The number you see depends on which one they chose.
- Best ask (for a buy) or best bid (for a sell). This measures only the cost of walking past the top level. It is the usual choice in exchange order previews.
- Mid price. This is the average of best bid and best ask. It also counts half the spread, so it measures the full cost of trading now instead of waiting.
- Last trade price. This is common on retail screens, but it can be stale or sit on either side of the spread.
Traders usually quote slippage in basis points (bps). One basis point is 0.01%, so 0.25% is 25 bps. Basis points avoid confusion when the numbers are small.
The sign matters. For a buy, a worse price means an average above the reference. For a sell, a worse price means an average below it. The function below handles both sides and both reference choices.
bids = [(99.5, 1.5), (99.0, 4.0), (98.0, 10.0)]
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def walk(levels, qty):
"""Return (filled, average_price, unfilled) for a market order on one side."""
remaining = qty
cost = 0.0
for price, size in levels:
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, remaining
def slippage_bps(side, qty, bids, asks, ref="best"):
"""Slippage in basis points; positive means worse than the reference."""
best_bid, best_ask = bids[0][0], asks[0][0]
buying = side == "buy"
filled, avg, _ = walk(asks if buying else bids, qty)
if filled == 0:
return None
if ref == "mid":
reference = (best_bid + best_ask) / 2
else:
reference = best_ask if buying else best_bid
sign = 1 if buying else -1
return sign * (avg - reference) / reference * 1e4
print(round(slippage_bps("buy", 4.0, bids, asks, "best"), 2)) # 25.0
print(round(slippage_bps("buy", 4.0, bids, asks, "mid"), 2)) # 50.13
print(round(slippage_bps("sell", 4.0, bids, asks, "best"), 2)) # 31.41
print(round(slippage_bps("sell", 4.0, bids, asks, "mid"), 2)) # 56.39The mid-price figures are larger because they include half of the 0.5 spread. That half-spread is the cost of immediacy you saw in the spread chapter.
How size changes the cost
Slippage rises with order size, and the increase is not a straight line. Each extra unit has to be bought at a worse price than the one before it. This sweep shows the effect on the ask side above.
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def walk(levels, qty):
remaining = qty
cost = 0.0
for price, size in levels:
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, remaining
best_ask = asks[0][0]
for qty in [1, 2, 4, 8, 10, 12]:
filled, avg, unfilled = walk(asks, float(qty))
bps = (avg - best_ask) / best_ask * 1e4
print(f"qty={qty:>2} filled={filled:>4} avg={avg:.4f} slippage={bps:.2f} bps unfilled={unfilled}")The output is:
qty= 1 filled= 1.0 avg=100.0000 slippage=0.00 bps unfilled=0.0
qty= 2 filled= 2.0 avg=100.0000 slippage=0.00 bps unfilled=0.0
qty= 4 filled= 4.0 avg=100.2500 slippage=25.00 bps unfilled=0.0
qty= 8 filled= 8.0 avg=100.5625 slippage=56.25 bps unfilled=0.0
qty=10 filled=10.0 avg=100.6500 slippage=65.00 bps unfilled=0.0
qty=12 filled=10.0 avg=100.6500 slippage=65.00 bps unfilled=2.0Three points follow from the table:
- Orders up to 2 units cost nothing extra, because the best level holds them.
- Doubling from 4 to 8 units more than doubles the slippage, from 25 to 56.25 bps.
- At 12 units the book runs out. A real exchange fills 10 units and then either rejects the rest, cancels it, or rests it, depending on the order type and the venue's rules. The 65 bps shown is the average of the part that filled, not a price for the whole order.
Market impact
Your own order changes the book. After the 4-unit buy, the 100.0 level is gone and the 100.5 level has 1.0 unit left. The best ask is now 100.5, so the book itself has moved up by 0.5. This effect is called market impact.
Impact has two parts:
- Temporary impact. The levels you consumed are empty, and market makers usually repost orders nearby within seconds or minutes. The book recovers.
- Lasting impact. If your order signals real demand, others may reprice their quotes upward and not return to the old level.
Slippage is what you pay in the moment. Impact is the lasting change that other traders, and you if you keep buying, then face.
Edge cases
- Quote-quantity orders. Many exchanges let you buy with a budget instead of a size, for example "spend 500 USDT". On the example book this takes 2.0 units at 100.0 (200 USDT) and then 300 / 100.5 ≈ 2.985 units at 100.5. You get about 4.985 units at an average near 100.30. The fill is the same walk, but the quantity is the unknown.
- Insufficient depth. If the book cannot absorb your order, the unfilled part follows exchange rules. Some venues reject the order outright. Check the venue's documentation before relying on any one behavior.
- Empty or crossed-looking books. On a thin pair, there may be very few levels, or large gaps between prices. One gap of several percent between the first and second ask level means a small order can produce large slippage.
- Hidden or iceberg size. Some resting orders show only part of their size. You may fill more at a level than the book displayed, so the real slippage can be lower than your estimate.
Controlling slippage
You cannot remove slippage from a market order, but you can bound it.
- Use a limit price as a cap. A buy limit at 100.5 will never pay more than 100.5. In the example, a limit buy of 4 units at 100.5 fills fully, but one at 100.2 fills only 2 units. The price is protected, and the cost is that you may not fill everything.
- Split the order. Smaller slices let the book recover between fills. Splitting adds fees and exposes you to price moves while you work the order, so it is a trade-off, not a free fix.
- Use a slippage tolerance. Some interfaces reject a market order if the estimated average price exceeds a percentage you set. Check whether the tolerance is measured against the best price or the mid price.
- Trade liquid pairs and liquid hours. Depth near the top of the book varies by pair and by time of day.
Exercise
Using the ask side [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)], you send a market buy of 8 units. What is the average fill price, and what is the slippage versus the best ask in basis points?
Show answer
The fills are 2.0 at 100.0 (200), 3.0 at 100.5 (301.5) and 3.0 at 101.0 (303). The total cost is 804.5, so the average price is 804.5 / 8 = 100.5625. Slippage versus the best ask of 100.0 is 0.5625 / 100.0 = 0.5625%, which is 56.25 bps.
Exercise
A buy of 4 units shows 25 bps of slippage versus the best ask but 50.13 bps versus the mid price on the example book (best bid 99.5, best ask 100.0). Why are the two numbers different, and which is the better measure of the full cost of trading immediately?
Show answer
The mid price (99.75) is 0.25 below the best ask, so measuring against the mid also counts half the spread. The best-ask figure counts only the extra cost from walking past the first level. The mid-price figure is the better measure of the full cost of trading immediately, because it includes the spread you pay to cross.