pub struct OrderBook { /* private fields */ }Expand description
The complete order book.
Maintains both sides of the book plus a central index of all orders (active and historical) for O(1) lookup.
Implementations§
Source§impl OrderBook
impl OrderBook
Sourcepub fn next_order_id(&mut self) -> OrderId
pub fn next_order_id(&mut self) -> OrderId
Generate the next order ID (monotonically increasing).
Sourcepub fn next_trade_id(&mut self) -> TradeId
pub fn next_trade_id(&mut self) -> TradeId
Generate the next trade ID (monotonically increasing).
Sourcepub fn next_timestamp(&mut self) -> Timestamp
pub fn next_timestamp(&mut self) -> Timestamp
Generate the next timestamp (monotonically increasing).
Sourcepub fn peek_next_order_id(&self) -> OrderId
pub fn peek_next_order_id(&self) -> OrderId
Peek at what the next order ID would be (without consuming it).
Sourcepub fn get_order(&self, order_id: OrderId) -> Option<&Order>
pub fn get_order(&self, order_id: OrderId) -> Option<&Order>
Get an order by ID (includes historical filled/cancelled orders).
Sourcepub fn get_order_mut(&mut self, order_id: OrderId) -> Option<&mut Order>
pub fn get_order_mut(&mut self, order_id: OrderId) -> Option<&mut Order>
Get a mutable reference to an order by ID.
Sourcepub fn contains_order(&self, order_id: OrderId) -> bool
pub fn contains_order(&self, order_id: OrderId) -> bool
Check if an order exists.
Sourcepub fn order_count(&self) -> usize
pub fn order_count(&self) -> usize
Returns the total number of orders (including historical).
Sourcepub fn active_order_count(&self) -> usize
pub fn active_order_count(&self) -> usize
Returns the number of active orders (on the book).
Sourcepub fn bids(&self) -> &PriceLevels
pub fn bids(&self) -> &PriceLevels
Get the bids side (buy orders).
Sourcepub fn asks(&self) -> &PriceLevels
pub fn asks(&self) -> &PriceLevels
Get the asks side (sell orders).
Sourcepub fn bids_mut(&mut self) -> &mut PriceLevels
pub fn bids_mut(&mut self) -> &mut PriceLevels
Get mutable access to bids.
Sourcepub fn asks_mut(&mut self) -> &mut PriceLevels
pub fn asks_mut(&mut self) -> &mut PriceLevels
Get mutable access to asks.
Sourcepub fn side(&self, side: Side) -> &PriceLevels
pub fn side(&self, side: Side) -> &PriceLevels
Get the appropriate side for an order.
Sourcepub fn side_mut(&mut self, side: Side) -> &mut PriceLevels
pub fn side_mut(&mut self, side: Side) -> &mut PriceLevels
Get mutable access to the appropriate side.
Sourcepub fn opposite_side(&self, side: Side) -> &PriceLevels
pub fn opposite_side(&self, side: Side) -> &PriceLevels
Get the opposite side (for matching).
Sourcepub fn opposite_side_mut(&mut self, side: Side) -> &mut PriceLevels
pub fn opposite_side_mut(&mut self, side: Side) -> &mut PriceLevels
Get mutable access to the opposite side.
Sourcepub fn is_crossed(&self) -> bool
pub fn is_crossed(&self) -> bool
Check if the book is crossed (best bid >= best ask). This should never happen after matching is complete.
Sourcepub fn add_order(&mut self, order: Order)
pub fn add_order(&mut self, order: Order)
Add a new order to the book.
The order must have a unique ID (typically from next_order_id()).
The order is stored in the central index and added to the appropriate
price level based on its side and price.
§Panics
Panics if an order with the same ID already exists.
Sourcepub fn cancel_order(&mut self, order_id: OrderId) -> Option<Quantity>
pub fn cancel_order(&mut self, order_id: OrderId) -> Option<Quantity>
Remove an order from the book (for cancellation).
Updates the order’s status to Cancelled and marks it as a tombstone in the price level queue for O(1) performance.
Sourcepub fn create_order(
&mut self,
side: Side,
price: Price,
quantity: Quantity,
time_in_force: TimeInForce,
) -> Order
pub fn create_order( &mut self, side: Side, price: Price, quantity: Quantity, time_in_force: TimeInForce, ) -> Order
Create a new order with auto-generated ID and timestamp.
This is a convenience method that:
- Generates the next order ID
- Generates the next timestamp
- Creates the Order struct
The order is NOT added to the book — use add_order() for that.
Sourcepub fn clear_history(&mut self) -> usize
pub fn clear_history(&mut self) -> usize
Remove filled and cancelled orders from history to free memory.
Active orders (on the book) are preserved. Returns the number of orders removed.
Use this periodically for long-running instances to prevent unbounded memory growth.
Source§impl OrderBook
impl OrderBook
Sourcepub fn match_order(&mut self, incoming: &mut Order) -> MatchResult
pub fn match_order(&mut self, incoming: &mut Order) -> MatchResult
Match an incoming order against the book.
This is the core matching algorithm:
- Find the best price on the opposite side
- If prices cross, fill against resting orders (FIFO)
- Continue until no more crosses or order is filled
- Return trades and remaining quantity
Important: This method modifies both the incoming order and resting orders in the book. The incoming order is NOT added to the book — the caller decides whether to add it based on TIF.
Sourcepub fn available_to_fill(&self, side: Side, price: Price) -> Quantity
pub fn available_to_fill(&self, side: Side, price: Price) -> Quantity
Calculate how much quantity is available at prices that would cross.
This is used for FOK (fill-or-kill) feasibility checks.
Source§impl OrderBook
impl OrderBook
Sourcepub fn snapshot(&self, depth: usize) -> BookSnapshot
pub fn snapshot(&self, depth: usize) -> BookSnapshot
Take a snapshot of the top N levels on each side.
Sourcepub fn full_snapshot(&self) -> BookSnapshot
pub fn full_snapshot(&self) -> BookSnapshot
Take a full snapshot of all levels.