Quant Memo
Core

Order Book Reconstruction from Message Feeds

Raw exchange data doesn't arrive as a picture of the order book — it arrives as a stream of add, modify, and cancel messages, and a simulator has to replay that stream correctly, in order, to rebuild the book state a strategy would actually have seen at any instant.

Prerequisites: L1, L2 and L3 Data Fidelity for Simulation

Exchanges don't publish "the order book" as a live snapshot — they publish a stream of discrete messages: a new order was added at this price and size, an existing order was modified or canceled, a trade occurred. To simulate what the book looked like at any given millisecond, or what a resting order's queue position was, a backtest has to apply every one of those messages in the exact order they were sent and maintain the running book state itself. This process — turning a message stream into a live order book — is order book reconstruction, and getting it wrong produces a simulator that's confidently showing the wrong market.

The mechanics: a running ledger, not a lookup

Reconstruction works like replaying a ledger: start from either an initial snapshot or an empty book, then apply each message in sequence — add inserts a new resting order at its price and size; modify changes size (and, depending on the exchange's rules, may reset the order's queue priority); cancel removes an order; a trade message reduces the resting quantity at the price level it executed against and, on exchanges that report full order-level detail (L3), identifies which specific order was hit. Every message must be applied in the sequence number or timestamp the exchange assigned it, because these messages are only meaningful relative to each other — a cancel message means nothing without knowing exactly which prior add it's canceling, and applying messages out of order produces a book state that never actually existed.

Worked example: reconstructing five messages

Starting from an empty book at one price level, the exchange sends:

#MessageBook at this price after applying
1Add order A, size 100100 (A)
2Add order B, size 50150 (A, B)
3Cancel order A50 (B)
4Add order C, size 80130 (B, C)
5Trade: 50 executes against B80 (C)

After message 5, the reconstructed book correctly shows only order C resting with size 80 — order A is long gone, order B was fully consumed by the trade, and the queue order matters too: because B arrived before C and wasn't canceled, B had priority and was the one filled by the incoming trade, not C, even though C also had resting size at that price. Getting message 3 processed before message 4, or applying the trade before the cancel, would silently produce a different, wrong book — this is why sequence numbers, not arrival time at the simulator, must drive the reconstruction.

add A add B cancel A, add C trade fills B
Each message updates the book state in sequence — the final state (rightmost) only makes sense as the cumulative result of all five messages applied strictly in order.

What this means in practice

Reconstruction quality is the ceiling on simulation fidelity for anything that depends on book depth or queue position — a matching engine or fill-probability model is only as good as the reconstructed book it's simulating against. Common failure modes are dropped or out-of-order messages from a lossy feed, misinterpreting a "modify" as preserving queue priority when the exchange's actual rule resets it, and gaps handled by silently skipping rather than flagging — any of which quietly corrupts every downstream simulation result without an obvious error.

Order book reconstruction rebuilds live book state by replaying add/modify/cancel/trade messages in their exact sequence — the book is never handed to you directly, it has to be derived, and doing so out of order produces a book state that never actually existed. This reconstructed book is the foundation every higher-fidelity simulation feature (queue position, matching, fill probability) is built on top of.

A common bug is applying messages by the time they arrived at the simulator's storage rather than by the exchange's own sequence number — network jitter and multi-threaded ingestion can reorder messages in transit, and reconstructing from arrival order instead of true sequence silently produces an impossible book state.

Related concepts

Practice in interviews

Further reading

  • Harris, Trading and Exchanges, ch. 4
ShareTwitterLinkedIn