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
use std::ops::{Add, Div};
pub trait TopOfBook {
type Price;
type Volume;
fn bid_price(&self) -> Option<Self::Price>;
fn bid_volume(&self) -> Option<Self::Volume>;
fn ask_price(&self) -> Option<Self::Price>;
fn ask_volume(&self) -> Option<Self::Volume>;
fn mid_price(&self) -> Option<<<Self::Price as Add>::Output as Div<Self::Price>>::Output> where Self::Price: Add + From<u8>, <Self::Price as Add>::Output: Div<Self::Price> {
if let Some(ask_price) = self.ask_price() {
self.bid_price().map(|bid_price| (ask_price + bid_price) / Self::Price::from(2u8))
} else {
None
}
}
}
pub trait LastPrice {
type Price;
fn last_price(&self) -> Option<Self::Price>;
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use rust_decimal::Decimal;
use super::TopOfBook;
pub struct TopOfBookData {
bid: Decimal,
ask: Decimal,
}
impl TopOfBook for TopOfBookData {
type Price = Decimal;
type Volume = Decimal;
fn bid_price(&self) -> Option<Self::Price> {
Some(self.bid)
}
fn bid_volume(&self) -> Option<Self::Volume> {
todo!()
}
fn ask_price(&self) -> Option<Self::Price> {
Some(self.ask)
}
fn ask_volume(&self) -> Option<Self::Volume> {
todo!()
}
}
#[test]
fn test_mid_price() {
let data = TopOfBookData {
bid: 100u8.into(),
ask: 99u8.into(),
};
assert_eq!(data.mid_price(), Decimal::from_str("99.5").ok());
}
}