Skip to main content

OrderBookTracker

Struct OrderBookTracker 

Source
pub struct OrderBookTracker {
    pub symbol: String,
    /* private fields */
}
Expand description

Maintains a live orderbook from snapshot + delta stream.

§Ordering

  • bids: ascending DecimalKey → iterate in reverse for descending price order
  • asks: ascending DecimalKey → iterate forward for ascending price order

§Sequence gap detection

When a delta carries prev_update_id and the book has a known last_update_id, they must match. On mismatch OrderBookError::SequenceGap is returned and the book is left unchanged — the consumer should request a fresh snapshot.

Fields§

§symbol: String

Symbol this tracker was created for.

Implementations§

Source§

impl OrderBookTracker

Source

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

Create a new empty tracker for the given symbol.

Source

pub fn apply_snapshot( &mut self, snapshot: &OrderBook, ) -> Result<(), OrderBookError>

Replace the full book state with the given snapshot.

The snapshot.symbol is not checked (snapshots from OrderBook REST calls do not carry a symbol field — the caller is responsible for routing the right snapshot to the right tracker). Pass symbol explicitly via the second argument to verify.

Clears all previous bids/asks, then populates from the snapshot levels.

Source

pub fn apply_delta( &mut self, delta: &OrderbookDelta, ) -> Result<(), OrderBookError>

Apply an incremental delta to the live book.

Rules:

  • Returns OrderBookError::NoSnapshot if no snapshot has been applied.
  • Checks prev_update_id against last_update_id when both are present.
  • A level with size 0.0 removes that price level; otherwise upserts.
Source

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

Top n bid levels, highest price first.

Source

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

Top n ask levels, lowest price first.

Source

pub fn bbo(&self) -> Option<(Decimal, Decimal)>

Best bid/ask pair (best_bid, best_ask). None if either side is empty.

Source

pub fn mid(&self) -> Option<Decimal>

Mid price: (best_bid + best_ask) / 2. None if book is empty.

Source

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

Spread: best_ask - best_bid. None if book is empty.

Source

pub fn total_bid_volume(&self) -> Decimal

Sum of all bid quantities.

Source

pub fn total_ask_volume(&self) -> Decimal

Sum of all ask quantities.

Source

pub fn depth(&self) -> (usize, usize)

(bid_levels, ask_levels) — number of distinct price levels per side.

Source

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

last_update_id from the most recent snapshot or delta.

Source

pub fn last_timestamp_ms(&self) -> i64

Unix millisecond timestamp of the most recent update.

Source

pub fn has_snapshot(&self) -> bool

Whether a snapshot has been applied.

Source

pub fn reset(&mut self)

Reset the tracker to an empty state (keeps symbol).

Trait Implementations§

Source§

impl Clone for OrderBookTracker

Source§

fn clone(&self) -> OrderBookTracker

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OrderBookTracker

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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> 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