Expand description
In-process L2 order book state.
This crate keeps limit order books in memory and applies exchange feeds to them: full snapshots, incremental deltas, and the sequence checks needed to notice when a feed has dropped a message.
§Design
- Fixed-capacity sides. Each side of a book is a sorted
Levelarray with the best level at the end, so applying an update is a short scan plus a small memmove with no allocation. - Integer prices. Prices and quantities are
Scale9fixed-point values rather thanf64or barei64, so arithmetic is exact and an unscaled number cannot be passed where a scaled one belongs. - Sequence tracking.
BookStore::apply_deltarejects an update that skips a sequence number instead of silently corrupting the book.
§Example
use depthbook::{f64_to_scale9, BookStore, Level};
let store = BookStore::new();
store.apply_snapshot(
"binance",
"BTC-USDT",
&[Level::new(f64_to_scale9(50_000.0), f64_to_scale9(1.0))],
&[Level::new(f64_to_scale9(50_001.0), f64_to_scale9(2.0))],
1,
1_700_000_000_000_000_000,
)?;
let (bid, ask) = store.bbo("binance", "BTC-USDT").unwrap();
assert_eq!(ask.price - bid.price, f64_to_scale9(1.0));
// A delta that skips sequence 2 is refused rather than applied.
let gap = store.apply_delta("binance", "BTC-USDT", &[], &[], 7, 1_700_000_000_000_000_001);
assert!(gap.is_err());
assert_eq!(store.stats().sequence_gaps, 1);§Scope
This is a data structure, not a feed handler. It does no I/O and speaks no exchange
protocol; feeding it is the caller’s job. State lives in the process that owns the
BookStore and is not shared between processes or persisted.
Re-exports§
pub use decimal::f64_to_scale9;pub use decimal::scale9_to_f64;pub use decimal::scale9_to_string;pub use decimal::str_to_scale9;pub use decimal::Scale9;pub use decimal::SCALE;pub use decimal::SCALE9;pub use error::Error;pub use error::Result;pub use intern::InternedString;pub use store::BookStore;pub use store::Stats;pub use types::Book;pub use types::Insertion;pub use types::Level;pub use types::Side;pub use types::MAX_LEVELS;
Modules§
- book_
state - Lock and atomic bookkeeping around a single
Book. - decimal
- Fixed-point prices and quantities.
- error
- Error type for order book operations.
- intern
- String interning for efficient venue and instrument storage.
- store
- Concurrent store holding one
Bookper venue and instrument. - types
- The book itself: levels, sides, and a full book.