pub struct OrderBookTracker {
pub symbol: String,
/* private fields */
}Expand description
Maintains a live orderbook from snapshot + delta stream.
§Ordering
bids: ascendingDecimalKey→ iterate in reverse for descending price orderasks: ascendingDecimalKey→ 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: StringSymbol this tracker was created for.
Implementations§
Source§impl OrderBookTracker
impl OrderBookTracker
Sourcepub fn apply_snapshot(
&mut self,
snapshot: &OrderBook,
) -> Result<(), OrderBookError>
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.
Sourcepub fn apply_delta(
&mut self,
delta: &OrderbookDelta,
) -> Result<(), OrderBookError>
pub fn apply_delta( &mut self, delta: &OrderbookDelta, ) -> Result<(), OrderBookError>
Apply an incremental delta to the live book.
Rules:
- Returns
OrderBookError::NoSnapshotif no snapshot has been applied. - Checks
prev_update_idagainstlast_update_idwhen both are present. - A level with size
0.0removes that price level; otherwise upserts.
Sourcepub fn top_bids(&self, n: usize) -> Vec<(Decimal, Decimal)>
pub fn top_bids(&self, n: usize) -> Vec<(Decimal, Decimal)>
Top n bid levels, highest price first.
Sourcepub fn top_asks(&self, n: usize) -> Vec<(Decimal, Decimal)>
pub fn top_asks(&self, n: usize) -> Vec<(Decimal, Decimal)>
Top n ask levels, lowest price first.
Sourcepub fn bbo(&self) -> Option<(Decimal, Decimal)>
pub fn bbo(&self) -> Option<(Decimal, Decimal)>
Best bid/ask pair (best_bid, best_ask). None if either side is empty.
Sourcepub fn mid(&self) -> Option<Decimal>
pub fn mid(&self) -> Option<Decimal>
Mid price: (best_bid + best_ask) / 2. None if book is empty.
Sourcepub fn total_bid_volume(&self) -> Decimal
pub fn total_bid_volume(&self) -> Decimal
Sum of all bid quantities.
Sourcepub fn total_ask_volume(&self) -> Decimal
pub fn total_ask_volume(&self) -> Decimal
Sum of all ask quantities.
Sourcepub fn depth(&self) -> (usize, usize)
pub fn depth(&self) -> (usize, usize)
(bid_levels, ask_levels) — number of distinct price levels per side.
Sourcepub fn last_update_id(&self) -> Option<u64>
pub fn last_update_id(&self) -> Option<u64>
last_update_id from the most recent snapshot or delta.
Sourcepub fn last_timestamp_ms(&self) -> i64
pub fn last_timestamp_ms(&self) -> i64
Unix millisecond timestamp of the most recent update.
Sourcepub fn has_snapshot(&self) -> bool
pub fn has_snapshot(&self) -> bool
Whether a snapshot has been applied.
Trait Implementations§
Source§impl Clone for OrderBookTracker
impl Clone for OrderBookTracker
Source§fn clone(&self) -> OrderBookTracker
fn clone(&self) -> OrderBookTracker
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more