OrderBook

Struct OrderBook 

Source
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

Source

pub fn new() -> Self

Creates a new empty order book.

§Examples
use order_book::OrderBook;

let order_book = OrderBook::new();
Source

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)); // 100
Source

pub fn insert_order(&mut self, order: Order) -> OrderEvent

Inserts a new order into the order book and returns an event.

This method:

  1. Adds the order to the appropriate price level (maintaining time priority)
  2. Returns an OrderEvent that 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);
Source

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);
Source

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

Source

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

Source

pub fn orders_at_exact_price_level(&self, price: Decimal, side: Side) -> usize

Returns the total number of orders at a specific price level.

§Arguments
  • price: The price level to query
  • side: The side (bid or ask) to query
§Returns

The number of orders at that price level, or 0 if no orders exist

Trait Implementations§

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§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.