pub struct WsOrderBook {
pub symbol: String,
pub asks: Vec<Ask>,
pub bids: Vec<Bid>,
pub update_id: u64,
pub seq: u64,
}Expand description
Structure for WebSocket order book data.
Contains the bids, asks, and sequence numbers for a trading pair’s order book. Bots use this to maintain an up-to-date view of market depth and liquidity.
Fields§
§symbol: StringThe trading pair symbol (e.g., “BTCUSDT”).
Identifies the perpetual futures contract for the order book. Bots use this to verify the correct market.
asks: Vec<Ask>A list of ask prices and quantities.
Contains the current ask levels in the order book, sorted by price. Bots use this to assess selling pressure and liquidity on the ask side.
bids: Vec<Bid>A list of bid prices and quantities.
Contains the current bid levels in the order book, sorted by price. Bots use this to assess buying pressure and liquidity on the bid side.
update_id: u64The update ID for the order book.
A unique identifier for the order book update. Bots use this to ensure updates are processed in the correct order.
seq: u64The sequence number for the update.
A monotonically increasing number for ordering updates. Bots use this to detect missing or out-of-order updates and maintain order book consistency.
Implementations§
Source§impl WsOrderBook
impl WsOrderBook
Sourcepub fn new(
symbol: &str,
asks: Vec<Ask>,
bids: Vec<Bid>,
update_id: u64,
seq: u64,
) -> Self
pub fn new( symbol: &str, asks: Vec<Ask>, bids: Vec<Bid>, update_id: u64, seq: u64, ) -> Self
Creates a new WsOrderBook instance.
Sourcepub fn best_ask_quantity(&self) -> Option<f64>
pub fn best_ask_quantity(&self) -> Option<f64>
Returns the best ask quantity.
Sourcepub fn best_bid_quantity(&self) -> Option<f64>
pub fn best_bid_quantity(&self) -> Option<f64>
Returns the best bid quantity.
Sourcepub fn mid_price(&self) -> Option<f64>
pub fn mid_price(&self) -> Option<f64>
Returns the mid price (average of best bid and best ask).
Sourcepub fn spread_percentage(&self) -> Option<f64>
pub fn spread_percentage(&self) -> Option<f64>
Returns the spread as a percentage of the mid price.
Sourcepub fn total_ask_quantity(&self) -> f64
pub fn total_ask_quantity(&self) -> f64
Returns the total quantity available at the ask side.
Sourcepub fn total_bid_quantity(&self) -> f64
pub fn total_bid_quantity(&self) -> f64
Returns the total quantity available at the bid side.
Sourcepub fn total_ask_value(&self) -> f64
pub fn total_ask_value(&self) -> f64
Returns the total value available at the ask side.
Sourcepub fn total_bid_value(&self) -> f64
pub fn total_bid_value(&self) -> f64
Returns the total value available at the bid side.
Sourcepub fn imbalance(&self) -> Option<f64>
pub fn imbalance(&self) -> Option<f64>
Returns the order book imbalance. Positive values indicate more buying pressure, negative values indicate more selling pressure.
Sourcepub fn ask_vwap(&self) -> Option<f64>
pub fn ask_vwap(&self) -> Option<f64>
Returns the volume-weighted average price (VWAP) for asks.
Sourcepub fn bid_vwap(&self) -> Option<f64>
pub fn bid_vwap(&self) -> Option<f64>
Returns the volume-weighted average price (VWAP) for bids.
Sourcepub fn depth_at_price(&self, price: f64, tolerance: f64) -> (f64, f64)
pub fn depth_at_price(&self, price: f64, tolerance: f64) -> (f64, f64)
Returns the market depth at a given price level. Returns (bid_quantity, ask_quantity) at the specified price level.
Sourcepub fn cumulative_depth(&self, price_limit: f64, side: OrderBookSide) -> f64
pub fn cumulative_depth(&self, price_limit: f64, side: OrderBookSide) -> f64
Returns the cumulative order book quantities up to a given price level.
Sourcepub fn price_levels_in_range(
&self,
percentage_range: f64,
) -> (Vec<&Ask>, Vec<&Bid>)
pub fn price_levels_in_range( &self, percentage_range: f64, ) -> (Vec<&Ask>, Vec<&Bid>)
Returns the price levels within a given percentage range from the mid price.
Sourcepub fn liquidity_in_range(&self, min_price: f64, max_price: f64) -> (f64, f64)
pub fn liquidity_in_range(&self, min_price: f64, max_price: f64) -> (f64, f64)
Returns the order book liquidity in a given price range.
Sourcepub fn price_impact(&self, trade_size: f64, side: TradeSide) -> Option<f64>
pub fn price_impact(&self, trade_size: f64, side: TradeSide) -> Option<f64>
Returns the price impact for a given trade size. Estimates how much the price would move if a trade of the given size were executed.
Sourcepub fn depth_profile(&self, num_buckets: usize) -> DepthProfile
pub fn depth_profile(&self, num_buckets: usize) -> DepthProfile
Returns the market depth profile. Groups order book levels into price buckets for analysis.
Sourcepub fn price_range(&self) -> (f64, f64)
pub fn price_range(&self) -> (f64, f64)
Returns the order book’s price range.
Sourcepub fn weighted_spread(&self) -> Option<f64>
pub fn weighted_spread(&self) -> Option<f64>
Returns the order book’s quantity-weighted average spread.
Sourcepub fn resilience(&self) -> f64
pub fn resilience(&self) -> f64
Returns the order book’s resilience. Measures how quickly the order book recovers after a trade.
Sourcepub fn toxicity(&self) -> f64
pub fn toxicity(&self) -> f64
Returns the order book’s toxicity. Measures the likelihood of adverse selection.
Sourcepub fn to_summary_string(&self) -> String
pub fn to_summary_string(&self) -> String
Returns a summary string for this order book.
Sourcepub fn merge(&mut self, update: &WsOrderBook) -> bool
pub fn merge(&mut self, update: &WsOrderBook) -> bool
Merges this order book with another order book update. Used for incremental updates.
Sourcepub fn age(&self, current_seq: u64) -> u64
pub fn age(&self, current_seq: u64) -> u64
Returns the order book age based on sequence numbers.
Sourcepub fn is_stale(&self, current_seq: u64, max_age: u64) -> bool
pub fn is_stale(&self, current_seq: u64, max_age: u64) -> bool
Returns true if the order book is stale.
Sourcepub fn market_quality_score(&self) -> f64
pub fn market_quality_score(&self) -> f64
Returns the order book’s market quality score. Higher scores indicate better market quality (more liquidity, tighter spreads).
Sourcepub fn estimated_transaction_cost(&self, trade_size: f64) -> Option<f64>
pub fn estimated_transaction_cost(&self, trade_size: f64) -> Option<f64>
Returns the order book’s estimated transaction cost. This includes both the spread and potential price impact.
Sourcepub fn optimal_trade_size(&self, max_price_impact: f64) -> Option<f64>
pub fn optimal_trade_size(&self, max_price_impact: f64) -> Option<f64>
Returns the optimal trade size based on market conditions. Considers price impact and available liquidity.
Sourcepub fn support_resistance_levels(
&self,
threshold_multiplier: f64,
) -> (Vec<f64>, Vec<f64>)
pub fn support_resistance_levels( &self, threshold_multiplier: f64, ) -> (Vec<f64>, Vec<f64>)
Returns the order book’s support and resistance levels. Based on significant accumulation of orders.
Sourcepub fn momentum_indicator(&self) -> f64
pub fn momentum_indicator(&self) -> f64
Returns the order book’s momentum indicator. Positive values indicate buying pressure, negative values indicate selling pressure.
Sourcepub fn volatility_estimate(&self) -> Option<f64>
pub fn volatility_estimate(&self) -> Option<f64>
Returns the order book’s volatility estimate. Based on the density of orders around the mid price.
Sourcepub fn efficiency_metric(&self) -> Option<f64>
pub fn efficiency_metric(&self) -> Option<f64>
Returns the order book’s efficiency metric. Measures how well the order book facilitates trading.
Sourcepub fn fair_value_estimate(&self) -> Option<f64>
pub fn fair_value_estimate(&self) -> Option<f64>
Returns the order book’s fair value estimate. Based on volume-weighted average prices and order book imbalance.
Sourcepub fn arbitrage_opportunity(&self) -> Option<f64>
pub fn arbitrage_opportunity(&self) -> Option<f64>
Returns the order book’s arbitrage opportunity. Difference between fair value and mid price as percentage.
Sourcepub fn market_impact_profile(
&self,
max_trade_size: f64,
steps: usize,
) -> Vec<(f64, f64)>
pub fn market_impact_profile( &self, max_trade_size: f64, steps: usize, ) -> Vec<(f64, f64)>
Returns the order book’s market impact profile. Shows how price impact changes with trade size.
Sourcepub fn snapshot(&self) -> OrderBookSnapshot
pub fn snapshot(&self) -> OrderBookSnapshot
Returns the order book’s snapshot for persistence.
Sourcepub fn analysis_report(&self) -> String
pub fn analysis_report(&self) -> String
Returns a comprehensive analysis report.
Trait Implementations§
Source§impl Clone for WsOrderBook
impl Clone for WsOrderBook
Source§fn clone(&self) -> WsOrderBook
fn clone(&self) -> WsOrderBook
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more