pub struct OrderBook {
pub symbol: Symbol,
/* private fields */
}Expand description
A level-2 order book for a single symbol.
Fields§
§symbol: SymbolThe instrument this book tracks.
Implementations§
Source§impl OrderBook
impl OrderBook
Sourcepub fn new(symbol: Symbol) -> Self
pub fn new(symbol: Symbol) -> Self
Constructs a new empty OrderBook for symbol. Sequence starts at 0.
Sourcepub fn apply_delta(&mut self, delta: BookDelta) -> Result<(), FinError>
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.
Sourcepub fn best_bid(&self) -> Option<PriceLevel>
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).
Sourcepub fn best_quote(&self) -> Option<(PriceLevel, PriceLevel)>
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.
Sourcepub fn best_ask(&self) -> Option<PriceLevel>
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).
Sourcepub fn mid_price(&self) -> Option<Decimal>
pub fn mid_price(&self) -> Option<Decimal>
Returns the mid-price (best_ask + best_bid) / 2, or None if either side is empty.
Sourcepub fn spread(&self) -> Option<Decimal>
pub fn spread(&self) -> Option<Decimal>
Returns the spread best_ask - best_bid, or None if either side is empty.
Sourcepub fn spread_pct(&self) -> Option<Decimal>
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.
Sourcepub fn depth_at(&self, side: Side, price: Price) -> Option<Decimal>
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.
Sourcepub fn top_bids(&self, n: usize) -> Vec<PriceLevel>
pub fn top_bids(&self, n: usize) -> Vec<PriceLevel>
Returns the top n bid levels in descending price order.
Sourcepub fn top_asks(&self, n: usize) -> Vec<PriceLevel>
pub fn top_asks(&self, n: usize) -> Vec<PriceLevel>
Returns the top n ask levels in ascending price order.
Sourcepub fn vwap_for_qty(
&self,
side: Side,
qty: Quantity,
) -> Result<Decimal, FinError>
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.
Sourcepub fn snapshot(&self, n: usize) -> (Vec<PriceLevel>, Vec<PriceLevel>)
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.
Sourcepub fn level_count(&self, side: Side) -> usize
pub fn level_count(&self, side: Side) -> usize
Returns the number of price levels on the given side.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all price levels from both sides of the book, resetting sequence to 0.
Sourcepub fn remove_all(&mut self, side: Side)
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).
Sourcepub fn is_crossed(&self) -> bool
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.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if both sides of the book have no resting quantity.
Sourcepub fn total_levels(&self) -> usize
pub fn total_levels(&self) -> usize
Returns the total number of distinct price levels across both sides.
Sourcepub fn cumulative_depth(&self, side: Side, price: Price) -> Decimal
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.
Sourcepub fn total_bid_volume(&self) -> Decimal
pub fn total_bid_volume(&self) -> Decimal
Returns the total resting quantity on the bid side.
Sourcepub fn total_ask_volume(&self) -> Decimal
pub fn total_ask_volume(&self) -> Decimal
Returns the total resting quantity on the ask side.
Sourcepub fn best_bid_price(&self) -> Option<Price>
pub fn best_bid_price(&self) -> Option<Price>
Returns the best bid price, or None if the bid side is empty.
Sourcepub fn best_ask_price(&self) -> Option<Price>
pub fn best_ask_price(&self) -> Option<Price>
Returns the best ask price, or None if the ask side is empty.
Sourcepub fn best_bid_qty(&self) -> Option<Quantity>
pub fn best_bid_qty(&self) -> Option<Quantity>
Returns the resting quantity at the best bid, or None if the bid side is empty.
Sourcepub fn best_ask_qty(&self) -> Option<Quantity>
pub fn best_ask_qty(&self) -> Option<Quantity>
Returns the resting quantity at the best ask, or None if the ask side is empty.
Sourcepub fn liquidity_at_pct(
&self,
side: Side,
pct_from_mid: Decimal,
) -> Option<Decimal>
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.
Sourcepub fn has_price(&self, side: Side, price: Price) -> bool
pub fn has_price(&self, side: Side, price: Price) -> bool
Returns true if price is present in the given side of the book.
Sourcepub fn weighted_mid(&self) -> Option<Decimal>
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.
Sourcepub fn imbalance(&self) -> Option<Decimal>
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.
Sourcepub fn depth_ratio(&self, n: usize) -> Option<Decimal>
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.
Sourcepub fn weighted_mid_price(&self) -> Option<Decimal>
👎Deprecated since 2.1.0: Use weighted_mid instead
pub fn weighted_mid_price(&self) -> Option<Decimal>
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.
Sourcepub fn price_levels_between(
&self,
side: Side,
lo: Price,
hi: Price,
) -> Vec<PriceLevel>
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.
Sourcepub fn tick_size(&self) -> Option<Decimal>
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.
Sourcepub fn bid_ask_ratio(&self) -> Option<Decimal>
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).
Sourcepub fn price_impact(&self, side: Side, qty: Quantity) -> Option<Decimal>
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).
Sourcepub fn bid_depth(&self, n: usize) -> Vec<PriceLevel>
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.
Sourcepub fn ask_depth(&self, n: usize) -> Vec<PriceLevel>
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.
Sourcepub fn depth_imbalance(&self) -> Option<Decimal>
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)
Nonewhen both sides are empty (total depth is zero)
Sourcepub fn ask_bid_ratio(&self) -> Option<Decimal>
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).
Sourcepub fn total_bid_depth(&self) -> Decimal
pub fn total_bid_depth(&self) -> Decimal
Returns the total quantity across all bid price levels.
Sourcepub fn total_ask_depth(&self) -> Decimal
pub fn total_ask_depth(&self) -> Decimal
Returns the total quantity across all ask price levels.
Sourcepub fn price_at_volume(&self, side: Side, target_qty: Decimal) -> Option<Price>
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.
Sourcepub fn top_n_bid_levels(&self, n: usize) -> Vec<PriceLevel>
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.
Sourcepub fn top_n_ask_levels(&self, n: usize) -> Vec<PriceLevel>
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.
Sourcepub fn cumulative_bid_qty(&self, n: usize) -> Decimal
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.
Sourcepub fn bid_depth_skew(&self, n: usize) -> Option<Decimal>
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.
Sourcepub fn spread_bps(&self) -> Option<Decimal>
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.
Sourcepub fn cumulative_ask_qty(&self, n: usize) -> Decimal
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.