pub struct OrderBook { /* private fields */ }Expand description
Normalised Barter OrderBook snapshot.
§Equality
PartialEq/Eq is derived over all fields, including the OrderBookTimes
timestamps. Because time_received is stamped per construction, two
content-identical books observed at different instants compare unequal. For
content comparison use the accessors (sequence()/bids()/asks()).
Implementations§
Source§impl OrderBook
impl OrderBook
Sourcepub fn new<IterBids, IterAsks, L>(
sequence: u64,
times: OrderBookTimes,
bids: IterBids,
asks: IterAsks,
) -> Self
pub fn new<IterBids, IterAsks, L>( sequence: u64, times: OrderBookTimes, bids: IterBids, asks: IterAsks, ) -> Self
Construct a new sorted OrderBook.
Note that the passed bid and asks levels do not need to be pre-sorted.
Sourcepub fn from_sides(
sequence: u64,
times: OrderBookTimes,
bids: OrderBookSide<Bids>,
asks: OrderBookSide<Asks>,
) -> Self
pub fn from_sides( sequence: u64, times: OrderBookTimes, bids: OrderBookSide<Bids>, asks: OrderBookSide<Asks>, ) -> Self
Construct an OrderBook from pre-sorted OrderBookSides.
Use this when you already have sorted sides to avoid re-sorting overhead. Caller must ensure sides are correctly sorted (bids descending, asks ascending).
Sourcepub fn time_engine(&self) -> Option<DateTime<Utc>>
pub fn time_engine(&self) -> Option<DateTime<Utc>>
Matching-engine time associated with the OrderBook ("T" on Binance
futures).
None when the venue doesn’t supply matching-engine time (Binance spot,
IBKR, …). This is for latency / engine audit — not a liveness signal;
use OrderBook::time_exchange/OrderBook::time_received for staleness.
Sourcepub fn time_exchange(&self) -> Option<DateTime<Utc>>
pub fn time_exchange(&self) -> Option<DateTime<Utc>>
Venue event/broadcast time associated with the OrderBook ("E" on
Binance, ts on Bybit).
Feed-lag-aware liveness where present: now - time_exchange catches data
that is old despite being just received. None when the venue supplies no
broadcast timestamp (IBKR; Binance spot REST seed before the first diff).
None is a capability signal, not a defect — fall back to
OrderBook::time_received.
Note the asymmetry with MarketEvent::time_exchange (non-Option, with a
local fallback): here None means “the venue gave nothing”.
Sourcepub fn time_received(&self) -> DateTime<Utc>
pub fn time_received(&self) -> DateTime<Utc>
Local ingestion wall-clock associated with the OrderBook.
The universal liveness floor — always present once a revision is
applied, on every venue (including IBKR, where it is the only liveness
signal). Skew-immune: now - time_received is a same-clock comparison.
Prefer it as the fallback when OrderBook::time_exchange is None.
A default/pre-population book (OrderBook::default) reports the epoch
(1970) here, so it reads as stale until the first revision — the intended
liveness-floor behaviour.
Sourcepub fn times(&self) -> OrderBookTimes
pub fn times(&self) -> OrderBookTimes
All revision timestamps for this OrderBook as a single
OrderBookTimes.
Convenience over the individual time_engine/time_exchange/
time_received accessors for forwarding the whole set in one (Copy) move
— e.g. reconstructing a book that shares this revision’s times.
Sourcepub fn snapshot(&self, depth: usize) -> Self
pub fn snapshot(&self, depth: usize) -> Self
Generate a sorted OrderBook snapshot with a maximum depth.
The returned snapshot carries the same OrderBookTimes as the source
book (timestamps are not reset).
Sourcepub fn update(&mut self, event: &OrderBookEvent)
pub fn update(&mut self, event: &OrderBookEvent)
Update the local OrderBook from a new OrderBookEvent.
Update advances the book’s OrderBookTimes wholesale (alongside
upserting the changed levels); Snapshot replaces the book in its
entirety, including its times.
Sourcepub fn bids(&self) -> &OrderBookSide<Bids>
pub fn bids(&self) -> &OrderBookSide<Bids>
Return a reference to this OrderBooks bids.
Sourcepub fn asks(&self) -> &OrderBookSide<Asks>
pub fn asks(&self) -> &OrderBookSide<Asks>
Return a reference to this OrderBooks asks.
Sourcepub fn mid_price(&self) -> Option<Decimal>
pub fn mid_price(&self) -> Option<Decimal>
Calculate the mid-price by taking the average of the best bid and ask prices.
See Docs: https://www.quantstart.com/articles/high-frequency-trading-ii-limit-order-book
Sourcepub fn volume_weighed_mid_price(&self) -> Option<Decimal>
pub fn volume_weighed_mid_price(&self) -> Option<Decimal>
Calculate the volume weighted mid-price (micro-price), weighing the best bid and ask prices with their associated amount.
See Docs: https://www.quantstart.com/articles/high-frequency-trading-ii-limit-order-book
Trait Implementations§
Source§impl<'de> Deserialize<'de> for OrderBook
impl<'de> Deserialize<'de> for OrderBook
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for OrderBook
impl StructuralPartialEq for OrderBook
Auto Trait Implementations§
impl Freeze for OrderBook
impl RefUnwindSafe for OrderBook
impl Send for OrderBook
impl Sync for OrderBook
impl Unpin for OrderBook
impl UnsafeUnpin for OrderBook
impl UnwindSafe for OrderBook
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more