Sub topic 2
Reading an Order Book
Assume you know what a cryptocurrency exchange is, have seen a buy or sell screen, and understand percentages and basis points. This chapter uses those ideas to explain how an order book shows available liquidity and expected execution prices.
The Working Model
An order book is a live list of limit orders waiting to trade. A limit order specifies both a quantity and the worst price the trader accepts.
The two sides are:
- Bids: buy orders. The highest bid is the most a buyer currently offers.
- Asks: sell orders. The lowest ask is the least a seller currently accepts.
- Best bid: the highest price in the bid list.
- Best ask: the lowest price in the ask list.
- Depth: the quantity available at each price level.
A trade occurs when a new order matches an existing order. For example, a market buy consumes asks from the lowest price upward. A market sell consumes bids from the highest price downward.
| Side | Price | Available quantity | Meaning |
|---|---|---|---|
| Ask | 100.00 | 2.0 | Sellers offer 2 units at 100.00 |
| Ask | 100.50 | 3.0 | Sellers offer 3 units at 100.50 |
| Bid | 99.80 | 4.0 | Buyers offer 4 units at 99.80 |
| Bid | 99.50 | 5.0 | Buyers offer 5 units at 99.50 |
The difference between the best ask and best bid is the bid-ask spread. In this example, the spread is 100.00 − 99.80 = 0.20.
The mid price is the average of the best bid and best ask:
\[ \text{mid} = \frac{\text{best bid}+\text{best ask}}{2} \]
For this book, the mid price is 99.90. The spread as a percentage of the mid price is about 0.20%, or 20 basis points.
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
def market_buy_avg_price(asks, quantity):
remaining = quantity
total_cost = 0.0
for price, size in asks:
amount = min(remaining, size)
total_cost += amount * price
remaining -= amount
if remaining == 0:
break
if remaining > 0:
raise ValueError("not enough liquidity")
return total_cost / quantity
average = market_buy_avg_price(asks, 4.0)
slippage_bps = (average - asks[0][0]) / asks[0][0] * 10_000
print(average) # 100.25
print(slippage_bps) # 25.0The order buys 2 units at 100.00 and 2 at 100.50. Its average price is 100.25, even though the best ask was 100.00. The extra 25 basis points relative to the best ask is slippage.
Common Errors
- Treating the displayed last-traded price as the price for your next order. The next execution depends on the current best bid and ask.
- Assuming the entire order fills at the top displayed level. A large order may consume several levels.
- Comparing spread values without considering the asset price. Use basis points or percentages for comparison.
- Assuming visible depth is guaranteed. Orders can be canceled before your order reaches them.
- Forgetting that a market order has uncertain execution price. A limit order controls price but may not fill.
Exercise
The best ask is 200.00 for 1 unit, and the next ask is 201.00 for 2 units. What average price will a market buy of 2 units receive, before fees?
Show answer
It will buy 1 unit at 200.00 and 1 unit at 201.00, for an average price of 200.50.