Skip to main content
QuantDXB

Markets and trading · 11 min read

The limit order book

Where trades actually happen: bids, asks and the spread, how market orders walk the book, and why large orders cost more per share.

Before you start

  • No finance background needed
  • Basic Python (lists, loops)

By the end you'll be able to

  • Read an order book: bids, asks, spread, mid and depth
  • Explain price-time priority and the difference between limit and market orders
  • Compute the average price and cost of a market order that walks the book
  • Explain why trading costs grow with order size

Every trade on an exchange happens because someone was willing to wait and someone else wasn't. The limit order book is where the waiting happens: a live list of every price at which someone has offered to buy or sell, and how much. Understanding it explains what a "price" really is, why trading costs money even with zero fees, and why large orders cost more per share than small ones.

TermMeaning
limit order"Buy (or sell) up to this quantity at this price or better", left waiting
market order"Buy (or sell) this quantity now, at whatever prices are available"
bid, askThe highest price someone will pay; the lowest price someone will accept
spreadAsk minus bid
mid priceThe average of the best bid and the best ask
depthHow much quantity is available near the best prices
bpsBasis points: hundredths of a percent (1 bp=0.01%1\,\text{bp} = 0.01\%)

What's in the book

A limit order says: I will buy 500 shares at 99.98 or less. If no seller is willing to trade at that price right now, the order waits in the book. Buy orders waiting are bids; sell orders waiting are asks (or offers). The highest bid and the lowest ask are the best bid and best ask, together called the top of the book or the quote.

The best ask is always above the best bid. If a new buy order came in at or above the best ask, it would trade immediately instead of waiting. The gap between them is the spread, and the price everyone quotes when they say a stock "is at 100" is usually the mid, halfway between.

Within one price level, orders queue in time order: whoever arrived first trades first. This rule is called price-time priority, and it is why trading firms care about being early in the queue at a price, not just about the price.

Market orders walk the book

A market order doesn't wait. It trades against the best available orders on the other side until it is filled. A small market buy fills entirely at the best ask. A large one empties the best ask, then the next price up, then the next, paying more for each slice.

Drag the order size below and watch the order eat through the asks:

2,000 shares
Shares filled
0
Price levels used
0
Average price
–
Cost vs mid (bps)
–
spread is 2.0 bps
Each bar is the quantity waiting at one price. A market buy takes the cheapest offers first (white), then has to pay more at each level it empties. Small orders pay about half the spread; large ones pay much more.

Three things to notice:

  • A small order pays half the spread. Buying at the best ask instead of the mid costs 12(ask−bid)\tfrac{1}{2}(\text{ask} - \text{bid}) per share. Here the spread is 2 bps, so a small buy costs 1 bp against the mid. Selling at the bid costs the other half.
  • A large order pays more per share. Each level it empties forces the rest of the order to the next, worse price. The average price rises with size.
  • The book changes after the trade. The levels the order consumed are gone. Until new sellers arrive, the best ask is now higher, so the mid price has moved up. Large trades move prices simply by using up liquidity.

Key idea. The price you see is the price for a small quantity. The cost of trading grows with size because a large order has to reach deeper into the book.

Who provides the orders

Limit orders that wait in the book provide liquidity: they let other traders trade immediately. Market orders take liquidity. Exchanges often charge takers a fee and pay makers a small rebate, to encourage people to post limit orders.

Most of the orders near the top of the book in liquid markets come from market makers, firms that continuously post both bids and asks and earn the spread, which the market making lesson covers. They are paid for taking a risk: the traders who hit their quotes sometimes know more than they do.

Depth and liquidity

How much a market order costs depends on depth: the quantity available close to the mid. A deep book (thousands of shares within a few ticks) lets large orders trade cheaply. A thin book, like a small stock or any market during a crash, can make even moderate orders expensive.

Depth changes all the time. It is lower around news releases, at the open and the close of some markets, and in stressed markets, which is exactly when many traders most want to trade.

In code

A market order walking the book takes a few lines of plain Python:

python
# A market order walking an order book, in plain Python.
asks = [(100.01, 300), (100.02, 500), (100.03, 800), (100.04, 1000), (100.05, 1200)]
bids = [(99.99, 400), (99.98, 600), (99.97, 900)]
mid = (asks[0][0] + bids[0][0]) / 2

def market_buy(asks, quantity):
    fills, left = [], quantity
    for price, size in asks:  # best (lowest) price first
        take = min(left, size)
        fills.append((price, take))
        left -= take
        if left == 0:
            break
    filled = quantity - left
    average = sum(price * size for price, size in fills) / filled
    return fills, average

for quantity in (200, 1000, 3000):
    fills, average = market_buy(asks, quantity)
    cost_bps = (average - mid) / mid * 10_000
    print(f"{quantity:>5} shares: {len(fills)} levels, average {average:.4f}, cost {cost_bps:.1f} bps")

It prints a cost of 1.0 bp for 200 shares (one level, half the spread), 1.9 bps for 1,000 shares (three levels) and 3.2 bps for 3,000 shares (five levels). Fifteen times the size costs more than three times as much per share.

Where this shows up in quant work

  • Execution. Every strategy pays the spread and the cost of walking the book. A backtest that assumes trades at the mid overstates profits, sometimes enough to turn a loser into a winner.
  • Market making and high-frequency trading. These businesses are built on the details: queue position, the order book's shape, and how it reacts to trades.
  • Signals. The order book itself carries information. An imbalance between bid and ask size predicts short-term price moves, which is the signal used in the conditional expectation lesson.

Exercises

  • In the code's book, how many shares can you buy before your average cost exceeds 2.5 bps over the mid?
  • A stock's best bid is 49.98 and best ask is 50.02. What is the spread in basis points? What does a round trip (buy, then immediately sell) cost a small trader?
  • Extend market_buy to a limit_buy(asks, quantity, limit_price) that only takes asks at or below the limit and returns the unfilled quantity, which would rest in the book as a new bid.

Key takeaways

  • The order book lists waiting buy (bid) and sell (ask) orders by price; orders at the same price queue by arrival time.
  • Small market orders pay half the spread; large ones walk the book and pay more per share.
  • Trades consume liquidity and move the price.
  • Depth varies, and is often thinnest when you most want to trade.