# Limit Order Book: Price–Time Priority and Slippage

Limit and market orders, price–time priority, depth, how a market order walks the book, and slippage measured against the mid price.

"Market makers and liquidity" described what a market maker does: it quotes a bid and an ask and earns the spread for standing ready to trade. This lesson builds the machine those quotes live in, the **limit order book**. It defines the two kinds of order, shows how the book is organised and who gets filled first, and then follows a large market order down the book to measure what it costs to trade size.

Prices are quoted in USD per share, and a **tick** is the smallest allowed price step, 0.01 here. Sizes are in shares.

## Orders

A **limit order** has three parts: a side (buy or sell), a price and a size. It says: trade up to this size, at this price or better. A buy limit order at 99.99 accepts any price of 99.99 or lower. If nothing on the other side is willing to trade at that price, the order **rests** in the book and waits. Resting orders are what provide liquidity.

A **market order** has a side and a size but no price. It trades immediately against the best resting orders on the opposite side, whatever their prices are. A market buy hits resting sell orders; a market sell hits resting buy orders. It takes liquidity instead of providing it.

A limit order whose price already reaches the other side, such as a buy at 100.05 when the best sell is at 100.01, is **marketable**. The part that can trade does so at once, at the resting orders' prices, and any remainder rests at the limit price. A resting order can also be **cancelled** at any time, which removes it from the book without a trade.

## The book

The book collects all resting limit orders. Buy orders are the **bids** and sell orders the **asks**. Bids are sorted from the highest price down, asks from the lowest price up, so the best prices of both sides face each other in the middle.

The **best bid** $b$ is the highest bid and the **best ask** $a$ the lowest ask. Together they are the **touch**. The **spread** is $a - b$, and the **mid price** is their average:

$$
m = \frac{a + b}{2}.
$$

At each price the book keeps a **level**, the queue of orders resting there. The **depth** of a level is the total size of its orders, and the **cumulative depth** at a level is the depth of that level plus all better ones on its side. In the examples below the best bid is 99.99 and the best ask 100.01, so the mid is 100.00 and the spread is two ticks.

## Price–time priority

When a market order arrives, the book needs a rule for which resting order trades first. Almost every exchange uses **price–time priority**: orders at a better price trade first (higher for bids, lower for asks), and among orders at the same price the one that arrived earlier trades first. A level is therefore a first-in, first-out queue.

Both parts of the rule reward the people who provide liquidity. A better price moves an order to the front of the book, and an earlier arrival moves it to the front of its queue. The price rule pushes quotes towards each other and narrows the spread; the time rule rewards speed, and it is why a later lesson on queues treats a place in the line as something worth having. Cancelling an order and re-entering it, even at the same price, sends it to the back of the queue.

## Walking the book

A market buy of size $Q$ takes the whole of the best ask level first. If that is not enough, it moves to the next ask level, and so on until all $Q$ shares are bought. The order **walks the book**. Suppose it takes $q_i$ shares at price $p_i$ at the $i$-th level it reaches, so that $\sum_i q_i = Q$. Its **average price** is the size-weighted average of those prices:

$$
\bar p = \frac{\sum_i p_i\, q_i}{\sum_i q_i}.
$$

The measure of cost is **slippage**, the gap between the average price and the mid price at the moment the order was sent. For a buy it is positive when $\bar p$ is above $m$, and it is quoted in **basis points** (bp, one hundredth of a percent) of the mid. In dollars, the total cost of the order against the mid is the shares times the price gap:

$$
\text{slippage} = \frac{\bar p - m}{m}\cdot 10^{4}\ \text{bp}, \qquad \text{cost} = Q\,(\bar p - m).
$$

For a market sell the signs flip: the order receives $\bar p$ below the mid, so slippage is $(m - \bar p)/m$. Even a one-share order pays something. With the best ask at 100.01 and the mid at 100.00, it costs 0.01 per share, or 1 bp. That is half the spread, the price of immediacy from the previous lesson.

### Worked example: A market buy of 1,600 shares

1. The asks are 100.01 for 318 shares, 100.02 for 311, 100.03 for 518 and 100.04 for 453. The first three levels are taken in full, so the fourth level supplies the rest of the order.
   $$1600 - 318 - 311 - 518 = 453$$ → **453**
2. The order consumes all four levels exactly. Its notional value is the sum of price times shares over the levels, in USD.
   $$318\cdot 100.01 + 311\cdot 100.02 + 518\cdot 100.03 + 453\cdot 100.04$$ → **160043.06**
3. Divide by the 1,600 shares to get the average price.
   $$\bar p = 160043.06 / 1600$$ → **100.0269**
4. Slippage against the mid of 100.00, in basis points.
   $$\frac{100.0269 - 100.00}{100.00}\cdot 10^{4}$$ → **2.69**
5. Cost against the mid in USD.
   $$1600\,(100.0269125 - 100.00)$$ → **43.06**

## Depth and the cost of size

