Skip to content
All library documents

B-Tree Market Depth for L2 and L3 Order Book Updates

Code Stratmill research code

Summary

The document presents a market-depth implementation that stores bid and ask quantities by integer price ticks in ordered B-tree maps. It tracks the best bid and ask, converts prices to ticks using a configured tick size, and filters near-zero quantities relative to a lot size. For level-three data, it also maps order identifiers to individual orders so additions, deletions, and modifications can update aggregated depth at each price.

Snapshot application rebuilds the two sides from event data, while depth-clearing operations remove levels on one or both sides. The code includes tests for order modifications and warns that missing feed updates can leave the book crossed, preventing ordinary refresh logic from restoring accurate top-of-book values. This is implementation documentation, not a trading strategy or empirical study. The excerpt also leaves snapshot serialization unimplemented and reveals edge cases that require care, including best-price updates after deletions and maintaining consistency between order records and aggregated depth.

Key ideas

  • Bids and asks are stored by integer price tick in ordered maps, making the best levels available from map extremes.
  • Level-three order records support aggregation of quantities and updates by price level.
  • Price and quantity handling depends on configured tick size and lot size.
  • Snapshots rebuild depth from event data, and clearing can target one side or both sides.
  • Missing feed data can leave a crossed book that natural updates may not repair.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.