pub struct OrderBook { /* private fields */ }Expand description
The core order book structure that maintains price-time priority.
This structure is responsible only for:
- Storing orders at each price level
- Maintaining price priority (best bid/ask)
- Publishing events when orders are inserted
It does not maintain aggregated market depth, as that is handled by the external
MarketDepthCache service to minimize lock contention.
§Thread Safety
This structure is designed to be wrapped in a RwLock for concurrent access.
The write lock should be held only briefly during order insertion.
Implementations§
Source§impl OrderBook
impl OrderBook
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty order book.
§Examples
use order_book::OrderBook;
let order_book = OrderBook::new();Sourcepub fn aggregate_price_to_level(price: Decimal) -> Decimal
pub fn aggregate_price_to_level(price: Decimal) -> Decimal
Aggregates a precise price to its integer price level.
This function truncates the decimal portion of the price, effectively grouping all orders with prices like 100.01, 100.25, 100.99 into the same aggregated level of 100.
§Arguments
price: The exact order price to aggregate
§Returns
The truncated price level as a Decimal
§Examples
use order_book::OrderBook;
use rust_decimal::Decimal;
let price = Decimal::new(10025, 2); // 100.25
let aggregated = OrderBook::aggregate_price_to_level(price);
assert_eq!(aggregated, Decimal::new(100, 0)); // 100Sourcepub fn insert_order(&mut self, order: Order) -> OrderEvent
pub fn insert_order(&mut self, order: Order) -> OrderEvent
Inserts a new order into the order book and returns an event.
This method:
- Adds the order to the appropriate price level (maintaining time priority)
- Returns an
OrderEventthat downstream services can use to update their state
The write lock should be held only during this operation, which is $O(\log{N})$ where $N$ is the number of distinct price levels.
§Arguments
order: The order to insert
§Returns
An OrderEvent describing the change that occurred
§Examples
use order_book::{OrderBook, Order, Side};
use rust_decimal::Decimal;
let mut order_book = OrderBook::new();
let order = Order::new(100.50, 100, Side::Bid);
let event = order_book.insert_order(order);
assert_eq!(event.quantity_delta, 100);Sourcepub fn compute_spread(
&self,
) -> (Option<Decimal>, Option<Decimal>, Option<Decimal>)
pub fn compute_spread( &self, ) -> (Option<Decimal>, Option<Decimal>, Option<Decimal>)
Computes the current best bid and best ask prices.
This operation acquires a read lock and is O(1) due to the BTreeMap structure:
- Best bid is the highest price in the bid map (last key)
- Best ask is the lowest price in the ask map (first key)
§Returns
A tuple of (best_bid, best_ask) where each is Option<Decimal>.
Returns None if there are no orders on that side.
§Examples
use order_book::{OrderBook, Order, Side};
use rust_decimal::Decimal;
let mut order_book = OrderBook::new();
order_book.insert_order(Order::new(100.50, 100, Side::Bid));
let (best_bid, best_ask, spread) = order_book.compute_spread();
assert_eq!(best_bid, Some(Decimal::new(10050, 2)));
assert_eq!(best_ask, None);Sourcepub fn bid_levels_count(&self) -> usize
pub fn bid_levels_count(&self) -> usize
Returns the number of distinct price levels on the bid side.
§Returns
The count of unique bid price levels
Sourcepub fn ask_levels_count(&self) -> usize
pub fn ask_levels_count(&self) -> usize
Returns the number of distinct price levels on the ask side.
§Returns
The count of unique ask price levels