The worked example shows the pattern: every extra level costs one more tick. To see how the cost grows with size, take a simple book in which every level holds the same depth $d$ and the best ask is $h$ ticks above the mid (here $h = 1$). Level $j = 1, 2, \dots$ then sits $h + j - 1$ ticks above the mid.

A buy of $Q = k\,d$ shares takes $k$ complete levels. Its average distance above the mid is the average of those $k$ distances, and the arithmetic series $\sum_{j=1}^{k}(j-1) = k(k-1)/2$ gives

$$
\bar p - m = \left(h + \frac{k-1}{2}\right)\text{ticks}, \qquad k = \frac{Q}{d}.
$$

Slippage is therefore about linear in size: a fixed part $h$ for the spread and a part that grows by $1/(2d)$ ticks for every extra share. Multiplying by $Q$ gives the total cost in ticks:

$$
Q\,(\bar p - m) = Q\,h + \frac{Q^2}{2d} - \frac{Q}{2}\;\approx\;\frac{Q^2}{2d}\quad\text{for large } Q.
$$

Total cost grows with the square of the size. Doubling an order roughly quadruples the part of its cost that comes from walking the book, because it takes twice as many levels and each of its shares is also dearer on average. Deep books, with a large $d$, flatten this. Real books are usually thinner at the touch and thicker further out, so the first levels are the expensive ones and the curve eventually bends over; the quadratic rule is a guide for moderate sizes, not a law.

The figure runs this experiment on a book with ten levels a side, about 450 shares each beyond the first four. Choose a market buy or sell and drag the size: the rows the order consumes are highlighted, and the curve beside them shows slippage against size, with a marker at the chosen size. At the default of 1,600 shares it reproduces the worked example, four levels and 2.69 bp, costing 43.06 USD. A single level of 100 shares costs 1.00 bp. Double the size to 3,200 and eight levels are hit: slippage rises to 4.50 bp and the cost to 144.06 USD, more than three times as much for twice the shares.

*[Interactive figure: book walk](https://quantsandbox.dev/guide/microstructure/limit-order-book)*

> A market order at the touch pays half the spread, and every further level adds a tick to the average price. Cost against the mid is $Q(\bar p - m)$, which grows roughly like $Q^2$ in a book of even depth.

## Summary

- A limit order has a side, a price and a size, and rests in the book until it trades or is cancelled; a market order has no price and trades at once against the opposite side.
- The book keeps bids from high to low and asks from low to high, with the best bid, the best ask, the spread and the mid $m = (a+b)/2$ at the touch.
- Price–time priority fills better prices first and earlier orders first within a price, so each level is a first-in, first-out queue.
- A market order walks the book at average price $\bar p = \sum p_i q_i / \sum q_i$, with slippage $(\bar p - m)/m$ and cost $Q(\bar p - m)$.
- Even depth makes slippage about linear in size and total cost about proportional to $Q^2$, so size is expensive.

[Walk the book with market orders in the Order Book lab](https://quantsandbox.dev/order-book)

## Interactive labs

- [Order Book Simulator: Slippage and Queues](https://quantsandbox.dev/order-book)

## Practice questions

### 1. Two buy orders make up the best bid, both at 99.97: 500 shares entered at 09:30:01 and 2,000 shares entered at 09:30:05. A market sell of 600 shares arrives. Who is filled?

- a) The 2,000 in full, because it is the larger order
- b) The 500 in full, then 100 of the 2,000
- c) Both pro rata to their size: 120 and 480
- d) Neither: the market sell trades at the mid price instead

**Answer:** b) The 500 in full, then 100 of the 2,000

At the same price the earlier order has priority. The 500 trades in full, and the remaining 100 shares of the market sell go to the second order, which keeps 1,900 resting. Size does not matter, the book does not share fills pro rata, and a market order trades at the resting orders' prices, not at the mid.

### 2. The asks are 50.00 for 200 shares and 50.01 for 300 shares. A market buy of 400 shares arrives. What is its average price in USD?

**Answer:** 50.005

The order takes 200 shares at 50.00 and the other 200 at 50.01, so $\bar p = (50.00 \cdot 200 + 50.01 \cdot 200)/400 = 50.005$.

### 3. In a book with the same depth at every level, a market buy of size Q walks the book and costs C against the mid, ignoring the spread. Roughly what does a market buy of size 2Q cost?

- a) About 2C
- b) About 3C
- c) About 4C
- d) About 8C

**Answer:** c) About 4C

It takes twice as many levels, so the average gap above the mid doubles, and it buys twice as many shares at that gap. The cost is $Q^2/(2d)$ in ticks, which multiplies by $2^2 = 4$.

---

Source: https://quantsandbox.dev/guide/microstructure/limit-order-book

Previous lesson: [Market Makers Explained: Bid, Ask and Spread](https://quantsandbox.dev/guide/microstructure/market-makers)

Next lesson: [Queue Position and Limit Order Fill Probability](https://quantsandbox.dev/guide/microstructure/queue-position)
