digdigdig3_station/data/
liquidation.rs1use digdigdig3::core::types::{StreamEvent, TradeSide};
2use serde::{Deserialize, Serialize};
3
4use crate::series::DataPoint;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct LiquidationPoint {
10 pub ts_ms: i64,
11 pub price: f64,
12 pub quantity: f64,
13 pub value: f64,
14 pub side: u8,
15}
16
17const SIZE: usize = 33;
18
19impl DataPoint for LiquidationPoint {
20 const RECORD_SIZE: usize = SIZE;
21
22 fn encode(&self, out: &mut [u8]) {
23 out[0..8].copy_from_slice(&(self.ts_ms as u64).to_le_bytes());
24 out[8..16].copy_from_slice(&self.price.to_le_bytes());
25 out[16..24].copy_from_slice(&self.quantity.to_le_bytes());
26 out[24..32].copy_from_slice(&self.value.to_le_bytes());
27 out[32] = self.side;
28 }
29
30 fn decode(bytes: &[u8]) -> Option<Self> {
31 if bytes.len() != SIZE { return None; }
32 Some(Self {
33 ts_ms: u64::from_le_bytes(bytes[0..8].try_into().ok()?) as i64,
34 price: f64::from_le_bytes(bytes[8..16].try_into().ok()?),
35 quantity: f64::from_le_bytes(bytes[16..24].try_into().ok()?),
36 value: f64::from_le_bytes(bytes[24..32].try_into().ok()?),
37 side: bytes[32],
38 })
39 }
40
41 fn timestamp_ms(&self) -> i64 { self.ts_ms }
42
43 fn from_stream_event(ev: &StreamEvent) -> Option<Self> {
44 if let StreamEvent::Liquidation { price, quantity, value, side, timestamp, .. } = ev {
45 let s = match side {
46 TradeSide::Buy => 0,
47 TradeSide::Sell => 1,
48 };
49 Some(Self {
50 ts_ms: *timestamp,
51 price: *price,
52 quantity: *quantity,
53 value: value.unwrap_or(f64::NAN),
54 side: s,
55 })
56 } else {
57 None
58 }
59 }
60}