Sub topic 3
Liquidity, depth, and slippage
This chapter assumes you know how an order book stores bids and asks, what the spread is, and the difference between market and limit orders. It also assumes you can read short Python functions. Here you will measure how much a trade of a given size moves the price against you.
What liquidity means
Liquidity is how easily you can trade a size without moving the price much. A liquid market has a tight spread, many resting orders near the best prices, and quick replenishment after trades. An illiquid market has a wide spread, a few small orders, and long gaps between price levels.
Liquidity has three parts:
- Tightness is the spread. It is the cost of a small round trip.
- Depth is the quantity resting at or near the best prices. It is the cost of a larger trade.
- Resilience is how fast the book refills after a large order consumes it.
The spread only describes the first unit you trade. Depth describes the rest.
Depth and the book walk
A market order has no price limit, so it matches the best resting orders first. If the best level is too small, the order continues to the next level. This is called walking the book. Each level fills at its own price, so your average price is a size-weighted average of the levels you consumed. That average is the volume-weighted average price (VWAP).
Take this example book for a BTC contract.
| Side | Price | Size (BTC) |
|---|---|---|
| Ask | 60100 | 5.0 |
| Ask | 60050 | 2.0 |
| Ask | 60020 | 1.0 |
| Ask | 60010 | 0.5 |
| Bid | 59990 | 0.5 |
| Bid | 59980 | 1.5 |
The best bid is 59990 and the best ask is 60010. The spread is 20 and the mid-price is 60000.
Defining slippage
Slippage is the difference between the price you expected and the price you got. Traders use the term for two different effects, so keep them apart.
- Impact slippage comes from your own order consuming depth. The code below measures it. It is deterministic if the book does not change.
- Latency slippage comes from the book changing between your decision and your fill. Another trader may take the level you were aiming at. This is random and affects you more in fast markets.
A common benchmark is the mid-price at decision time. Measured against it, slippage includes half the spread, which every market order pays even at size zero. In basis points (bps, where 1 bps is 0.01%):
slippage_bps = (vwap - mid) / mid * 10,000 (for a buy)
slippage_bps = (mid - vwap) / mid * 10,000 (for a sell)Measuring slippage in code
The function below walks one side of a book and reports how much filled and at what average price. It handles a book too thin to fill the whole order, which happens in real markets.
ASKS = [(60010.0, 0.5), (60020.0, 1.0), (60050.0, 2.0), (60100.0, 5.0)]
BIDS = [(59990.0, 0.5), (59980.0, 1.5)]
def walk_book(levels, qty):
"""Return (filled_qty, vwap) for a market order against sorted levels."""
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
return filled, (cost / filled if filled else None)
mid = (ASKS[0][0] + BIDS[0][0]) / 2
for size in (0.5, 2.0, 5.0, 10.0):
filled, vwap = walk_book(ASKS, size)
bps = (vwap - mid) / mid * 10_000
print(f"buy {size:>4}: filled={filled:.1f} vwap={vwap:,.2f} slippage={bps:.2f} bps")Output:
buy 0.5: filled=0.5 vwap=60,010.00 slippage=1.67 bps
buy 2.0: filled=2.0 vwap=60,025.00 slippage=4.17 bps
buy 5.0: filled=5.0 vwap=60,055.00 slippage=9.17 bps
buy 10.0: filled=8.5 vwap=60,073.53 slippage=12.25 bpsThree things follow from the output:
- The 0.5 BTC order pays only the half-spread, 1.67 bps. That cost is a floor.
- Cost grows with size. Slippage rises from 4.17 to 9.17 bps as size goes from 2 to 5 BTC.
- The 10 BTC order did not fill. The book held only 8.5 BTC, so 1.5 BTC went unfilled. Most venues cancel the remainder of a market order, but some leave it resting or reject the order.
Bigger orders pay more per unit, so total cost grows faster than size. Depth is also uneven, and gaps between levels (60050 to 60100 above) make the cost jump.
Measuring depth
Traders summarize depth in a few common ways:
- Depth within a band. The total size within a percentage of mid, such as 0.1% or 1%. In the example, asks up to 60060 (0.1% above mid) hold 3.5 BTC.
- Size for a target slippage. The largest order that stays under, say, 5 bps. In the example it is a bit over 2 BTC.
- Cumulative depth curve. Plot cumulative size against price. A steep curve is thin and a flat curve is deep.
Compare depth in notional value (price times size), not only in coins, when you compare markets with different prices or contract sizes.
What affects liquidity
Liquidity is not constant. It varies with:
- Instrument. Major perpetual swaps on large venues are usually deeper than dated futures far from expiry, and far deeper than small-cap altcoin markets.
- Time. Depth often thins in quiet hours and on weekends. It also thins just before major news, when market makers pull quotes.
- Volatility. Market makers widen spreads and cut size when prices move fast. Depth disappears when you need it most.
- Venue. The same asset can have very different books on different exchanges.
Common errors
- Trusting the top of book. The best ask may show 0.5 BTC. That says nothing about your 5 BTC order.
- Treating displayed depth as guaranteed. Resting orders can be cancelled before your order arrives. Some are placed to look large and are cancelled when price approaches (spoofing). Depth within a tight band is more reliable than depth far from the price.
- Ignoring fees. A taker fee comes on top of slippage. Total cost is slippage plus fee, so use both when you compare a market order with a limit order.
- Using last-traded price as a benchmark. Last price may be a bid fill or an ask fill, so it hides the spread. Use mid.
- Forgetting that exits cost too. You pay slippage on entry and exit. A position that is easy to enter in calm conditions may be costly to close in a sell-off.
- Testing only the buy side. Bid and ask depth are not symmetric, and the side you need to trade is the one that matters.
Reducing slippage
- Use limit orders to set a worst acceptable price. A limit order near the touch can also earn the maker role and its lower fee.
- Split a large order into slices over time (a TWAP, or time-weighted average price, approach). This lets the book refill, though it exposes you to price moves between slices.
- Trade when depth is higher and avoid thin hours.
- Use an exchange's slippage or price-protection setting where one exists.
- Choose a more liquid instrument for the same exposure, such as a major perpetual instead of a far-dated future.
Why this matters for derivatives
Leverage makes slippage matter more. A 5 bps slip on notional is 50 bps of margin at 10x leverage. Thin books also matter for liquidations. A liquidation is a forced market order, so it walks the book. If depth is poor, the forced order fills far from the bankruptcy price, which creates losses that later chapters on insurance funds and auto-deleveraging address.
Exercise
Using the ASKS list in this chapter, a trader buys 3.5 BTC at market. Compute the VWAP and the slippage in bps against a mid of 60000.
Show answer
The order takes 0.5 at 60010 (30,005), 1.0 at 60020 (60,020), and 2.0 at 60050 (120,100). Total cost is 210,125 for 3.5 BTC, so the VWAP is 60,035. Slippage is (60035 - 60000) / 60000 * 10,000, which is about 5.83 bps.
Exercise
A trader says, "The spread is only 20 dollars, so my 10 BTC market buy will cost about 10 dollars per BTC over mid." Explain why this is wrong using the example book.
Show answer
The spread only prices the first unit. A 10 BTC buy walks the book, so the average price depends on depth at every level it takes. In the example the book holds only 8.5 BTC, so 1.5 BTC would not fill. The 8.5 BTC that does fill has a VWAP of about 60,073.53, which is about 73.53 above mid, not 10.