Order Book Feeds: Snapshots and Incremental Updates
How exchanges publish order book data as snapshots and incremental updates, and how to build and maintain an accurate local order book in code without errors.
An order book feed tells your program how the queue of buy and sell orders changes over time. Rather than sending the whole book on every change, which would be enormous, venues send a starting snapshot and then a stream of small updates called deltas. Your program combines them to keep a local copy of the book. Done correctly, the local book matches the exchange's view within milliseconds. Done carelessly, it drifts into a fictional market with prices and sizes that no longer exist.
Feed styles#
| Style | Content | Typical source |
|---|---|---|
| Market by price (MBP) | Total size at each price level | Most crypto exchanges, many futures feeds |
| Market by order (MBO) | Every individual order with an ID | Professional exchange feeds |
| Top of book | Best bid and ask only | Lightweight feeds |
| Periodic snapshots | Full book every N milliseconds | Some retail APIs |
See Market Data Levels: Level 1, 2 and 3.
Snapshot plus deltas#
- Subscribe to the delta stream and start buffering messages.
- Request a snapshot of the book, which includes an update ID or sequence number.
- Discard buffered deltas older than the snapshot.
- Apply remaining deltas in order.
- Check continuity: each delta's ID must follow the previous one; if not, start again from step 2.
A delta in a market by price feed usually says "price level X on the bid side now has size Y". A size of zero means remove the level.
def apply_delta(book_side, price, size):
if size == 0:
book_side.pop(price, None)
else:
book_side[price] = size
Keeping the book sorted#
The best bid is the highest bid price and the best ask is the lowest ask price. A sorted structure, such as a sorted dictionary or a balanced tree, makes reading the top levels fast. For many retail uses, a plain dictionary plus sorting the top few levels when needed is fast enough.
Sanity checks#
| Check | Problem it reveals |
|---|---|
| Best bid below best ask | A crossed book usually means missed updates |
| Sequence continuity | Lost messages. See Sequence Numbers, Dropped Packets and Out-of-Order Messages |
| Checksums | Some exchanges publish a checksum of the top levels to compare against |
| Book age | Stale book when updates stop |
| Level count | Unbounded growth from never removed levels |
If any check fails, rebuild from a fresh snapshot rather than trying to patch the book.
Market by order books#
MBO feeds send add, modify, cancel and execute messages for individual orders. Your program tracks each order ID and its price and size, and aggregates them into price levels. This allows queue position estimates, useful for market making and execution research. See Fill Probability and Queue Position.
Performance#
Busy books can produce thousands of updates per second. Keep update handling minimal, avoid recalculating everything on each message and move analytics to a separate process if needed. See Exchange vs Receive Timestamps and Latency Measurement and Message Queues.
Recording books#
Storing every delta allows exact replay later, for backtesting execution strategies and debugging. Store snapshots periodically too, so replay can start from any point without processing the whole day. See Market Data Replay.
Frequently asked questions#
What is an order book snapshot?#
A full picture of the bids and asks at a moment in time, usually with an ID that lets later updates be applied correctly.
Why is my local order book crossed?#
Most often because updates were missed or applied out of order. Rebuild the book from a new snapshot.
What is the difference between market by price and market by order?#
Market by price gives total size at each price level; market by order shows every individual order and its changes.
Next, learn how systems normalise feeds from many venues in Feed Handlers and Normalization.
3 quick questions on this lesson. Get them all right to finish it.
Turn on JavaScript to take the quiz.
Mentioned in
- Data Storage, Compression and CachingProgramming and Data
- Market Data ReplayProgramming and Data
- Historical Data for BacktestingResearch and Backtesting
- Fill Models, Partial Fills and Order QueuesResearch and Backtesting
- Binary ProtocolsTrading Infrastructure
- Quant Developer and Trading EngineerThe Trading Industry