Basic Prerequisites: 03 · The Limit Order Book.


1. Intuition & Practical Objective

The matching engine is the deterministic heart of an exchange: a pure function that takes an incoming order and the current book and returns a list of trades plus a new book. There is no optimisation, no discretion - just a priority rule applied over and over. This page makes the engine exact, because the priority rule is where real money lives: two orders at the same price are not equal - the one that arrived first is filled first, and that difference is worth latency, technology, and cancellations.

The objective is threefold:

  1. State the priority rule precisely (price-time / FIFO, and its pro-rata alternative).
  2. Derive fill odds from queue position - the number that decides whether a passive quote is worth posting.
  3. Specify the engine algorithm so it can be implemented (and audited) exactly.

The one-sentence essence. “The matching engine consumes the best-priced, oldest orders first; therefore a resting order’s expected fill is governed not by its price but by the size queued ahead of it - so queue position, not price, is the true currency of passive liquidity provision.”


2. Mathematical Ground Truth & Derivations

2.1 Priority order. Resting orders on side are ranked by a lexicographic key. The engine always matches the minimum key on the opposite side:

Price priority first (better price always wins), time priority second (ties broken by earliest arrival, FIFO). When a match occurs, the trade prints at the resting (older) order’s price (Hasbrouck §2.1: “matched when price limits overlap, trade at the price of the first (older) order”) - so the maker’s price prevails and the taker can receive price improvement.

2.2 The engine as an algorithm. Let an incoming order have side , limit (for market orders ), and remaining size . Match:

“Crosses” means for a buy, for a sell. Any remainder either rests at (limit orders) or is handled by the venue rule (rest / reject / cancel). This is exactly the code in §3.

2.3 Pro-rata, the alternative priority rule. Some venues (notably short-end futures) split a fill proportionally to resting size rather than FIFO:

Under pro-rata, size - not time - buys priority, so a large order at the back can out-fill a small order at the front. The optimal posting behaviour is different (you post big, early, and don’t bother racing for the front), which is why the priority rule is a first-class design choice for exchanges.

2.4 Queue position and fill odds. Suppose you post size at price behind lots (your initial queue position). Under FIFO, executions at consume the queue from the front, and cancellations ahead also move you forward. Let = cumulative lots executed at by time . Then

If executions at arrive as a Poisson process of rate , then and the probability of being completely filled by is

Two consequences drive the entire market-making desk: (i) the expected wait to fill grows roughly linearly in (the queue you join), and (ii) a deeper position is not merely slower - it is more adversely selected (§2.5), because the only way a long queue clears is a burst of aggressive flow, which is exactly when the price moves.

2.5 Why queue position is a risk, not a free option. Combining §2.4 with the adverse-selection economics of page 02: while you wait behind lots, the price can move through your quote. With adverse price-move rate and queue-clearing rate , a race argument gives

which increases toward 1 as grows. Being at the back therefore means you are filled mainly in the states where you would rather not be. This single fact is the reason passive market making needs L3 data, dynamic cancel/replace, and latency - all of it is queue-position management.


3. Computational Implementation - a FIFO matching engine, verified

A complete price-time-priority engine: limit orders (with crossing), market orders, partial fills, maker/taker accounting, and queue position. Standard library only.

The trade log is the whole proof: order #5 arrived later than #1 and #2 at the same price, so the market sell of 150 fills #1 first (100 lots), then #2 (50 lots) - never #5. Note the post-sweep L2: $100.00 still shows 200 lots, but that is 150 (the rest of #2) + 50 (#5) - the aggregate hides that a fresh arrival now sits behind 150 lots. That hidden queue position is precisely what L3 data exists to expose.


4. Failure Modes & First-Principles Breakdowns

  1. Queue-position risk (the core failure). A passive order that sits behind a large queue may never fill; if it does fill, it fills when the queue finally clears - a burst of aggressive flow, i.e. when the price is moving. You are systematically last in line exactly when being last is dangerous. This is not a modelling error; it is the structure of FIFO.
  2. Priority loss for the “clever” order types. Hidden and reserve/iceberg orders trade visibility for fill odds, but they lose time priority. Posting hidden to avoid signaling can move you behind a later-arriving visible order - a self-inflicted queue penalty (02).
  3. Pro-rata inverts the incentive. Optimising for a FIFO venue (race to the front, small size) is exactly wrong on a pro-rata venue (post large, early). A strategy transplanted without checking the venue’s priority rule can be systematically at the back - a silent, structural leak.
  4. Partial fills break naive size logic. A market order larger than the touch is filled in slices at successively worse prices; a limit order can be partially filled and then cancelled. Code that assumes all-or-nothing position updates will misstate inventory. (Venue AON qualifiers exist precisely to avoid this.)
  5. The engine is deterministic, but the clock is not. “Time priority” is defined by the venue’s timestamp; across fragmented venues with independent clocks, the same logically-ordered events can be sequenced differently, so a resting order’s rank is venue-specific. Latency and clock quality therefore change fill outcomes (§05; Queue Position & Fill Probability).

5. References

  • Hasbrouck, Empirical Market Microstructure
  • Foucault, Pagano & Röell, Market Liquidity
  • Huang, Lehalle & Rosenbaum (2015), Simulating and analysing the queue-reactive model, Journal of Statistical Mechanics
  • Parlour (1998), Price dynamics in limit order markets, Review of Financial Studies 11(4)
  • Foucault, Kadan & Kandel (2005), Limit order book as a market for liquidity, Review of Financial Studies 18(4)

6. Connected Graph Bridges