Skip to main content

OrderBook

Struct OrderBook 

Source
pub struct OrderBook {
    pub symbol: Symbol,
    /* private fields */
}
Expand description

A level-2 order book for a single symbol.

Fields§

§symbol: Symbol

The instrument this book tracks.

Implementations§

Source§

impl OrderBook

Source

pub fn new(symbol: Symbol) -> Self

Constructs a new empty OrderBook for symbol. Sequence starts at 0.

Source

pub fn apply_delta(&mut self, delta: BookDelta) -> Result<(), FinError>

Applies a BookDelta to the order book.

§Errors

Returns FinError::SequenceMismatch if delta.sequence != self.sequence + 1.

Source

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

Returns the best bid (highest price) or None if the bid side is empty.

Returns None if the book is empty or if the stored price is somehow non-positive (which is structurally prevented by apply_delta).

Source

pub fn best_quote(&self) -> Option<(PriceLevel, PriceLevel)>

Returns (best_bid, best_ask) as a tuple, or None if either side is empty.

Convenience wrapper for accessing both sides of the top-of-book in one call.

Source

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

Returns the best ask (lowest price) or None if the ask side is empty.

Returns None if the book is empty or if the stored price is somehow non-positive (which is structurally prevented by apply_delta).

Source

pub fn mid_price(&self) -> Option<Decimal>

Returns the mid-price (best_ask + best_bid) / 2, or None if either side is empty.

Source

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

Returns the spread best_ask - best_bid, or None if either side is empty.

Source

pub fn spread_pct(&self) -> Option<Decimal>

Returns the spread as a percentage of the mid-price: spread / mid * 100.

Returns None when either side is empty or mid-price is zero.

Source

pub fn depth_at(&self, side: Side, price: Price) -> Option<Decimal>

Returns the resting quantity at a specific price level, or None if the level is absent.

Source

pub fn top_bids(&self, n: usize) -> Vec<PriceLevel>

Returns the top n bid levels in descending price order.

Source

pub fn top_asks(&self, n: usize) -> Vec<PriceLevel>

Returns the top n ask levels in ascending price order.

Source

pub fn vwap_for_qty( &self, side: Side, qty: Quantity, ) -> Result<Decimal, FinError>

Computes the volume-weighted average price to fill qty on side.

Walks levels from best to worst until qty is filled.

§Errors

Returns FinError::InsufficientLiquidity if the book cannot fill qty.

Source

pub fn sequence(&self) -> u64

Returns the last successfully applied sequence number.

Source

pub fn snapshot(&self, n: usize) -> (Vec<PriceLevel>, Vec<PriceLevel>)

Returns the top n bid and ask levels as a snapshot.

Returns (bids, asks) where bids are in descending price order and asks are in ascending price order.

Source

pub fn bid_count(&self) -> usize

Returns the number of bid price levels.

Source

pub fn ask_count(&self) -> usize

Returns the number of ask price levels.

Source

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

Returns the number of price levels on the given side.

Source

pub fn clear(&mut self)

Removes all price levels from both sides of the book, resetting sequence to 0.

Source

pub fn remove_all(&mut self, side: Side)

Removes all resting levels from side, leaving the opposite side intact.

Useful when a snapshot update arrives for one side only (e.g., bid-side snapshot).

Source

pub fn is_crossed(&self) -> bool

Returns true if the book is currently in a crossed (inverted) state.

A book is crossed when best_bid >= best_ask. Under normal operation this is always false since apply_delta rejects crossing deltas. Provided for diagnostic / assertion use.

Source

pub fn is_empty(&self) -> bool

Returns true if both sides of the book have no resting quantity.

Source

pub fn total_levels(&self) -> usize

Returns the total number of distinct price levels across both sides.

Source

pub fn cumulative_depth(&self, side: Side, price: Price) -> Decimal

Returns the total resting quantity available on side up to and including price.

For bids: sums all bid levels at prices >= price (levels at or above the given price). For asks: sums all ask levels at prices <= price (levels at or below the given price).

Returns Decimal::ZERO when there are no matching levels.

Source

pub fn total_bid_volume(&self) -> Decimal

Returns the total resting quantity on the bid side.

Source

pub fn total_ask_volume(&self) -> Decimal

Returns the total resting quantity on the ask side.

Source

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

Returns the best bid price, or None if the bid side is empty.

Source

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

Returns the best ask price, or None if the ask side is empty.

Source

pub fn best_bid_qty(&self) -> Option<Quantity>

Returns the resting quantity at the best bid, or None if the bid side is empty.

Source

pub fn best_ask_qty(&self) -> Option<Quantity>

Returns the resting quantity at the best ask, or None if the ask side is empty.

Source

pub fn liquidity_at_pct( &self, side: Side, pct_from_mid: Decimal, ) -> Option<Decimal>

Returns the total resting quantity on side within pct_from_mid percent of the mid-price.

For example, liquidity_at_pct(Side::Ask, dec!(0.5)) returns all ask volume within 0.5% above the mid-price. Returns None when the book has no mid-price.

Source

pub fn has_price(&self, side: Side, price: Price) -> bool

Returns true if price is present in the given side of the book.

Source

pub fn weighted_mid(&self) -> Option<Decimal>

Returns the quantity-weighted midpoint (micro-price).

Weights best-bid by ask quantity and best-ask by bid quantity: (bid_price × ask_qty + ask_price × bid_qty) / (bid_qty + ask_qty). Returns None when either side is empty.

