Sub topic 6
Slippage and Market Depth
This chapter assumes you know what an order book is, how the bid-ask spread is defined, and how to convert between percentages and basis points (1 bp = 0.01%). The Python snippets are short, and you can follow the chapter without running them.
What slippage is
Slippage is the gap between the price you expected and the average price you actually got. It happens because an order book holds a limited quantity at each price. A market order that is larger than the quantity at the best price takes that level completely and moves on to the next one, which is worse for you.
The spread is a fixed cost for taking the best price. Slippage is the extra cost that grows with order size. A small order may pay the spread and nothing more. A large order pays the spread and also pays for every level it eats through.
The word is used in two ways, so be clear about which one you mean:
- Execution slippage is the difference between the price you expected when you clicked and the average fill price.
- Slippage tolerance is a limit you set. It tells the venue to refuse or cancel the trade if the price would move more than that amount. DEX interfaces use this term heavily.
Walking the book
To estimate slippage for a market buy, start at the best ask and consume levels in order until the order is filled. The code below does this. It also accepts an optional price cap, which becomes useful later.
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)] # (price, size), best first
best_bid, best_ask = 99.8, 100.0
mid = (best_bid + best_ask) / 2
def walk_book(levels, qty, limit_price=None):
"""Fill a buy against ask levels. Return (filled_qty, average_price)."""
remaining, cost = qty, 0.0
for price, size in levels:
if limit_price is not None and price > limit_price:
break
take = min(remaining, size)
cost += take * price
remaining -= take
if remaining == 0:
break
filled = qty - remaining
return filled, (cost / filled if filled else None)
def bps(delta, base):
return delta / base * 10_000
filled, avg = walk_book(asks, 4.0)
print(filled, avg) # 4.0 100.25
print(round(bps(avg - best_ask, best_ask), 1)) # 25.0 vs the best ask
print(round(bps(avg - mid, mid), 1)) # 35.0 vs the mid priceThe same fill gives two slippage figures because the reference price is different. Against the best ask, the 25 bps is pure depth cost. Against the mid price, the 35 bps also includes the half-spread you pay for crossing at all. State which reference you use when you compare numbers. This tutorial measures slippage against the best price on your side of the book, and counts the spread separately.
Market depth
Market depth is how much quantity sits near the best price. A deep book can absorb a large order with little price movement. A thin book cannot. One practical measure is the total size available within some distance of the best price:
def depth_within_bps(levels, ref_price, band_bps):
limit = ref_price * (1 + band_bps / 10_000)
return sum(size for price, size in levels if price <= limit + 1e-9)
print(depth_within_bps(asks, best_ask, 0)) # 2.0
print(depth_within_bps(asks, best_ask, 50)) # 5.0
print(depth_within_bps(asks, best_ask, 100)) # 10.0Here a buyer can take 5 units without paying more than 50 bps above the best ask. Quoting depth this way is more useful than quoting the top-of-book size. The top level can be tiny while the next few levels are large, or the reverse.
Depth varies a lot between markets:
- Major pairs on large venues have deep books. A retail-size order rarely moves the price.
- Small tokens and obscure pairs can have a spread of 1% or more and almost nothing behind the first few levels.
- The same pair is deeper on one venue than another, and the quoted fee schedule does not show this.
Why impact is not linear
Doubling the order size more than doubles the slippage cost. The first units fill at the best price. Each additional unit fills at a progressively worse price, so the average moves further from the top of the book as size grows. The total extra cost grows faster than size, roughly with the square of the size when levels are evenly spaced and equally deep.
Edge cases to watch for:
- Insufficient liquidity. If the whole visible book cannot fill the order, an exchange may fill part of it and cancel the rest, or reject it. The
market_buy_avg_pricestyle of function in the spine raises an error in this case. A real venue behaves according to its own rules. - A stale view. The book on your screen can be a fraction of a second old. In fast markets, other orders can remove the levels you were counting on before your order arrives.
- Hidden liquidity. Some venues allow iceberg or hidden orders. The real depth can be larger than what you see, so a visible-book estimate is a conservative figure.
- Sell side. Selling walks the bid side downward. The calculation is the same with the sign flipped, and bid depth is often different from ask depth.
- Fees on top. Slippage is not part of the trading fee. It is paid in addition, and the fee applies to the average fill value.
Controlling slippage
You cannot remove slippage on a large order, but you can bound it.
| Tool | How it limits slippage | Trade-off |
|---|---|---|
| Limit order at a cap price | Never fills above your price | May fill only partly, or not at all |
| Slicing a large order into smaller ones | Lets the book refill between pieces | Takes time, and the price may drift |
| Resting maker order | Fills at your own price | Not guaranteed to fill, and you carry price risk while waiting |
| Slippage tolerance on a swap or convert screen | Cancels the trade if the price moves too much | A tight value causes failed trades in volatile markets |
| Choosing a deeper venue or pair | Less impact for the same size | May cost more in other ways, such as fees or withdrawal steps |
The price cap in walk_book models a marketable limit order:
filled, avg = walk_book(asks, 8.0, limit_price=100.5)
print(filled, round(avg, 2)) # 5.0 100.3The order asked for 8 units but got 5, because the third level at 101.0 is above the cap. You bought less than you wanted, at an average price that is capped. You must then decide whether to place the remainder elsewhere.
Slippage on DEXes and instant-buy screens
On an automated market maker, there is no order book. The pool's formula sets the price, and the price impact grows with the trade's share of the pool. The idea is the same. Bigger trades relative to liquidity get a worse average price. The tolerance setting is the guard against the price changing between quote and execution.
Instant buy and convert screens usually show one all-in quote, so the slippage is folded into that number instead of being shown. The next chapter covers how to tell a markup from a fee.
Exercise
Using the ask book [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)], you market-buy 7 units. What is the average fill price, and what is the slippage in basis points against the best ask of 100.0?
Show answer
Fill 2 at 100.0 (200.0), 3 at 100.5 (301.5), and 2 at 101.0 (202.0). Total cost is 703.5, so the average is 703.5 / 7 = 100.5. Slippage is (100.5 - 100.0) / 100.0 * 10,000 = 50 bps.
Exercise
With the same book, you place a buy of 8 units as a limit order capped at 100.5. How many units fill, and at what average price? What should you do about the rest?
Show answer
Only the first two levels are at or below the cap, so 5 units fill (2 at 100.0 and 3 at 100.5) for 501.5, an average of 100.3. The remaining 3 units stay unfilled. You can leave them as a resting order, raise the cap, or buy them elsewhere.