rusty_model/data/
best_bid_ask.rs1use crate::instruments::InstrumentId;
7use rust_decimal::Decimal;
8
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14pub struct BestBidAsk {
15 pub instrument_id: InstrumentId,
17
18 pub bid_price: Decimal,
20
21 pub bid_size: Decimal,
23
24 pub ask_price: Decimal,
26
27 pub ask_size: Decimal,
29
30 pub exchange_timestamp_ns: u64,
32
33 pub local_timestamp_ns: u64,
35}
36
37impl BestBidAsk {
38 #[must_use]
40 pub const fn new(
41 instrument_id: InstrumentId,
42 bid_price: Decimal,
43 bid_size: Decimal,
44 ask_price: Decimal,
45 ask_size: Decimal,
46 exchange_timestamp_ns: u64,
47 local_timestamp_ns: u64,
48 ) -> Self {
49 Self {
50 instrument_id,
51 bid_price,
52 bid_size,
53 ask_price,
54 ask_size,
55 exchange_timestamp_ns,
56 local_timestamp_ns,
57 }
58 }
59
60 #[inline]
62 #[must_use]
63 pub fn spread(&self) -> Decimal {
64 self.ask_price - self.bid_price
65 }
66
67 #[inline]
69 #[must_use]
70 pub fn mid_price(&self) -> Decimal {
71 (self.ask_price + self.bid_price) / Decimal::TWO
72 }
73
74 #[inline]
76 #[must_use]
77 pub fn spread_percentage(&self) -> Option<Decimal> {
78 let mid = self.mid_price();
79 if mid.is_zero() {
80 None
81 } else {
82 Some(self.spread() / mid * Decimal::from(100))
83 }
84 }
85}