1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Order book for the matchcore library
mod analytics;
mod book;
mod execution;
mod level;
mod logic;
mod market_data;
pub use analytics::*;
pub use book::*;
pub use level::*;
pub use market_data::*;
use crate::{Price, SequenceNumber, Timestamp};
/// Order book that manages all kinds of orders and levels
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct OrderBook<const PRICE_LEVELS_INITIAL_CAPACITY: usize = 2048> {
/// The symbol for this order book
symbol: String,
/// The last sequence number of the order book, `None` if no command has been processed yet.
/// This is used to ensure that the incoming commands are processed in the correct order.
pub(self) last_sequence_number: Option<SequenceNumber>,
/// The last seen timestamp of the order book, `None` if no command has been processed yet.
/// This is used to ensure that the timestamps of the incoming commands are non-decreasing.
pub(self) last_seen_timestamp: Option<Timestamp>,
/// The last price at which a trade occurred, `None` if no trade has occurred yet
pub(self) last_trade_price: Option<Price>,
/// Limit order book
pub(self) limit: LimitBook<PRICE_LEVELS_INITIAL_CAPACITY>,
/// Pegged order book
pub(self) pegged: PeggedBook,
/// Price-conditional order book
pub(self) price_conditional: PriceConditionalBook,
}
impl OrderBook {
/// Create a new order book
pub fn new(symbol: &str) -> Self {
Self {
symbol: symbol.to_string(),
last_sequence_number: None,
last_seen_timestamp: None,
last_trade_price: None,
limit: LimitBook::new(),
pegged: PeggedBook::new(),
price_conditional: PriceConditionalBook::new(),
}
}
/// Get the symbol for this order book
pub fn symbol(&self) -> &str {
&self.symbol
}
/// Get the last sequence number of the order book, `None` if no command has been processed yet.
pub fn last_sequence_number(&self) -> Option<SequenceNumber> {
self.last_sequence_number
}
/// Get the last seen timestamp of the order book, `None` if no command has been processed yet.
pub fn last_seen_timestamp(&self) -> Option<Timestamp> {
self.last_seen_timestamp
}
/// Get the last trade price, `None` if no trade has occurred yet
pub fn last_trade_price(&self) -> Option<Price> {
self.last_trade_price
}
/// Get the limit order book
pub fn limit(&self) -> &LimitBook {
&self.limit
}
/// Get the pegged order book
pub fn pegged(&self) -> &PeggedBook {
&self.pegged
}
/// Get the price-conditional order book
pub fn price_conditional(&self) -> &PriceConditionalBook {
&self.price_conditional
}
}