AnyLearn
All lessons
Businessintermediate

The Limit Order Book: Where a Price Comes From

A market price is not published by anyone. It is the residue of a queue of resting orders and the orders that consume them. This lesson builds the limit order book and its matching rules, shows what a fill actually costs, and takes apart the assumption that an instrument has one price at all.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 9

Nobody sets the price

It is natural to imagine an exchange as a place that quotes a price, the way a shop does. It is closer to a noticeboard.

Participants post offers to buy or sell at prices they choose. The exchange stores them, sorts them, and matches them when two are compatible. It never forms an opinion about value and never quotes anything of its own. Every number on a market data screen is derived from other people's orders.

This matters because it changes what the word price means. There is a highest price someone is currently willing to pay, a lowest price someone is currently willing to accept, and a record of what the last trade happened at. Those are three different numbers, and none of them is the price.

The rest of this lesson is about the structure that produces them, because everything else in trading is downstream of it.

Full lesson text

All 9 steps on one page, for reading, reference, and search.

Show

1. Nobody sets the price

It is natural to imagine an exchange as a place that quotes a price, the way a shop does. It is closer to a noticeboard.

Participants post offers to buy or sell at prices they choose. The exchange stores them, sorts them, and matches them when two are compatible. It never forms an opinion about value and never quotes anything of its own. Every number on a market data screen is derived from other people's orders.

This matters because it changes what the word price means. There is a highest price someone is currently willing to pay, a lowest price someone is currently willing to accept, and a record of what the last trade happened at. Those are three different numbers, and none of them is the price.

The rest of this lesson is about the structure that produces them, because everything else in trading is downstream of it.

2. Two orders, and the asymmetry between them

Nearly everything reduces to two instructions.

A limit order specifies a price and a size: buy 500 at 99.98, no worse. If nobody will trade there now, it rests in the book and waits. It may never fill.

A market order specifies size only: buy 500, now, at whatever the book offers. It always fills, if there is depth, and the price is whatever it turns out to be.

The trade-off is exact and it is the first thing to internalise. A limit order controls price and surrenders certainty of execution. A market order controls execution and surrenders control of price. There is no instruction that gives both, because the two are precisely what the counterparty is choosing between as well.

The resting limit orders are collectively called liquidity. An order that rests provides it; an order that executes against a resting order takes it. Most exchange fee schedules price these differently, and most trading strategy is a decision about which side of that line to be on.

3. The book, and the rule that orders it

Resting orders are held in two sorted queues. Bids descend from the highest price, offers ascend from the lowest.

        BIDS                  ASKS
  size   price          price   size
   200 @ 99.98    |     99.99 @ 150
   800 @ 99.97    |    100.00 @ 900
  1500 @ 99.95    |    100.01 @ 400

The best bid is 99.98 and the best ask 99.99. Together they are the touch, or best bid and offer. The gap between them is the spread, here one cent. Their average, 99.985, is the mid.

Matching follows price-time priority. A better price always executes first. Among orders at the same price, the one that arrived earlier executes first. That second clause creates a queue, and position in it is a real asset: at a crowded price level, being late means waiting through everyone ahead of you, and the level may be consumed or cancelled before your turn arrives.

4. A matching engine, in miniature

The rules are simple enough to write out, which is worth doing once because the behaviour that follows is not obvious from prose.

import heapq
from itertools import count

_seq = count()   # arrival order, breaks price ties

def add_limit_buy(bids, asks, price, size, trades):
    # cross against the book while an ask is at or below our limit
    while size > 0 and asks and asks[0][0] <= price:
        ask_px, _, ask_sz = heapq.heappop(asks)
        traded = min(size, ask_sz)
        trades.append((ask_px, traded))      # fills at the RESTING price
        size -= traded
        if ask_sz > traded:                  # partial: put the rest back
            heapq.heappush(asks, (ask_px, _, ask_sz - traded))
    if size > 0:                             # remainder rests in the book
        heapq.heappush(bids, (-price, next(_seq), size))

bids, asks, trades = [], [], []
for px, sz in [(99.99, 150), (100.00, 900)]:
    heapq.heappush(asks, (px, next(_seq), sz))

add_limit_buy(bids, asks, 100.00, 500, trades)
trades          # [(99.99, 150), (100.0, 350)]

One order, two fills, two prices. The average paid is about 99.997, worse than the 99.99 on the screen when the order was sent. Nothing went wrong: 150 was all that existed at that price.

5. What an incoming order actually does

The loop is the important part. An order does not transact at one price, it walks the book consuming levels until it is filled or runs out of limit.

Two consequences follow immediately. First, the price you get depends on the size you send relative to the depth available, so the same order is cheap in a liquid instrument and expensive in a thin one. Second, a market order is just a limit order with no price constraint, which is why it can never fail to fill and can always fill badly.

This walk is also the mechanism behind slippage: the difference between the price you saw and the average price you got. It is not a fee and nobody charges it.

flowchart TD
A["Incoming buy order"] --> B["Is there an ask at or below the limit?"]
B --> C["Yes: fill at the resting order's price"]
C --> D["Size remaining?"]
D --> B
B --> E["No: rest in the book as a new bid"]
E --> F["Wait, and possibly never fill"]

