Skip to main content

OrderBook

Struct OrderBook 

Source
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

Source

pub fn new() -> Self

Create a new empty order book.

Source

pub fn next_order_id(&mut self) -> OrderId

Generate the next order ID (monotonically increasing).

Source

pub fn next_trade_id(&mut self) -> TradeId

Generate the next trade ID (monotonically increasing).

Source

pub fn next_timestamp(&mut self) -> Timestamp

Generate the next timestamp (monotonically increasing).

Source

pub fn peek_next_order_id(&self) -> OrderId

Peek at what the next order ID would be (without consuming it).

Source

pub fn get_order(&self, order_id: OrderId) -> Option<&Order>

Get an order by ID (includes historical filled/cancelled orders).

Source

pub fn get_order_mut(&mut self, order_id: OrderId) -> Option<&mut Order>

Get a mutable reference to an order by ID.

Source

pub fn contains_order(&self, order_id: OrderId) -> bool

Check if an order exists.

Source

pub fn order_count(&self) -> usize

Returns the total number of orders (including historical).

Source

pub fn active_order_count(&self) -> usize

Returns the number of active orders (on the book).

Source

pub fn bids(&self) -> &PriceLevels

Get the bids side (buy orders).

Source

pub fn asks(&self) -> &PriceLevels

Get the asks side (sell orders).

Source

pub fn bids_mut(&mut self) -> &mut PriceLevels

Get mutable access to bids.

Source

pub fn asks_mut(&mut self) -> &mut PriceLevels

Get mutable access to asks.

Source

pub fn side(&self, side: Side) -> &PriceLevels

Get the appropriate side for an order.

Source

pub fn side_mut(&mut self, side: Side) -> &mut PriceLevels

Get mutable access to the appropriate side.

Source

pub fn opposite_side(&self, side: Side) -> &PriceLevels

Get the opposite side (for matching).

Source

pub fn opposite_side_mut(&mut self, side: Side) -> &mut PriceLevels

Get mutable access to the opposite side.

Source

pub fn best_bid(&self) -> Option<Price>

Get the best bid price (highest buy price).

Source

pub fn best_ask(&self) -> Option<Price>

Get the best ask price (lowest sell price).

Source

pub fn best_bid_ask(&self) -> (Option<Price>, Option<Price>)

Get both best bid and best ask.

Source

pub fn spread(&self) -> Option<i64>

Get the spread (best ask - best bid), if both exist.

Source

pub fn is_crossed(&self) -> bool

Check if the book is crossed (best bid >= best ask). This should never happen after matching is complete.

Source

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.

Source

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.

Source

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:

  1. Generates the next order ID
  2. Generates the next timestamp
  3. Creates the Order struct

The order is NOT added to the book — use add_order() for that.

Source

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

pub fn compact(&mut self)

Remove all tombstones from the book.

Source§

impl OrderBook

Source

pub fn match_order(&mut self, incoming: &mut Order) -> MatchResult

Match an incoming order against the book.

This is the core matching algorithm:

  1. Find the best price on the opposite side
  2. If prices cross, fill against resting orders (FIFO)
  3. Continue until no more crosses or order is filled
  4. 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.

Source

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

pub fn can_fully_fill( &self, side: Side, price: Price, quantity: Quantity, ) -> bool

Check if an order can be fully filled (for FOK orders).

Source§

impl OrderBook

Source

pub fn snapshot(&self, depth: usize) -> BookSnapshot

Take a snapshot of the top N levels on each side.

Source

pub fn full_snapshot(&self) -> BookSnapshot

Take a full snapshot of all levels.

Trait Implementations§

Source§

impl Clone for OrderBook

Source§

fn clone(&self) -> OrderBook

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OrderBook

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for OrderBook

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.