Skip to main content

Overview

The OrderBook 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.
Parameters:
  • instrument_id: Unique identifier for the instrument
Example:

Market Data Updates

apply_book_update

Applies an incremental book update message.
Parameters:
  • update: MDP3.0 incremental book update message
Handles:
  • New price levels
  • Price/quantity changes
  • Level deletions
  • Delete-through and delete-from operations
Example:

apply_snapshot

Applies a full book snapshot message.
Parameters:
  • snapshot: MDP3.0 snapshot message
Details:
  • Clears existing book state
  • Rebuilds book from snapshot data
  • Resets sequence number
Use case: Initial book construction or recovery after gap

Best Bid and Offer (BBO)

best_bid

Returns the best bid price and quantity.
Returns: Some((price, quantity)) or None if no bids

best_ask

Returns the best ask price and quantity.
Returns: Some((price, quantity)) or None if no asks

Example

Price Calculations

mid_price

Calculates the mid-price between best bid and ask.
Returns: Some(price) if both sides exist, otherwise None Formula: (best_bid + best_ask) / 2

spread

Calculates the bid-ask spread.
Returns: Spread as a Price (in ticks) Formula: best_ask - best_bid

quote

Returns a complete quote with BBO and timestamp.
Returns: Quote struct containing bid/ask prices, quantities, and timestamp

Level Access

bid_level

Gets a bid level by index (0 = best).
Parameters:
  • 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.
Example:

total_ask_quantity

Calculates total ask quantity up to N levels.

VWAP Calculation

vwap

Calculates volume-weighted average price for a given quantity.
Parameters:
  • side: Side::Buy (uses ask side) or Side::Sell (uses bid side)
  • quantity: Total quantity to fill
Returns: VWAP if sufficient liquidity exists, otherwise 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).
Returns: true if best bid >= best ask (market anomaly)

Metadata

instrument_id

Returns the instrument ID.

sequence

Returns the current sequence number.
Use case: Gap detection in market data feed

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.
Details: CME uses exponents to represent decimal places (e.g., -2 for 2 decimals)

OrderBookTrait Implementation

The OrderBook 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