Skip to main content

digdigdig3_station/data/
block_trade.rs

1use digdigdig3::core::types::{StreamEvent, TradeSide};
2use serde::{Deserialize, Serialize};
3
4use crate::series::DataPoint;
5
6/// Block trade — large off-book print, identified by exchange-issued `block_id`.
7///
8/// Disk layout:
9/// - Header (44 B): `ts_ms (8) | price (8) | quantity (8) | side (1) | is_iv (1) | _pad (6) | blob_off (8) | blob_len (4)`.
10/// - Blob: `len(block_id):u16 | block_id_utf8`.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct BlockTradePoint {
13    pub ts_ms: i64,
14    pub block_id: String,
15    pub price: f64,
16    pub quantity: f64,
17    pub side: TradeSide,
18    pub is_iv: bool,
19}
20
21const TAIL_OFFSET: usize = 32;
22
23impl DataPoint for BlockTradePoint {
24    const RECORD_SIZE: usize = 44;
25
26    fn encode(&self, out: &mut [u8]) {
27        out[0..8].copy_from_slice(&(self.ts_ms as u64).to_le_bytes());
28        out[8..16].copy_from_slice(&self.price.to_le_bytes());
29        out[16..24].copy_from_slice(&self.quantity.to_le_bytes());
30        out[24] = match self.side {
31            TradeSide::Buy => 0,
32            TradeSide::Sell => 1,
33        };
34        out[25] = u8::from(self.is_iv);
35    }
36
37    fn decode(bytes: &[u8]) -> Option<Self> {
38        if bytes.len() != Self::RECORD_SIZE { return None; }
39        Some(Self {
40            ts_ms: u64::from_le_bytes(bytes[0..8].try_into().ok()?) as i64,
41            block_id: String::new(),
42            price: f64::from_le_bytes(bytes[8..16].try_into().ok()?),
43            quantity: f64::from_le_bytes(bytes[16..24].try_into().ok()?),
44            side: match bytes[24] { 0 => TradeSide::Buy, _ => TradeSide::Sell },
45            is_iv: bytes[25] != 0,
46        })
47    }
48
49    fn timestamp_ms(&self) -> i64 { self.ts_ms }
50
51    fn from_stream_event(ev: &StreamEvent) -> Option<Self> {
52        if let StreamEvent::BlockTrade { block, .. } = ev {
53            // BlockTrade.is_buy: true = buyer aggressor (Buy), false = seller aggressor (Sell).
54            let side = if block.is_buy { TradeSide::Buy } else { TradeSide::Sell };
55            Some(Self {
56                ts_ms: block.timestamp,
57                block_id: block.block_id.clone(),
58                price: block.price,
59                quantity: block.quantity,
60                side,
61                is_iv: block.is_iv,
62            })
63        } else {
64            None
65        }
66    }
67
68    fn encode_blob(&self) -> Option<Vec<u8>> {
69        let bytes = self.block_id.as_bytes();
70        let mut out = Vec::with_capacity(2 + bytes.len());
71        out.extend_from_slice(&(bytes.len() as u16).to_le_bytes());
72        out.extend_from_slice(bytes);
73        Some(out)
74    }
75
76    fn decode_blob(header: &[u8], blob: &[u8]) -> Option<Self> {
77        let mut p = Self::decode(header)?;
78        if blob.len() >= 2 {
79            let len = u16::from_le_bytes(blob[0..2].try_into().ok()?) as usize;
80            if blob.len() >= 2 + len {
81                p.block_id = String::from_utf8_lossy(&blob[2..2 + len]).into_owned();
82            }
83        }
84        Some(p)
85    }
86
87    fn blob_pointer_offset() -> Option<usize> { Some(TAIL_OFFSET) }
88}