Overview
TheOrderBook provides a high-performance limit order book implementation that maintains up to 20 price levels on each side. It processes CME MDP3.0 market data messages and provides efficient queries for market data, depth analysis, and feature extraction.
Source: nano-lob/src/orderbook.rs
OrderBook
BookLevel
Represents a single price level in the order book.Construction
new
Creates a new empty order book for an instrument.
instrument_id: Unique identifier for the instrument
Market Data Updates
apply_book_update
Applies an incremental book update message.
update: MDP3.0 incremental book update message
- New price levels
- Price/quantity changes
- Level deletions
- Delete-through and delete-from operations
apply_snapshot
Applies a full book snapshot message.
snapshot: MDP3.0 snapshot message
- Clears existing book state
- Rebuilds book from snapshot data
- Resets sequence number
Best Bid and Offer (BBO)
best_bid
Returns the best bid price and quantity.
Some((price, quantity)) or None if no bids
best_ask
Returns the best ask price and quantity.
Some((price, quantity)) or None if no asks
Example
Price Calculations
mid_price
Calculates the mid-price between best bid and ask.
Some(price) if both sides exist, otherwise None
Formula: (best_bid + best_ask) / 2
spread
Calculates the bid-ask spread.
Price (in ticks)
Formula: best_ask - best_bid
quote
Returns a complete quote with BBO and timestamp.
Quote struct containing bid/ask prices, quantities, and timestamp
Level Access
bid_level
Gets a bid level by index (0 = best).
index: Level index (0 = best bid, 1 = second best, etc.)
ask_level
Gets an ask level by index (0 = best).
bid_at_level
Gets bid price and quantity at a specific level.
ask_at_level
Gets ask price and quantity at a specific level.
Example
Level Iteration
bid_levels
Returns an iterator over bid levels from best to worst.
ask_levels
Returns an iterator over ask levels from best to worst.
Example
Depth Queries
bid_depth
Returns the number of bid levels.
ask_depth
Returns the number of ask levels.
total_bid_quantity
Calculates total bid quantity up to N levels.
total_ask_quantity
Calculates total ask quantity up to N levels.
VWAP Calculation
vwap
Calculates volume-weighted average price for a given quantity.
side:Side::Buy(uses ask side) orSide::Sell(uses bid side)quantity: Total quantity to fill
None
Use case: Estimating execution price for a market order
Example:
State Queries
is_empty
Checks if the book has no levels on either side.
is_valid
Checks if the book has at least one level on both sides.
is_crossed
Checks if the book is crossed (bid >= ask).
true if best bid >= best ask (market anomaly)
Metadata
instrument_id
Returns the instrument ID.
sequence
Returns the current sequence number.
update_count
Returns the total number of updates processed.
timestamp
Returns the timestamp of the last update.
Maintenance
clear
Clears all levels and resets the book.
set_exponent
Sets the price exponent for the instrument.
OrderBookTrait Implementation
TheOrderBook implements the OrderBookTrait from nano-core, providing a standard interface for all order book implementations.
Complete Example
Performance Characteristics
- Update complexity: O(log N) for insert/delete, where N <= 20
- BBO access: O(1)
- Level access: O(log N)
- Memory usage: ~4KB per book (with 20 levels)
See Also
- LobFeatureExtractor - Feature extraction from order books
- Market Data Feed - Parsing CME market data