kiteticker_async_manager/models/
ohlc.rs

1use crate::Exchange;
2use serde::{Deserialize, Serialize};
3
4use crate::parser::price;
5
6#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
7///
8/// OHLC packet structure
9///
10pub struct OHLC {
11  pub open: f64,
12  pub high: f64,
13  pub low: f64,
14  pub close: f64,
15}
16
17impl OHLC {
18  pub(crate) fn from(value: &[u8], exchange: &Exchange) -> Option<Self> {
19    value.get(0..16).map(|bs| OHLC {
20      open: price(&bs[0..=3], exchange).unwrap(),
21      high: price(&bs[4..=7], exchange).unwrap(),
22      low: price(&bs[8..=11], exchange).unwrap(),
23      close: price(&bs[12..=15], exchange).unwrap(),
24    })
25  }
26
27  /// Parse OHLC bytes for index instruments.
28  ///
29  /// The order of fields for indices is `high`, `low`, `open`, `close`.
30  pub(crate) fn from_index(value: &[u8], exchange: &Exchange) -> Option<Self> {
31    value.get(0..16).map(|bs| OHLC {
32      open: price(&bs[8..=11], exchange).unwrap(),
33      high: price(&bs[0..=3], exchange).unwrap(),
34      low: price(&bs[4..=7], exchange).unwrap(),
35      close: price(&bs[12..=15], exchange).unwrap(),
36    })
37  }
38}