Sub topic 2
Reading an Order Book
This chapter assumes you know what a limit order and a market order are, and that you have seen a buy and sell screen on an exchange. Basis points (1 bp = 0.01%) matter in later chapters. The Python snippets are short and optional.
What an order book is
An order book is the exchange's list of all resting orders for one trading pair, such as BTC/USDT. A resting order is a limit order that has not been filled yet. It waits in the book until someone trades against it or the owner cancels it.
The book has two sides:
- Bids are buy orders. Each one names the highest price its owner will pay.
- Asks (also called offers) are sell orders. Each one names the lowest price its owner will accept.
Bids are sorted from highest to lowest price. Asks are sorted from lowest to highest. The two best prices sit in the middle, facing each other:
- The best bid is the highest bid.
- The best ask is the lowest ask.
The difference between them is the spread, which a later chapter covers. For now, notice that a healthy book always has the best bid below the best ask. If they met, the exchange would match the two orders at once.
Price levels and sizes
Exchange screens do not show every individual order. They group orders at the same price into one price level. Each row shows a price and the total size at that price. Size is usually in the base currency (BTC in BTC/USDT). Some venues show cumulative size or quote-currency value instead, so check the column header.
from collections import defaultdict
# (side, price, size, arrival_time)
orders = [
("buy", 99.8, 1.0, 1),
("buy", 99.8, 0.5, 3),
("buy", 99.7, 4.0, 2),
("sell", 100.0, 2.0, 4),
("sell", 100.5, 3.0, 5),
]
def levels(orders, side):
agg = defaultdict(float)
for s, price, size, _ in orders:
if s == side:
agg[price] += size
return sorted(agg.items(), reverse=(side == "buy"))
print(levels(orders, "buy")) # [(99.8, 1.5), (99.7, 4.0)]
print(levels(orders, "sell")) # [(100.0, 2.0), (100.5, 3.0)]Two separate orders at 99.8 (1.0 and 0.5) appear as one level of 1.5. The screen hides how many people are behind that number.
Prices move in steps. The tick size is the smallest allowed price step, and the lot size is the smallest allowed size step. A tick of 0.1 means you can bid 99.8 or 99.9 but not 99.85. Venues can also offer a "grouping" setting that merges several ticks into one displayed row. Grouping makes the screen easier to read, but it hides the true best bid and ask.
A sample book
Here is a small book. Size is in base units.
| Side | Price | Size | Cumulative size |
|---|---|---|---|
| Ask | 101.0 | 5.0 | 10.0 |
| Ask | 100.5 | 3.0 | 5.0 |
| Ask | 100.0 | 2.0 | 2.0 |
| Bid | 99.8 | 1.5 | 1.5 |
| Bid | 99.7 | 4.0 | 5.5 |
| Bid | 99.5 | 6.0 | 11.5 |
The best ask is 100.0 and the best bid is 99.8. The mid price is the average of the two, 99.9. Cumulative size is the running total as you move away from the middle. It tells you how much you could trade before reaching a given price. This quantity is called depth.
bids = [(99.8, 1.5), (99.7, 4.0), (99.5, 6.0)] # best first
asks = [(100.0, 2.0), (100.5, 3.0), (101.0, 5.0)]
best_bid, best_ask = bids[0][0], asks[0][0]
mid = (best_bid + best_ask) / 2
bid_depth = sum(size for _, size in bids)
ask_depth = sum(size for _, size in asks)
imbalance = (bid_depth - ask_depth) / (bid_depth + ask_depth)
print(best_bid, best_ask, mid) # 99.8 100.0 99.9
print(bid_depth, ask_depth) # 11.5 10.0
print(round(imbalance, 3)) # 0.07The imbalance figure runs from -1 (all asks) to +1 (all bids). Here it is slightly positive, which means more size is waiting on the buy side across these levels.
How orders interact with the book
Every new order is checked against the opposite side of the book. The diagram shows the decision.
A buy order crosses when its price is at or above the best ask. A sell order crosses when its price is at or below the best bid. A market order has no price limit, so it always crosses and keeps consuming levels until it is filled.
An order that crosses takes liquidity. An order that rests makes it. The next chapter turns this distinction into fees.
Price-time priority
Most spot exchanges use price-time priority. Better prices fill first. At the same price, the order that arrived earlier fills first. Your place in line at a level is your queue position.
from collections import deque
ask_queue = deque([("A", 1.0), ("B", 0.5), ("C", 2.0)]) # at 100.0, oldest first
def fill_at_level(queue, qty):
fills = []
while qty > 0 and queue:
owner, size = queue[0]
take = min(qty, size)
fills.append((owner, take))
qty -= take
if take == size:
queue.popleft()
else:
queue[0] = (owner, size - take)
return fills
print(fill_at_level(ask_queue, 2.0)) # [('A', 1.0), ('B', 0.5), ('C', 0.5)]
print(list(ask_queue)) # [('C', 1.5)]A buyer of 2.0 fills against A first, then B, then part of C. Owner C still has 1.5 resting, but only after the two earlier orders are cleared.
A few venues use pro-rata matching, which splits fills in proportion to size. This is rare in spot crypto. Check the venue's documentation if you trade on an unusual market.
Common errors when reading a book
- Treating displayed size as guaranteed. Orders can be cancelled at any moment. A large order near the top may vanish when the price approaches. Some large orders are placed to look like support or resistance and then pulled. This practice is called spoofing.
- Missing hidden size. Some venues allow iceberg orders, which show only a small part of their true size. After the visible part fills, more appears. The book can understate real liquidity at a level.
- Reading a snapshot as the present. On an active pair the book changes many times per second. A screenshot, or a screen that updates slowly, can show prices that are already gone.
- Mixing units. Check whether size is in base currency, in quote currency, or in contracts. For derivatives, a contract can represent a fixed dollar value rather than one coin.
- Assuming one global book. Every exchange keeps its own book. The same pair has different best prices and different depth on different venues. Aggregator sites that merge books can show a combined view you cannot trade as one order.
- Trusting grouped views. A screen grouped to a step of 10 may show a "best ask" that is rounded away from the true one.
- Ignoring thin books. An exotic pair with a few hundred dollars at each level can move several percent on a modest market order. The top of the book alone does not tell you this. Depth does.
Where this leads
The order book is the source of three later costs. Who takes and who makes decides the trading fee. The gap between the best bid and ask is the spread. The way a large market order walks through several levels produces slippage. Reading the book carefully lets you predict each of these before you click.
Exercise
A book has asks of 2.0 at 50.0, 3.0 at 50.2, and 4.0 at 50.5. The best bid is 49.8. (a) What is the mid price? (b) You send a market buy for 4.0. Which levels does it consume, and what is the average fill price?
Show answer
(a) Mid = (49.8 + 50.0) / 2 = 49.9. (b) It takes all 2.0 at 50.0, then 2.0 of the 3.0 at 50.2, leaving 1.0 resting at 50.2. Cost = 2.0 x 50.0 + 2.0 x 50.2 = 100.0 + 100.4 = 200.4, so the average price is 200.4 / 4.0 = 50.1.
Exercise
Two limit orders to sell at 100.0 sit in the book: order X of 1.0 placed first, and order Y of 1.0 placed second. A market buy for 1.0 arrives. Which order fills under price-time priority, and what changes if Y was placed at 99.9 instead?
Show answer
X fills, because both orders have the same price and X arrived earlier. If Y was at 99.9, Y fills first, because a lower ask is a better price for the buyer and price takes priority over arrival time.