Skip to main content

WsOrderBook

Struct WsOrderBook 

Source
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: String

The 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: u64

The 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: u64

The 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

Source

pub fn new( symbol: &str, asks: Vec<Ask>, bids: Vec<Bid>, update_id: u64, seq: u64, ) -> Self

Creates a new WsOrderBook instance.

Source

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

Returns the best ask price (lowest ask).

Source

pub fn best_ask_quantity(&self) -> Option<f64>

Returns the best ask quantity.

Source

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

Returns the best bid price (highest bid).

Source

pub fn best_bid_quantity(&self) -> Option<f64>

Returns the best bid quantity.

Source

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

Returns the mid price (average of best bid and best ask).

Source

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

Returns the bid-ask spread.

Source

pub fn spread_percentage(&self) -> Option<f64>

Returns the spread as a percentage of the mid price.

Source

pub fn total_ask_quantity(&self) -> f64

Returns the total quantity available at the ask side.

Source

pub fn total_bid_quantity(&self) -> f64

Returns the total quantity available at the bid side.

Source

pub fn total_ask_value(&self) -> f64

Returns the total value available at the ask side.

Source

pub fn total_bid_value(&self) -> f64

Returns the total value available at the bid side.

Source

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

Returns the order book imbalance. Positive values indicate more buying pressure, negative values indicate more selling pressure.

Source

pub fn ask_vwap(&self) -> Option<f64>

Returns the volume-weighted average price (VWAP) for asks.

Source

pub fn bid_vwap(&self) -> Option<f64>

Returns the volume-weighted average price (VWAP) for bids.

Source

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.

Source

pub fn cumulative_depth(&self, price_limit: f64, side: OrderBookSide) -> f64

Returns the cumulative order book quantities up to a given price level.

Source

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.

Source

pub fn liquidity_in_range(&self, min_price: f64, max_price: f64) -> (f64, f64)

Returns the order book liquidity in a given price range.

Source

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.

Source

pub fn depth_profile(&self, num_buckets: usize) -> DepthProfile

Returns the market depth profile. Groups order book levels into price buckets for analysis.

Source

pub fn price_range(&self) -> (f64, f64)

Returns the order book’s price range.

Source

pub fn weighted_spread(&self) -> Option<f64>

Returns the order book’s quantity-weighted average spread.

Source

pub fn resilience(&self) -> f64

Returns the order book’s resilience. Measures how quickly the order book recovers after a trade.

Source

pub fn toxicity(&self) -> f64

Returns the order book’s toxicity. Measures the likelihood of adverse selection.

Source

pub fn is_valid(&self) -> bool

Returns true if the order book is valid for trading.

Source

pub fn to_summary_string(&self) -> String

Returns a summary string for this order book.

Source

pub fn to_json(&self) -> String

Returns the order book snapshot as a JSON string.

Source

pub fn merge(&mut self, update: &WsOrderBook) -> bool

Merges this order book with another order book update. Used for incremental updates.

Source

pub fn age(&self, current_seq: u64) -> u64

Returns the order book age based on sequence numbers.

Source

pub fn is_stale(&self, current_seq: u64, max_age: u64) -> bool

Returns true if the order book is stale.

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn momentum_indicator(&self) -> f64

Returns the order book’s momentum indicator. Positive values indicate buying pressure, negative values indicate selling pressure.

Source

pub fn volatility_estimate(&self) -> Option<f64>

Returns the order book’s volatility estimate. Based on the density of orders around the mid price.

Source

pub fn efficiency_metric(&self) -> Option<f64>

Returns the order book’s efficiency metric. Measures how well the order book facilitates trading.

Source

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.

Source

pub fn arbitrage_opportunity(&self) -> Option<f64>

Returns the order book’s arbitrage opportunity. Difference between fair value and mid price as percentage.

Source

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.

Source

pub fn snapshot(&self) -> OrderBookSnapshot

Returns the order book’s snapshot for persistence.

Source

pub fn analysis_report(&self) -> String

Returns a comprehensive analysis report.

Trait Implementations§

Source§

impl Clone for WsOrderBook

Source§

fn clone(&self) -> WsOrderBook

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 WsOrderBook

Source§

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

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

impl<'de> Deserialize<'de> for WsOrderBook

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for WsOrderBook

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,