Skip to main content

OrderBook

Struct OrderBook 

Source
pub struct OrderBook {
    pub symbol: String,
    pub asks: Vec<Ask>,
    pub bids: Vec<Bid>,
    pub timestamp: u64,
    pub update_id: u64,
    pub sequence: u64,
    pub matching_engine_timestamp: u64,
}
Expand description

Represents the order book for a trading pair.

Contains the current bid and ask levels, along with metadata like the update ID. Bots use this to analyze market depth and liquidity in perpetual futures.

Fields§

§symbol: String

The trading pair symbol (e.g., “BTCUSDT”).

Confirms the trading pair for the order book. Bots should verify this matches the requested symbol.

§asks: Vec<Ask>

A list of ask (sell) orders.

Contains the current ask prices and quantities. Bots use this to assess selling pressure and determine resistance levels in perpetual futures.

§bids: Vec<Bid>

A list of bid (buy) orders.

Contains the current bid prices and quantities. Bots use this to assess buying support and determine support levels in perpetual futures.

§timestamp: u64

The timestamp of the order book snapshot (Unix timestamp in milliseconds).

Indicates when the order book data was captured. Bots should use this to ensure the data is recent, as stale order book data can lead to poor trading decisions.

§update_id: u64

The update ID of the order book.

A unique identifier for the order book snapshot. Bots can use this to track updates and ensure they’re processing the latest data, especially in WebSocket streams.

§sequence: u64

Cross sequence.

You can use this field to compare different levels orderbook data, and for the smaller seq, then it means the data is generated earlier.

§matching_engine_timestamp: u64

The timestamp from the matching engine when this orderbook data is produced. It can be correlated with T from public trade channel.

Implementations§

Source§

impl OrderBook

Source

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

Returns the best ask price (lowest ask).

Source

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

Returns the best bid price (highest bid).

Source

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

Returns the bid-ask spread.

Source

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

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

Source

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

Returns the spread as a percentage of mid price.

Source

pub fn total_ask_quantity(&self) -> f64

Returns the total quantity on the ask side.

Source

pub fn total_bid_quantity(&self) -> f64

Returns the total quantity on the bid side.

Source

pub fn total_quantity(&self) -> f64

Returns the total quantity (bids + asks).

Source

pub fn bid_ask_quantity_ratio(&self) -> f64

Returns the bid-ask quantity ratio.

Source

pub fn order_book_imbalance(&self) -> f64

Returns the order book imbalance (bid - ask) / (bid + ask).

Source

pub fn weighted_average_ask_price(&self, target_quantity: f64) -> Option<f64>

Returns the weighted average price for a given quantity on the ask side.

Source

pub fn weighted_average_bid_price(&self, target_quantity: f64) -> Option<f64>

Returns the weighted average price for a given quantity on the bid side.

Source

pub fn ask_price_impact(&self, quantity: f64) -> Option<f64>

Returns the price impact for a given quantity on the ask side.

Source

pub fn bid_price_impact(&self, quantity: f64) -> Option<f64>

Returns the price impact for a given quantity on the bid side.

Source

pub fn cumulative_ask_quantity_to_price(&self, price: f64) -> f64

Returns the cumulative quantity up to a given price level on the ask side.

Source

pub fn cumulative_bid_quantity_to_price(&self, price: f64) -> f64

Returns the cumulative quantity up to a given price level on the bid side.

Source

pub fn ask_price_for_cumulative_quantity( &self, target_quantity: f64, ) -> Option<f64>

Returns the price level that contains a given cumulative quantity on the ask side.

Source

pub fn bid_price_for_cumulative_quantity( &self, target_quantity: f64, ) -> Option<f64>

Returns the price level that contains a given cumulative quantity on the bid side.

Source

pub fn market_depth(&self, percentage_range: f64) -> (f64, f64)

Returns the market depth (quantity within a percentage range of mid price).

Source

pub fn liquidity_score(&self) -> f64

Returns the liquidity score (higher is more liquid).

Source

pub fn timestamp_datetime(&self) -> DateTime<Utc>

Returns the timestamp as a DateTime.

Source

pub fn matching_engine_timestamp_datetime(&self) -> DateTime<Utc>

Returns the matching engine timestamp as a DateTime.

Source

pub fn processing_latency_ms(&self) -> i64

Returns the latency between system generation and matching engine.

Source

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

Returns the VWAP (Volume Weighted Average Price) for the visible order book.

Source

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

Returns the microprice (weighted by inverse of spread).

Source

pub fn order_book_slope( &self, side: OrderBookSide, levels: usize, ) -> Option<f64>

Returns the order book slope (price change per unit quantity).

Source

pub fn order_book_curvature( &self, side: OrderBookSide, levels: usize, ) -> Option<f64>

Returns the order book curvature (second derivative).

Source

pub fn order_book_resilience(&self) -> f64

Returns the order book resilience (how quickly liquidity replenishes).

Source

pub fn order_book_toxicity(&self) -> f64

Returns the order book toxicity (probability of informed trading).

Source

pub fn effective_cost(&self, quantity: f64) -> Option<f64>

Returns the effective cost of trading (spread + impact for a given quantity).

Source

pub fn round_trip_cost(&self, quantity: f64) -> Option<f64>

Returns the market impact cost for a round trip trade.

Source

pub fn optimal_order_size(&self, max_impact: f64) -> Option<f64>

Returns the optimal order size based on market impact.

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
Source§

impl<'de> Deserialize<'de> for OrderBook

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 OrderBook

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>,