Skip to main content

LocalOrderbook

Struct LocalOrderbook 

Source
pub struct LocalOrderbook { /* private fields */ }
Expand description

Local orderbook maintained from WebSocket updates.

This structure maintains a sorted orderbook from snapshot and delta updates. Bids are sorted by price descending (highest first), asks ascending (lowest first).

Implementations§

Source§

impl LocalOrderbook

Source

pub fn new(symbol: impl Into<String>) -> Self

Create a new empty orderbook for a symbol.

Source

pub fn symbol(&self) -> &str

Get the symbol this orderbook is for.

Source

pub fn is_initialized(&self) -> bool

Check if the orderbook has been initialized with a snapshot.

Source

pub fn update_id(&self) -> u64

Get the current update ID.

Source

pub fn sequence(&self) -> Option<u64>

Get the sequence number.

Source

pub fn last_update_ts(&self) -> u64

Get the last update timestamp.

Source

pub fn apply_update( &mut self, update: &WsStreamMessage<OrderbookData>, ) -> Result<(), BybitError>

Apply an orderbook update (snapshot or delta).

§Update Rules
  • Snapshots replace the entire orderbook.
  • Deltas are applied incrementally:
    • If size is “0”, the price level is removed.
    • If the price level does not exist, it is inserted.
    • If the price level exists, the size is updated.
  • If update_id is 1, treat it as a snapshot.
Source

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

Get the best bid (highest buy price).

Source

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

Get the best ask (lowest sell price).

Source

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

Get the bid-ask spread.

Source

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

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

Source

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

Get the top N bids (sorted by price descending).

Source

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

Get the top N asks (sorted by price ascending).

Source

pub fn bids(&self) -> impl Iterator<Item = &PriceLevel>

Get all bids (sorted by price descending).

Source

pub fn asks(&self) -> impl Iterator<Item = &PriceLevel>

Get all asks (sorted by price ascending).

Source

pub fn bid_depth(&self) -> f64

Get the total bid depth (sum of all bid sizes).

Source

pub fn ask_depth(&self) -> f64

Get the total ask depth (sum of all ask sizes).

Source

pub fn bid_levels(&self) -> usize

Get the number of bid levels.

Source

pub fn ask_levels(&self) -> usize

Get the number of ask levels.

Source

pub fn clear(&mut self)

Clear the orderbook and reset to uninitialized state.

Source

pub fn imbalance(&self) -> f64

Get orderbook imbalance ratio.

Returns a value between -1 and 1.

  • Positive values indicate more bid depth (buying pressure).
  • Negative values indicate more ask depth (selling pressure).
  • Zero indicates balanced depth.

Trait Implementations§

Source§

impl Debug for LocalOrderbook

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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

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