kiteticker_async_manager/models/
depth.rs1use crate::Exchange;
2use serde::{Deserialize, Serialize};
3
4use crate::parser::{price, value, value_short};
5
6#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
7pub struct Depth {
11 pub buy: [DepthItem; 5],
12 pub sell: [DepthItem; 5],
13}
14
15impl Depth {
16 pub(crate) fn from(input: &[u8], exchange: &Exchange) -> Option<Self> {
17 if let Some(bs) = input.get(0..120) {
18 let parse_depth_item = |v: &[u8], start: usize| {
19 v.get(start..start + 10)
20 .and_then(|xs| DepthItem::from(xs, exchange))
21 .unwrap_or_default()
22 };
23 let mut depth = Depth::default();
24 for i in 0..5 {
25 let start = i * 12;
26 depth.buy[i] = parse_depth_item(bs, start)
27 }
28 for i in 0..5 {
29 let start = 60 + i * 12;
30 depth.sell[i] = parse_depth_item(bs, start);
31 }
32
33 Some(depth)
34 } else {
35 None
36 }
37 }
38}
39
40#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
41pub struct DepthItem {
45 pub qty: u32,
46 pub price: f64,
47 pub orders: u16,
48}
49
50impl DepthItem {
51 pub fn from(input: &[u8], exchange: &Exchange) -> Option<Self> {
52 input.get(0..10).map(|bs| DepthItem {
53 qty: value(&bs[0..=3]).unwrap(),
54 price: price(&bs[4..=7], exchange).unwrap(),
55 orders: value_short(&bs[8..=9]).unwrap(),
56 })
57 }
58}