Sub topic 4
Order book, depth and liquidity
This chapter assumes you can read a quote (bid, ask, spread) and know what a share is. You do not need to have placed a trade. It explains the structure behind the quote: the order book. It also shows why the size of your order matters as much as its type.
What the order book is
The quote you see on a brokerage screen is the top of a longer list. An order book is the exchange's list of all resting orders for one stock. A resting order is a limit order that has not filled yet and is waiting for a counterparty.
The book has two sides:
- Bids are buy orders, sorted from the highest price down.
- Asks (also called offers) are sell orders, sorted from the lowest price up.
The best bid and best ask are the numbers in the quote. The gap between them is the spread. Everything behind them is depth: the number of shares waiting at each price further from the market.
A small example for a stock called XYZ:
| Side | Price | Size (shares) |
|---|---|---|
| Ask | 50.10 | 500 |
| Ask | 50.05 | 200 |
| Ask | 50.02 | 100 |
| Bid | 49.98 | 300 |
| Bid | 49.95 | 400 |
| Bid | 49.90 | 800 |
The quote for XYZ is bid 49.98, ask 50.02, and the spread is 0.04. The quote also reports a size for each side: 300 shares bid and 100 shares offered. Those sizes cover only the best price level.
Why the book matters for your order
Each price level holds a limited number of shares. If you want more shares than the best level holds, the rest of your order must reach the next level, and the next, at worse prices. That is the mechanism behind slippage, covered in a later chapter.
Orders match by price-time priority. A better price goes first. At the same price, the order that arrived earlier goes first. This has two consequences:
- A limit buy priced higher than the current best bid moves to the front of the bid line.
- A limit buy at the same price as other bids joins the back of the queue at that level.
Walking the book
Take a market buy for 400 shares against the asks above. The order fills from the lowest price upward:
- 100 shares at 50.02 uses up the first level.
- 200 shares at 50.05 uses up the second level.
- The remaining 100 shares fill at 50.10, taking part of the third level.
The total cost is 100 × 50.02 + 200 × 50.05 + 100 × 50.10 = 5002 + 10010 + 5010 = 20,022, so the average price is 50.055. The quote said 50.02, but you paid about 0.035 more per share on average.
The code below simulates this. It also shows how a limit order behaves on the same book.
def fill_buy(asks, qty, limit=None):
"""Simulate a buy against a list of (price, size) asks.
limit=None behaves like a market order; otherwise like a limit order."""
filled, cost = 0, 0.0
for price, size in sorted(asks):
if limit is not None and price > limit:
break
take = min(size, qty - filled)
filled += take
cost += take * price
if filled == qty:
break
avg_price = cost / filled if filled else None
return filled, avg_price
asks = [(50.02, 100), (50.05, 200), (50.10, 500)]
print(fill_buy(asks, 400)) # market order
print(fill_buy(asks, 400, limit=50.05)) # limit orderThe market order fills all 400 shares at an average of about 50.055. The limit order at 50.05 refuses the 50.10 level. It fills only 300 shares at an average of 50.04 and leaves 100 shares unfilled. The unfilled 100 shares then rest on the book as a bid at 50.05, unless the order's time in force or your broker's rules say otherwise.
Liquidity
Liquidity is how easily you can buy or sell without moving the price. You can judge it from three features of the book:
- Spread. A tight spread, such as one cent, usually signals a liquid stock. A wide spread signals a thin one.
- Depth. Large sizes at many price levels near the best quote mean a large order can fill with little price change.
- Volume. The number of shares that trade per day. High volume usually goes with a deep book, but it does not guarantee depth at the moment you trade.
Compare two stocks. A large, widely traded company might show a bid of 100.00 and an ask of 100.01, with tens of thousands of shares at each level. A small, rarely traded company might show a bid of 4.80 and an ask of 5.20, with 100 shares at each. A market buy of 500 shares in the second stock would push through several levels, and the average price could be well above 5.20.
What the quote does not show
The book you see through a retail broker is incomplete in several ways. Know these before you trust a number on the screen:
- The book changes constantly. Orders arrive, cancel, and fill many times per second. By the time you click submit, the displayed depth may be gone.
- Your broker may show only the top level. Many retail platforms display only the best bid and ask, with their sizes. Deeper levels may need a paid or separate data feed.
- Quotes may be delayed. Some free feeds lag by 15 minutes or more. Check whether your quote is real-time before you rely on it.
- Not all liquidity is displayed. Some orders are hidden or only partly shown, so real liquidity can be larger than the visible book.
- The last price is not a promise. The last traded price is where the most recent trade happened. It may be on either side of the current spread, so it tells you less than the bid and ask.
- Depth thins at the open and close. Spreads are often wider in the first minutes after the market opens and in extended hours, when fewer participants are present.
Using the book to choose an order
Use the book to decide how to submit an order:
- Compare your quantity to the size at the best price. If your quantity is smaller, a market order will likely fill at the quote.
- If your quantity is larger, estimate the average price by walking the book, as in the code above.
- If the spread is wide, a limit order protects you from paying the far side of it. You can place the limit between the bid and the ask and see whether someone meets it.
- If the stock is thin, consider splitting a large order into smaller ones over time. This is a common way to reduce the effect on price.
Common errors
- Treating the quote as the price of your whole order. It is the price of the first shares only.
- Judging liquidity from the share price. A low-priced stock is not necessarily liquid, and a high-priced stock is not necessarily illiquid.
- Assuming a limit order that "fills partly" is a mistake. Partial fills are normal when depth runs out at your price.
- Joining the back of a long queue and expecting a quick fill. At the same price, earlier orders fill first.
Exercise
The asks for a stock are 20.00 for 150 shares, 20.03 for 100 shares, and 20.08 for 400 shares. You send a market buy for 300 shares. What is the average fill price, rounded to four decimals? Then say how many shares a limit buy for 300 shares at 20.03 would fill.
Show answer
The market order takes 150 at 20.00 (3000), 100 at 20.03 (2003), and 50 at 20.08 (1004). The total is 6007, so the average is 6007 / 300 = 20.0233. The limit buy at 20.03 refuses the 20.08 level, so it fills 250 shares (150 at 20.00 and 100 at 20.03) and leaves 50 unfilled.
Exercise
A stock shows bid 12.40 and ask 12.60, with 100 shares at each. Another shows bid 80.00 and ask 80.01, with 5,000 shares at each. Which is more liquid, and which features of the book support your answer?
Show answer
The second stock is more liquid. Its spread is 0.01 against 0.20 for the first, and its displayed size at the best price is 5,000 shares against 100. A large order would move its price far less.