Sub topic 13
Execution risks and safeguards
This chapter covers what can go wrong between clicking "buy" and getting the fill you expected, and which habits and exchange features reduce that risk. It assumes you know how an order book is structured, how a market order walks the asks, and how limit, stop-market and stop-limit orders behave. It also assumes you can read short Python functions.
Where execution goes wrong
Most bad fills come from three causes. Each one is a different way of the order interacting with the book.
- Thin liquidity. The book has little size near the best price, so a modest order consumes many levels.
- Fat fingers. A human typo, such as an extra zero in the quantity or a misplaced decimal in the price, creates an order far larger or farther from the market than intended.
- Volatility. Prices move quickly, so the book you saw is not the book your order meets. Stops trigger on wicks, and liquidity disappears when you most need it.
These risks compound. A fat-fingered market order on a thin book during a volatile spike is the typical story behind a screenshot of a terrible fill.
Thin liquidity
Depth is the size available at each price. A book can look healthy because the spread is tight, yet hold very little size beyond the first level. The spread measures the cost of a small trade. It says nothing about the cost of a large one.
Three signs of thin liquidity:
- Large gaps between price levels, for example 100.0, then 100.5, then 103.0.
- Small size at the best bid and ask compared with your order.
- Illiquid pairs, or any pair outside its busiest trading hours. Liquidity on crypto pairs is thinner on weekends and late at night in the main trading regions.
Part of the displayed liquidity may also be gone before your order arrives. Another trader can take it, or its owner can cancel it. Treat the visible book as a snapshot, not a promise.
Estimating the cost before you send
Walk the book yourself and compare the result with your tolerance. The function below refuses an order if the book cannot fill it, if its value is too large, or if the estimated slippage is too high.
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def check_buy(asks, qty, max_slippage_pct, max_notional):
"""Pre-trade check for a market buy. Returns (ok, message)."""
remaining = qty
cost = 0.0
for price, size in asks:
take = min(remaining, size)
cost += take * price
remaining -= take
if remaining <= 1e-12:
break
if remaining > 1e-12:
return False, f"insufficient depth: {qty - remaining} of {qty} available"
if cost > max_notional:
return False, f"notional {cost:.2f} exceeds limit {max_notional:.2f}"
avg = cost / qty
slippage = (avg - asks[0][0]) / asks[0][0] * 100
if slippage > max_slippage_pct:
return False, f"estimated slippage {slippage:.3f}% exceeds {max_slippage_pct}%"
return True, f"ok: average {avg:.4f}, slippage {slippage:.3f}%"
print(check_buy(asks, 4.0, 0.5, 1000.0)) # (True, 'ok: average 100.2500, slippage 0.250%')
print(check_buy(asks, 8.0, 0.5, 1000.0)) # (False, 'estimated slippage 0.562% exceeds 0.5%')
print(check_buy(asks, 4.0, 0.5, 300.0)) # (False, 'notional 401.00 exceeds limit 300.00')
print(check_buy(asks, 20.0, 0.5, 5000.0)) # (False, 'insufficient depth: 10.0 of 20.0 available')The check uses a snapshot, so it estimates the cost and does not guarantee it. Between your check and the fill, the book can change. Treat the output as a floor on the risk, not a ceiling.
Many exchanges do their own version of this check. Some market-order implementations cap slippage by rejecting or partially cancelling an order that would trade too far from the last price. Read your exchange's documentation to find out whether it does this and what the thresholds are.
Fat-finger errors
A fat finger is a data-entry mistake. The common versions are:
- An extra zero in the quantity (10 instead of 1).
- A price typed in the wrong units or with a misplaced decimal.
- Buy and sell swapped.
- The wrong trading pair selected.
A limit order does not always protect you from these. A limit buy only guarantees you will not pay more than the limit price. If you type 1000 instead of 100.0 for a buy, the order is marketable at every ask up to 1000. It behaves like a market order and walks the entire ask side. The same happens in reverse with a sell priced far below the market: it sweeps the bids down to that price.
The limit price protects you only when it is near the market. A generous limit is no protection at all.
The limit_buy function from the limit-orders chapter shows this. With asks at 100.0, 100.5 and 101.0, a limit of 1000 fills everything the book has, at the same prices a market order would pay.
Safeguards against typos
- Check the confirmation screen for quantity, price, side and pair. The order value in the quote currency is often the clearest number to check.
- Set a personal maximum order value, as
max_notionaldoes in the code above. - Use exchange-side limits where available, such as price bands and maximum order size. Some exchanges reject orders priced more than a set percentage from the reference price. These are called price collars or price bands. They differ between exchanges and between markets on the same exchange, so check them rather than assuming.
- For API trading, validate every order in code before sending it, and test with small size or on a testnet first.
Volatility
Fast markets hurt execution in three ways.
- Stale quotes. Slippage on a market order is larger because the book moves while your order is in transit. The risk grows with latency and with how volatile the market is.
- Liquidity withdrawal. Market makers widen their spreads or pull their orders when prices move sharply. The book gets thinner exactly when more people want to trade.
- Stop triggers. A sudden wick can trigger a stop and then reverse, leaving you out of a position at a bad price.
How stop orders fail
A stop-market order becomes a market order once the trigger price is reached. It will fill, but the fill price is not controlled. If the price gaps through your stop, you sell at whatever the book offers, which can be well below the stop price.
A stop-limit order becomes a limit order at its trigger. It controls the price, but it may not fill. If the price falls through both the stop price and the limit price, the order sits unfilled while the market keeps moving away. Someone who set a stop-limit to protect a position can end up holding it all the way down.
| Order | What you control | What can go wrong |
|---|---|---|
| Stop-market | Whether it fills | Fill price can be far below the stop in a gap or thin book |
| Stop-limit | The worst price you accept | It may not fill at all if price moves past the limit |
Two further details matter:
- Exchanges differ on which price triggers a stop: the last traded price, the mark price, or the index price. A last-price trigger can be hit by one odd trade on a thin book. Check the trigger source in your exchange's order form.
- Setting a stop-limit's limit price a little beyond the stop price (lower for a sell stop) leaves room for the price to move between trigger and fill. A wider gap raises the chance of a fill and the worst-case price. A narrower gap does the opposite.
Safeguards in order
A pre-trade routine can be as short as five steps. The diagram shows the decision flow for a single order.
Tools that reduce risk
- Limit orders near the market. They cap the price you pay. Pair them with IOC or FOK if you do not want a leftover resting order.
- Splitting. Break a large order into smaller pieces over time so each one takes less depth. This lowers slippage but adds the risk that the price moves between pieces, and each piece may pay a fee.
- Post-only. If the order would cross the spread and take liquidity, the exchange rejects it instead. This protects you from accidentally paying the taker fee and from crossing into a book that changed after you looked.
- Small test orders. Before running a new script or trading a new pair, send a minimal order and check the resulting fill and fee.
- Stops with wider tolerance. Place stops beyond ordinary short-term swings, and choose the order type (stop-market or stop-limit) according to whether a fill or a price matters more.
What safeguards cannot do
Safeguards reduce the chance of a bad fill but do not remove it.
- A limit order can sit unfilled while the price runs away. The safeguard against bad prices has become a missed trade.
- Exchange price bands may be wide, and they can be absent on some markets.
- Order-book snapshots go stale in the time it takes to send an order.
- Exchange outages and delayed order handling during extreme volatility can stop you from cancelling or placing orders at all.
You are always trading one risk for another. Limiting price risk raises the risk of not trading, and the reverse. The next chapter turns this trade-off into a way of choosing an order type.
Exercise
The ask side of a book is [(50.0, 1.0), (50.2, 1.0), (52.0, 10.0)]. You send a market buy for 5 units. (a) What is the average fill price and the slippage against the best ask? (b) Why would a limit buy at 52.0 behave the same way, and what limit price would have protected you better?
Show answer
(a) The order takes 1 at 50.0, 1 at 50.2 and 3 at 52.0. Cost = 50.0 + 50.2 + 156.0 = 256.2, so the average is 256.2 / 5 = 51.24. Slippage is (51.24 - 50.0) / 50.0 = 2.48%. (b) A limit buy at 52.0 is marketable at every ask up to 52.0, so it fills against the same three levels at the same prices. A limit close to the best ask, such as 50.2, would have filled only 2 units at 50.0 and 50.2 and left the remaining 3 units resting on the bid side, capping the price at the cost of an incomplete fill.