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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use rust_decimal::prelude::*;
use std::collections::BTreeMap;
use std::error::Error;
use std::{cmp, fmt};
type L2Map = BTreeMap<Decimal, Decimal>;
pub enum Side {
Bid,
Ask,
}
#[derive(Clone, Debug)]
pub struct L2 {
orders: L2Map,
desc: bool,
}
#[derive(Debug)]
pub struct InconsistentBook {
pub details: String,
pub asset: String,
}
#[derive(Debug, Clone)]
pub struct Book {
pub asset: String,
pub bids: L2,
pub asks: L2,
}
pub fn bid_ask_spread(bid: Option<Decimal>, ask: Option<Decimal>) -> Option<Decimal> {
match bid {
Some(b) => ask.map(|a| a - b),
None => None,
}
}
pub struct L2Iterator<'a> {
iter: std::collections::btree_map::Iter<'a, Decimal, Decimal>,
desc: bool,
}
impl L2 {
fn new(desc: bool) -> L2 {
L2 {
orders: BTreeMap::new(),
desc,
}
}
pub fn len(&self) -> usize {
self.orders.len()
}
pub fn is_empty(&self) -> bool {
self.orders.is_empty()
}
pub fn set(&mut self, price: Decimal, volume: Decimal) {
match volume.is_zero() {
true => self.orders.remove(&price),
false => self.orders.insert(price, volume),
};
}
pub fn set_str(&mut self, price: &str, volume: &str) {
self.set(
Decimal::from_str(price).unwrap(),
Decimal::from_str(volume).unwrap(),
)
}
pub fn update(&mut self, price_volume: &[(String, String)]) {
for (price, volume) in price_volume.iter() {
self.set_str(price, volume);
}
}
pub fn best(&self) -> Option<(&Decimal, &Decimal)> {
if self.desc {
self.orders.iter().next_back()
} else {
self.orders.iter().next()
}
}
pub fn best_price(&self) -> Option<Decimal> {
self.best().map(|(price, _)| *price)
}
pub fn best_of(&self, price: Option<Decimal>) -> Option<Decimal> {
match self.best_price() {
Some(best) => match price {
Some(other_price) => match self.desc {
true => Some(cmp::max(best, other_price)),
false => Some(cmp::min(best, other_price)),
},
None => Some(best),
},
None => price,
}
}
pub fn iter(&self) -> L2Iterator {
L2Iterator {
iter: self.orders.iter(),
desc: self.desc,
}
}
fn trim(&self, max_depth: usize) -> Self {
let mut orders = L2Map::new();
for (i, (price, volume)) in self.iter().enumerate() {
if i >= max_depth {
break;
}
orders.insert(*price, *volume);
}
Self {
orders,
desc: self.desc,
}
}
}
impl<'a> Iterator for L2Iterator<'a> {
type Item = (&'a Decimal, &'a Decimal);
fn next(&mut self) -> Option<Self::Item> {
match self.desc {
false => self.iter.next(),
true => self.iter.next_back(),
}
}
}
impl InconsistentBook {
pub fn new(msg: &str, asset: &str) -> Self {
InconsistentBook {
details: msg.to_owned(),
asset: asset.to_owned(),
}
}
}
impl fmt::Display for InconsistentBook {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.details)
}
}
impl Error for InconsistentBook {
fn description(&self) -> &str {
&self.details
}
}
impl Book {
pub fn new(asset: &str) -> Self {
Self {
asset: asset.to_owned(),
bids: L2::new(true),
asks: L2::new(false),
}
}
pub fn is_consistent(&self) -> bool {
let bid = match self.bids.best() {
None => return true,
Some((price, _)) => price,
};
let ask = match self.asks.best() {
None => return true,
Some((price, _)) => price,
};
bid < ask
}
pub fn spread(&self) -> Option<Decimal> {
bid_ask_spread(self.bids.best_price(), self.asks.best_price())
}
pub fn trim(&self, max_depth: usize) -> Self {
Self {
asset: self.asset.to_owned(),
asks: self.asks.trim(max_depth),
bids: self.bids.trim(max_depth),
}
}
}