Source

pub fn imbalance(&self) -> Option<Decimal>

Returns the order-book imbalance: (bid_vol - ask_vol) / (bid_vol + ask_vol).

Returns None when both sides are empty (division by zero). Range is (-1, 1): positive = bid-heavy, negative = ask-heavy.

Source

pub fn depth_ratio(&self, n: usize) -> Option<Decimal>

Returns the depth ratio top_n_bid_vol / top_n_ask_vol for the best n levels.

A ratio > 1 indicates more buying pressure at the top of book; < 1 more selling pressure. Returns None when either side has no levels in the top-n or ask volume is zero.

Source

pub fn weighted_mid_price(&self) -> Option<Decimal>

👎Deprecated since 2.1.0:

Use weighted_mid instead

Returns the weighted mid price: (best_bid * ask_qty + best_ask * bid_qty) / (bid_qty + ask_qty).

Weights the midpoint by the opposite side’s quantity, so a thick ask pulls the WMP toward bid.

Alias for weighted_mid.

Source

pub fn price_levels_between( &self, side: Side, lo: Price, hi: Price, ) -> Vec<PriceLevel>

Returns all price levels on side whose price falls within [lo, hi] (inclusive).

Useful for computing the available liquidity within a price band.

Source

pub fn tick_size(&self) -> Option<Decimal>

Returns the smallest price increment between adjacent levels on either side.

Useful for estimating the instrument’s native tick size from live book data. Returns None when both sides have fewer than 2 levels.

Source

pub fn bid_ask_ratio(&self) -> Option<Decimal>

Returns the bid-to-ask volume ratio: total_bid_volume / total_ask_volume.

Values > 1 indicate more buy-side depth; values < 1 indicate more sell-side depth. Returns None if either side is empty (to avoid division by zero).

Source

pub fn price_impact(&self, side: Side, qty: Quantity) -> Option<Decimal>

Estimates the average fill price for a market order of qty on side.

Walks the book levels in price-time priority and returns the volume-weighted average price. Returns None if qty is zero or the book cannot fill qty in full (insufficient depth).

Source

pub fn bid_depth(&self, n: usize) -> Vec<PriceLevel>

Returns the top n bid levels in descending price order (best bid first).

Returns fewer than n levels if the bid side has fewer entries.

Source

pub fn ask_depth(&self, n: usize) -> Vec<PriceLevel>

Returns the top n ask levels in ascending price order (best ask first).

Returns fewer than n levels if the ask side has fewer entries.

Source

pub fn depth_imbalance(&self) -> Option<Decimal>

Returns the depth imbalance ratio: (bid_qty - ask_qty) / (bid_qty + ask_qty).

Result is in [-1.0, 1.0]:

  • Positive → more bid-side depth (buying pressure)
  • Negative → more ask-side depth (selling pressure)
  • None when both sides are empty (total depth is zero)
Source

pub fn ask_bid_ratio(&self) -> Option<Decimal>

Returns the ask-to-bid quantity ratio: total_ask_qty / total_bid_qty.

Values above 1 indicate more supply than demand at visible depth levels. Returns None when total bid quantity is zero (avoid division by zero).

Source

pub fn total_bid_depth(&self) -> Decimal

Returns the total quantity across all bid price levels.

Source

pub fn total_ask_depth(&self) -> Decimal

Returns the total quantity across all ask price levels.

Source

pub fn price_at_volume(&self, side: Side, target_qty: Decimal) -> Option<Price>

Walks the book on side to find the price level reached after consuming target_qty.

For Side::Ask walks ascending (cheapest ask first). For Side::Bid walks descending (highest bid first).

Returns the price of the level where target_qty is fully absorbed, or the last available level if the book lacks sufficient depth. Returns None when the side has no levels or target_qty is zero.

Source

pub fn top_n_bid_levels(&self, n: usize) -> Vec<PriceLevel>

Returns up to n best bid levels in descending price order (best bid first).

Returns an empty Vec when the bid side is empty or n == 0.

Source

pub fn top_n_ask_levels(&self, n: usize) -> Vec<PriceLevel>

Returns up to n best ask levels in ascending price order (best ask first).

Returns an empty Vec when the ask side is empty or n == 0.

Source

pub fn cumulative_bid_qty(&self, n: usize) -> Decimal

Returns the total quantity across the top n bid levels.

Sweeps from the best (highest) bid downwards and sums quantities. Returns zero when the bid side is empty or n == 0.

Source

pub fn bid_depth_skew(&self, n: usize) -> Option<Decimal>

Returns the bid-to-ask depth skew across the top n levels on each side.

bid_depth_skew = cumulative_bid_qty(n) / (cumulative_bid_qty(n) + cumulative_ask_qty(n)). Range: 0.0 (all ask-side depth) to 1.0 (all bid-side depth). Returns None if both sides are empty or n == 0.

Source

pub fn spread_bps(&self) -> Option<Decimal>

Returns the bid-ask spread in basis points.

spread_bps = (best_ask - best_bid) / mid_price * 10_000. Returns None if either side is empty or mid-price is zero.

Source

pub fn cumulative_ask_qty(&self, n: usize) -> Decimal

Returns the total quantity across the top n ask levels.

Sweeps from the best (lowest) ask upwards and sums quantities. Returns zero when the ask side is empty or n == 0.

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

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.