6. Depth is the number that matters

The spread gets quoted because it is one number and it is visible. Depth, the size resting at and near the touch, usually matters more.

Compare two instruments both showing a one-cent spread. The first has 50,000 resting on each side at the touch; the second has 200. To trade 5,000 shares, the first costs the spread and nothing else, while the second walks through many levels and pays far more.

So a tight spread is a claim about the price of a small trade. It says nothing about the price of yours. The pair of numbers to ask for is spread and size-at-touch, and for larger orders the whole shape of the book several levels deep.

The practical form of this is a simple exercise: for the size you actually trade, compute what walking the book would cost right now. That number is the real spread for you, and it can be many multiples of the quoted one.

7. Most orders are cancelled, not filled

The book is not a slow-moving list. In liquid electronic markets the large majority of submitted orders are cancelled rather than executed, and the top of the book can change many times a second.

That has a practical consequence which surprises people coming from a screen-trading intuition: the price you saw is a historical fact by the time your order arrives. Between your decision and your order reaching the matching engine, the resting order you aimed at may be gone.

A marketable order that arrives to find the level gone will simply fill at the next level, and you pay more. This is why order types exist to control the failure mode: fill-or-kill refuses partial execution, immediate-or-cancel takes what is available and discards the rest, and a plain limit order simply rests and waits.

Choosing among them is choosing which risk to accept, not eliminating one.

8. Which price, for which purpose

Because there is no single price, every use has to pick one, and picking wrongly produces errors that look like profits.

NumberWhat it isUse it for
Best bidHighest resting buyWhat you get selling small, now
Best askLowest resting sellWhat you pay buying small, now
MidAverage of the twoMarking positions, measuring costs
Last tradePrice of the previous executionReporting; it is already stale
VWAPVolume-weighted average over a windowBenchmarking a worked order

The common mistake is marking a position at the last trade or at the favourable side of the touch. A book of positions marked at bid when long and ask when short reports value that cannot be realised, because exiting means crossing the spread in the other direction.

Mid is the standard convention for valuation precisely because it is the one number that does not embed a direction.

9. The question this leaves open

The mechanism is now fully specified. Orders arrive, sort by price then time, and match. No participant is privileged and no price is announced.

What the mechanism does not explain is why the spread is not zero.

Someone posting a bid at 99.98 and an offer at 99.99 is inviting others to trade against them on both sides. If they are willing to buy at 99.98, and the fair value is 99.985, why not bid 99.984 and capture more of the flow? Competition should compress the spread until it disappears, and in a market with no transaction costs there seems to be nothing left to pay for.

It does not disappear, and the reason is not fees. The next lesson shows that a spread persists even when trading is free and the participant posting it expects to make nothing at all.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. What does a limit order surrender that a market order does not?
    • Control over the price paid
    • Certainty of execution
    • The ability to trade in size
    • Priority in the matching queue
  2. Under price-time priority, what determines which of two orders at the same price fills first?
    • The larger order
    • The order from the participant with more resting liquidity
    • The order that arrived earlier
    • The order with the shorter time in force
  3. A buy of 500 meets 150 resting at 99.99 and the rest at 100.00. What has happened?
    • The order was rejected because insufficient size existed at the quoted price
    • All 500 filled at 99.99 because that was the displayed price
    • All 500 filled at 100.00 because the order walked to the deepest level
    • The order walked the book, filling at each resting price in turn, so the average is worse than the quote
  4. Two instruments both show a one-cent spread. Why might one be far more expensive to trade?
    • Depth differs, so a given size walks through more levels in the thinner book
    • The tick size is different
    • One uses price-time priority and the other does not
    • Last-trade prices are reported on different delays
  5. Why is mid the standard convention for marking positions?
    • It always equals the most recent trade price
    • It is the only price the exchange publishes officially
    • It is the one number that does not embed a direction, so it cannot flatter a long or a short
    • It updates less often, which reduces reporting noise

Related lessons

Business
advanced

Where the Risk Moved

Central clearing was extended after the financial crisis because it removes counterparty risk from the network. It does not remove it from the system: it concentrates it in a small number of institutions that are now indispensable. This lesson assesses what was gained, what was created, and how to read any infrastructure change for the risk it relocates.

8 steps·~12 min
Business
advanced

Settlement, Custody, and What Happens When It Fails

Settlement is one instant of exchange, and getting it right means never letting the two legs come apart. This lesson covers delivery versus payment, where securities actually live and who is in the chain, why settlement fails are routine rather than scandalous, and what shortening the cycle costs.

8 steps·~12 min
Business
advanced

The Central Counterparty and Its Default Waterfall

A CCP takes the other side of every trade, which means one member's failure becomes its problem and therefore everyone's. This lesson covers how it survives that: the margin it collects, the ordered stack of resources it burns through in a default, and the deliberate design choice of putting its own capital ahead of the mutualised fund.

8 steps·~12 min
Business
intermediate

After the Fill: The Gap Nobody Sees

A trade is agreed in microseconds and completed days later. In between, both sides hold a promise rather than an asset, and either could fail. This lesson builds the trade lifecycle, explains why the gap exists at all, and introduces the legal manoeuvre that lets a stranger's creditworthiness stop being your problem.

8 steps·~12 min