A matching engine turns ordered trading instructions into book updates and fills. Follow a price-time event ledger and see why sequencing, cancellations and venue rules matter.
Matching engine
A matching engine is the trading system component that applies a venue's execution rules to incoming orders and the current order state, producing trades and updated outstanding quantities. For a quant, its practical importance is that a trade at your limit price does not establish that your order filled: the engine must allocate that execution to your order under the applicable priority rules.
The order book is the state the engine works on. Price-time priority, pro-rata allocation and auction rules are possible matching mechanisms. None alone defines the whole engine.
Start with the accepted event sequence
A useful model treats matching as a state transition. Given the same initial state, rule configuration and ordered sequence of accepted events, a deterministic engine produces the same fills and final state. The state includes order identifiers, outstanding quantities, priority and trading phase; it may also include reference prices and control settings. A historical replay needs those inputs, not just successive best quotes.
The relevant sequence is the venue's processing order within the applicable matching domain. Two traders' send timestamps do not determine it: network paths, gateways and sequencing intervene. Nor does this require a single global sequence across every instrument and venue. Where a rule uses random allocation or a tie-break, reproducing the result also requires that input or its recorded outcome.
An order acknowledgement records acceptance, whereas a fill records execution. For a concrete protocol example, Nasdaq OUCH 5.0 has separate Accepted, Executed, Canceled and Replaced messages, with incremental executed quantities. Its October 2025 specification also distinguishes participants' private order updates from the broader ITCH market-data feed. These are different views of the trading process, not interchangeable ledgers. Nasdaq OUCH specification.
A book walk with identifiable counterparties
Consider a fictional continuous market with price first, then acceptance-time priority at each price. All three asks are ordinary displayed orders; A is accepted before B, then C. No order changes while the incoming buy is processed. Prices are USD per share, quantities are whole shares, executions occur at resting ask prices and fees are excluded.
Resting order
Ask price
Available shares
Priority at its price
A
100.01
100
First
B
100.01
50
Second
C
100.02
200
First
A buy limit order for 250 shares with limit 100.02 is marketable against both prices. The engine consumes A's 100 shares, then B's 50, then 100 of C's 200. Better price precedes time: even if C had arrived earlier than A, its higher ask would still follow the cheaper asks under these stipulated rules.
The buy has three fills and no remainder. A and B leave the book; C remains with 100 shares at 100.02. The cash consideration before fees is
100(100.01)+50(100.01)+100(100.02)=25,003.50.
The quantity-weighted average execution price is 25,003.50/250=100.014. This average need not lie on the order-price grid: each actual fill is at a permitted price, while their weighted average is a summary statistic.
Fictional price-time ask book: A has 100 shares and B 50 at USD 100.01, with A ahead; C has 200 at 100.02. The buy fills 100, 50 and 100 shares respectively, leaving C 100. Notional is USD 25,003.50 and average 100.014.
Original deterministic example, with no fees, hidden interest, self-trade conflict or intervening events. The rows show execution order, not network timestamps.
Sequencing changes the answer. If B's full cancellation is processed before this buy, the buy instead takes A's 100 and C's 150, leaving C with 50. Its notional rises to USD 25,004.00. If B merely sent a cancellation but the buy is processed first, the original fills remain the result. Sending a cancel is not confirmation that the remaining order has been removed.
INTERACTIVE EXPLORER
Order-event replay and duplicates
Replay identified fill events with or without idempotent processing.
Partial fills, replacement and controls
After every fill, both counterparties' outstanding quantities must decrease consistently. C's partial fill is not a new 100-share order; it is the remaining quantity of the original order in this example. A cancellation removes only unexecuted quantity and does not undo a completed trade. A replacement must reconcile any fills that occurred while the request was travelling. Nasdaq's OUCH specification explicitly illustrates that in-flight execution issue. OUCH §§2.2–2.3 and 3.3–3.6.
Priority after a modification is a rule choice. CME's documented FIFO algorithm requeues an order after a quantity increase, price change or account-number change. That is a concrete CME rule, not a reason to assume every modification on every venue loses priority. CME supported algorithms, FIFO.
Eligibility also matters before allocating a possible match. Risk checks can reject an instruction before it enters the book; depending on the architecture, checks may sit at the broker, gateway or venue. Self-trade prevention can cancel or decrement otherwise crossing interest according to configured rules. OUCH includes an AIQ cancellation message distinct from an execution. A cancelled or prevented quantity must not be counted as traded volume. OUCH §3.5.
An engine is not a universal FIFO queue
As checked on 2026-10-31, Nasdaq Equity 4 Rule 4757 describes price/display/time priority: better prices first, then displayed orders in time order, followed by non-displayed interest in time order at the same price. Reserve and special order attributes therefore require more state than a single visible FIFO queue. The Closing Cross has separate price-selection and allocation provisions in Rule 4754; a continuous book walk is not an auction model. Nasdaq Equity 4.
CME's current documentation, checked the same day, lists several supported algorithms. Its pro-rata mechanism allocates by resting size at a price, with specified rounding, minimum allocations and residual handling. Other configurations include top-order or lead-market-maker stages. Product and phase determine which rules apply; seeing CME as the venue does not identify the algorithm. CME supported matching algorithms.
This is why a credible execution simulator states its matching assumptions explicitly. Aggregate depth can support a frozen book walk, but generally cannot identify which individual order cancelled, its priority, hidden quantity or competing messages processed before yours. The Order Books lesson develops those allocation and data limits (public preview; full lesson requires membership).