> ## Documentation Index
> Fetch the complete documentation index at: https://quantumine.net/learn/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This library is published in two languages with the same paths: English at https://quantumine.net/learn/<path> and Vietnamese at https://quantumine.net/learn/vi/<path>. Add .md to either address for the Markdown version.
> llms.txt and llms-full.txt list the English pages only. When the reader writes in Vietnamese, read and cite the Vietnamese page.
> To search the library, connect to the MCP server at https://quantumine.net/learn/mcp. Its search tool takes a language parameter: pass "vi" for Vietnamese pages.
> The library is licensed under CC BY 4.0: it may be quoted, reused and used to train models. Credit Quantumine and link to the page you used.

# Order book and order matching

> An order book is a list of the buy and sell orders waiting to be filled, ranked by price. A new order trades against the resting order with the best price, at the resting order's price. At the same price, the earlier order trades first.

An order book is the list of buy and sell orders for an asset that are waiting to be filled on an exchange, ranked by price. A new order sent to the exchange either trades at once against a resting order on the opposite side or goes into the book and waits. The exchange picks the resting order to trade against by two priority rules: the better price trades first, and at the same price the earlier order trades first. The trade price is the price of the resting order.

## What does an order book hold?

The diagram below draws an order book with made-up numbers, after the [illustration of Gould et al.](https://arxiv.org/abs/1012.0349).

Each box is a resting order and the number in it is its size. Buy orders rest below the price axis and sell orders above it, and at each price level the box next to the axis is the order that arrived first. The diagram labels five terms to know:

* **Best bid** is the highest price at which someone is waiting to buy, here 100.
* **Best ask** is the lowest price at which someone is waiting to sell, here 102.
* **Spread** is the gap between the two prices: 102 − 100 = 2.
* **Mid price** is the average of the two: (100 + 102) / 2 = 101.
* **Tick size** is the smallest price change the exchange allows, here 1.

The book holds only unfilled orders: an order that has traded or been cancelled leaves it. Stocks, futures and crypto on a centralized exchange have order books, while forex and CFDs trade at prices that a dealer or broker quotes, as the article [Exchanges and OTC markets](https://quantumine.net/learn/learn/foundations/exchanges-and-otc-markets.md) explains.

## When a new order arrives, does it wait or trade, and at what price?

There are two basic kinds of order. A market order names no price: it buys or sells at once at the best price available. A limit order buys at no more than the price you set, or sells at no less. The exchange handles the two along two branches:

```mermaid theme={null}
%%{init: {"flowchart": {"nodeSpacing": 18, "rankSpacing": 36}}}%%
flowchart LR
  A(["New order reaches the exchange"]) --> K{"Order type?"}
  K -- "Market order" --> M["Trades against the best resting order<br/>at the resting order's price"]
  M --> M2{"Size left<br/>unfilled?"}
  M2 -- "Yes: next price level" --> M
  M2 -- "No" --> T(["Aggressive order<br/>(taker)"])
  K -- "Limit order" --> L{"Marketable?"}
  L -- "Yes" --> L1["Trades against the best resting order<br/>at the resting order's price"]
  L1 --> L2{"Size left<br/>unfilled?"}
  L2 -- "Yes" --> L
  L2 -- "No" --> T
  L -- "No" --> P["Joins the book, at the back of the queue<br/>at its price"]
  P --> PM(["Passive order<br/>(maker)"])
  classDef default fill:#2a78d614,stroke:#2a78d6,stroke-width:1px
  classDef strong fill:#2a78d633,stroke:#2a78d6,stroke-width:2px
  class A,T,PM strong
```

The best resting order is the one with the best price on the opposite side and, at the same price, the one that arrived first. A limit order is marketable when it can trade at once: a buy limit order when its limit is not below the best ask, a sell limit order when its limit is not above the best bid. Send three buy orders into the book of the first diagram, each starting from the original book, and you get three results:

| Order sent to the exchange | Path through the diagram | Result |
| - | - | - |
| Market order to buy 5 | Trades 4 at 102, 1 left, moves to the next level and trades 1 at 103 | Aggressive order, average buy price (4 × 102 + 1 × 103) / 5 = 102.2 |
| Limit order to buy 2 at 101 | The best ask is 102, above 101: not marketable | Joins the book at 101, a passive order |
| Limit order to buy 8 at 102 | Marketable at 102: trades 4, 4 left. The best ask is now 103: no longer marketable | The part that traded is aggressive; the other 4 units rest at 102 as a passive order |

The rule for picking the resting order to trade against is called price-time priority, and most exchanges match orders continuously this way. In an exchange's fee schedule, an aggressive order is a taker and a passive order is a maker, and each role pays its own fee.

## Why does the price move?

The word “price” on a screen covers three different numbers: the best bid, the best ask and the last price, the price of the trade that just happened. The simulation below sends orders at random into an order book. On the left are the best bid and best ask over time, drawn over the size resting at each price. On the right is the order book at the current moment, with a Total column that accumulates size from the best price outward.

Take the book of the first diagram again, with a best bid of 100 and a best ask of 102. These two prices change in only three ways:

1. **An aggressive order takes the whole best level.** A market order to buy 5 takes the 4 units resting at 102 and 1 unit at 103, so the best ask rises to 103.
2. **A passive order is placed inside the spread.** A limit order to buy 2 at 101 trades with no one, but it bids higher than every resting buy order, so the best bid rises to 101.
3. **The orders at the best level are cancelled.** If both buy orders at 100 are cancelled, the best bid falls to 99.

The last two need no trade at all. The last price moves in its own way: an aggressive sell trades at the best bid and an aggressive buy at the best ask, so the last price can jump back and forth between 100 and 102 while the book stands still.

So why does the price move? Aggressive orders create trades and use up price levels, but passive orders also move the price whenever one is newly placed or cancelled.

## References

* [Statistical theory of the continuous double auction](https://arxiv.org/abs/cond-mat/0210475) by Smith, Farmer et al.: a model of an order book with random order flow, the idea the simulation on this page rests on.
* [Exchange Matching Engine](https://docs.cdp.coinbase.com/exchange/concepts/matching-engine) from Coinbase: an exchange's documentation of price priority, time priority and the trade price.
* [What are maker and taker fees?](https://support.binance.us/en/articles/9842914-what-do-maker-and-taker-mean) from Binance.US: maker, taker and the fee when an order fills only in part.

## Read next

> [card] [Exchanges and OTC markets](https://quantumine.net/learn/learn/foundations/exchanges-and-otc-markets.md)
>
> Which markets match orders through an exchange's order book and which trade through dealers.

> [card] [Market participants](https://quantumine.net/learn/learn/foundations/market-participants.md)
>
> Who stands behind the resting orders in the book, market by market.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.