Align an Order-Book Snapshot with Buffered Updates

By DX Research Group · · Market data

A sequence-boundary fixture and absolute-size example catch two common errors in reconstructed depth.

Start a reconstructed book from a snapshot and apply only updates that the venue protocol says follow it. The snapshot request and stream delivery overlap, so their arrival order alone cannot determine the book's state. We would make sequence alignment an adapter contract before using reconstructed depth to size an agent order.

Coinbase's channel documentation specifies discarding full-channel messages at or below the snapshot sequence. Its level2 updates supply replacement size at a price level. Those are separate channel contracts, even though both maintain books.

The snapshot already contains some buffered events

Assume a fictional full-channel snapshot with sequence 700. While fetching it, the collector buffers events 698, 699, 700, 701, and 702. In this illustrative protocol, events through 700 are already reflected in the snapshot. Replaying all five double-applies earlier changes; replaying only 701 and 702 preserves the boundary.

Buffered sequenceTreatmentReason
698 to 700Discard for reconstructionIncluded by snapshot boundary
701ApplyFirst subsequent event
702ApplyContinuous successor

The collector should still retain the original messages for diagnostics. Discarding a message from reconstruction differs from deleting its provenance.

Quantity replacement needs its own test

Now assume a separate fictional level2 fixture. A snapshot has size 5 at bid price 100. An update supplies size 3 at that price under an absolute-size protocol. The resulting size is 3. Treating the field as a signed increment produces 8 and overstates liquidity by 5 units.

A later zero-size update removes the level. Repeated application of the absolute-size update leaves size 3, which is useful for idempotency. An adapter for a genuine incremental protocol would need different arithmetic and duplicate handling. Name the quantity convention explicitly in the schema instead of letting every downstream consumer infer it.

Keep the book generation attached to features

A successful rebuild begins a new book generation. Features such as spread, imbalance, and depth should reference that generation plus their final applied update boundary. A feature calculated before recovery can otherwise survive in cache after the book has been replaced, creating a mixture of valid and invalid states in one agent prompt.

Construct the feature bundle only after alignment completes. Clear or recompute dependent caches together, and preserve the rejected feature version if it already reached a decision. This makes the change inspectable during an execution audit.

A proposed regression case combines a delayed snapshot, duplicate absolute-size updates, a removed price level, and a missing successor. Check the final levels and feature lineage, rather than only whether the adapter returns JSON. Our depth-unit note explains the arithmetic after reconstruction. Our state and memory framework places these features in the persistent runtime. These fixtures support a narrow adapter acceptance result; they establish no profitability effect by themselves.

Sources

Related field notes