roshar-types 0.1.20

Type definitions for cryptocurrency exchange websocket messages
Documentation
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use compact_str::CompactString;
use ordered_float::OrderedFloat;
use std::cmp::Reverse;
use std::collections::BTreeMap;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum LocalOrderBookError {
    #[error("Bid Above Ask of {0}/{1} for {2}/{3}")]
    BidAboveAsk(String, String, String, String),
    #[error("Attempted to update {0}/{1} before initial snapshot")]
    BookUpdateBeforeSnapshot(String, String),
    #[error("Unparseable input for order book update for {0}/{1}")]
    UnparseableInputs(String, String),
    #[error("Out of order updates for {0}/{1} expecting {2} but have {3}")]
    OutOfOrderUpdate(String, String, i64, i64),
    #[error("Wrong symbol: expected {0} but received message for {1}")]
    WrongSymbol(String, String),
    #[error("Message is not a partial update for {0}/{1}")]
    NotPartialUpdate(String, String),
    #[error("Invalid order book for {0}/{1}: bids={2}, asks={3}")]
    InvalidOrderBook(String, String, usize, usize),
}

/// Type aliases for order book storage
pub type BidMap = BTreeMap<Reverse<OrderedFloat<f64>>, CompactString>;
pub type AskMap = BTreeMap<OrderedFloat<f64>, CompactString>;

/// Owns the order book data and handles mutation.
/// Exchange-specific order books should own this and mutate it directly.
#[derive(Debug, Clone)]
pub struct OrderBookState {
    bids: BidMap,
    asks: AskMap,
    max_depth: usize,
}

impl OrderBookState {
    pub fn new(max_depth: usize) -> Self {
        Self {
            bids: BTreeMap::new(),
            asks: BTreeMap::new(),
            max_depth,
        }
    }

    /// Set or update a bid level. If size is zero or empty, removes the level.
    /// Returns error if size cannot be parsed as f64.
    /// NOTE: Does NOT trim. Caller must call `trim()` after processing a batch of updates.
    #[inline]
    pub fn set_bid(&mut self, price: f64, size: &str) -> Result<(), LocalOrderBookError> {
        let key = Reverse(OrderedFloat(price));
        if size.is_empty() {
            self.bids.remove(&key);
            return Ok(());
        }
        let size_f64 = size.parse::<f64>().map_err(|_| {
            LocalOrderBookError::UnparseableInputs(
                format!("bid size: {}", size),
                format!("price: {}", price),
            )
        })?;
        if size_f64 == 0.0 {
            self.bids.remove(&key);
        } else {
            self.bids.insert(key, size.into());
        }
        Ok(())
    }

    /// Set or update an ask level. If size is zero or empty, removes the level.
    /// Returns error if size cannot be parsed as f64.
    /// NOTE: Does NOT trim. Caller must call `trim()` after processing a batch of updates.
    #[inline]
    pub fn set_ask(&mut self, price: f64, size: &str) -> Result<(), LocalOrderBookError> {
        let key = OrderedFloat(price);
        if size.is_empty() {
            self.asks.remove(&key);
            return Ok(());
        }
        let size_f64 = size.parse::<f64>().map_err(|_| {
            LocalOrderBookError::UnparseableInputs(
                format!("ask size: {}", size),
                format!("price: {}", price),
            )
        })?;
        if size_f64 == 0.0 {
            self.asks.remove(&key);
        } else {
            self.asks.insert(key, size.into());
        }
        Ok(())
    }

    /// Remove a bid level by price
    #[inline]
    pub fn remove_bid(&mut self, price: f64) {
        self.bids.remove(&Reverse(OrderedFloat(price)));
    }

    /// Remove an ask level by price
    #[inline]
    pub fn remove_ask(&mut self, price: f64) {
        self.asks.remove(&OrderedFloat(price));
    }

    /// Clear all levels
    pub fn clear(&mut self) {
        self.bids.clear();
        self.asks.clear();
    }

    /// Get a read-only view for calculations
    pub fn as_view(&self) -> LocalOrderBook<'_> {
        LocalOrderBook::new(&self.bids, &self.asks)
    }

    /// Get BBO directly without creating a view
    pub fn get_bbo(&self) -> (Option<f64>, Option<f64>) {
        let bid = self.bids.keys().next().map(|k| k.0 .0);
        let ask = self.asks.keys().next().map(|k| k.0);
        (bid, ask)
    }

    /// Get BBO as strings (for compatibility)
    pub fn get_bbo_strings(&self) -> (String, String) {
        let bid = self
            .bids
            .keys()
            .next()
            .map_or(String::new(), |k| k.0 .0.to_string());
        let ask = self
            .asks
            .keys()
            .next()
            .map_or(String::new(), |k| k.0.to_string());
        (bid, ask)
    }

    /// Trim both sides to max_depth. Call this after processing a batch of updates.
    pub fn trim(&mut self) {
        self.trim_bids();
        self.trim_asks();
    }

    /// Trim bids to max_depth
    pub fn trim_bids(&mut self) {
        if self.bids.len() > self.max_depth {
            let keys_to_remove: Vec<_> = self.bids.keys().skip(self.max_depth).cloned().collect();
            for key in keys_to_remove {
                self.bids.remove(&key);
            }
        }
    }

    /// Trim asks to max_depth
    pub fn trim_asks(&mut self) {
        if self.asks.len() > self.max_depth {
            let keys_to_remove: Vec<_> = self.asks.keys().skip(self.max_depth).cloned().collect();
            for key in keys_to_remove {
                self.asks.remove(&key);
            }
        }
    }

    /// Direct access to bids for advanced use cases
    pub fn bids(&self) -> &BidMap {
        &self.bids
    }

    /// Direct access to asks for advanced use cases
    pub fn asks(&self) -> &AskMap {
        &self.asks
    }

    /// Check if the book is empty
    pub fn is_empty(&self) -> bool {
        self.bids.is_empty() && self.asks.is_empty()
    }
}

/// Read-only view over order book data for calculations.
/// Borrows the underlying BTreeMaps - no copying.
#[derive(Debug)]
pub struct LocalOrderBook<'a> {
    bids: &'a BidMap,
    asks: &'a AskMap,
}

impl<'a> LocalOrderBook<'a> {
    pub fn new(bids: &'a BidMap, asks: &'a AskMap) -> Self {
        Self { bids, asks }
    }

    pub fn get_bbo(&self) -> (String, String) {
        let bid = self
            .bids
            .keys()
            .next()
            .map_or(String::new(), |k| k.0 .0.to_string());
        let ask = self
            .asks
            .keys()
            .next()
            .map_or(String::new(), |k| k.0.to_string());
        (bid, ask)
    }

    pub fn get_bbo_f64(&self) -> (Option<f64>, Option<f64>) {
        let bid = self.bids.keys().next().map(|k| k.0 .0);
        let ask = self.asks.keys().next().map(|k| k.0);
        (bid, ask)
    }

    pub fn calc_top_n_exp_weight(&self, n: usize, exp: f64) -> f64 {
        let mut b_sz = 0.0;
        let mut a_sz = 0.0;

        for (i, (_, size)) in self.bids.iter().take(n.min(20)).enumerate() {
            let weight = (-exp * i as f64).exp();
            b_sz += weight * size.parse::<f64>().unwrap_or(0.0);
        }

        for (i, (_, size)) in self.asks.iter().take(n.min(20)).enumerate() {
            let weight = (-exp * i as f64).exp();
            a_sz += weight * size.parse::<f64>().unwrap_or(0.0);
        }

        if b_sz + a_sz > 0.0 {
            (b_sz - a_sz) / (b_sz + a_sz)
        } else {
            0.0
        }
    }

    pub fn bid_prices(&self) -> Vec<String> {
        self.bids.keys().map(|k| k.0 .0.to_string()).collect()
    }

    pub fn ask_prices(&self) -> Vec<String> {
        self.asks.keys().map(|k| k.0.to_string()).collect()
    }

    pub fn bid_sizes(&self) -> Vec<String> {
        self.bids.values().map(|v| v.to_string()).collect()
    }

    pub fn ask_sizes(&self) -> Vec<String> {
        self.asks.values().map(|v| v.to_string()).collect()
    }

    /// Convert the view to a DepthSnapshotData for database storage.
    /// This copies the data since DepthSnapshotData needs owned Vecs.
    pub fn to_depth_snapshot_data(
        &self,
        venue: crate::Venue,
        ticker: String,
    ) -> crate::DepthSnapshotData {
        let now = chrono::Utc::now();
        crate::DepthSnapshotData {
            bid_prices: self.bid_prices(),
            bid_sizes: self.bid_sizes(),
            ask_prices: self.ask_prices(),
            ask_sizes: self.ask_sizes(),
            time: now.timestamp_millis() as u64,
            time_ts: now,
            ticker,
            venue,
        }
    }

    /// Convert the view to a DepthSnapshotData with a specific timestamp.
    /// Use this when the message contains a timestamp that should be preserved.
    pub fn to_depth_snapshot_data_with_time(
        &self,
        venue: crate::Venue,
        ticker: String,
        time_ms: u64,
    ) -> crate::DepthSnapshotData {
        use chrono::{TimeZone, Utc};
        let time_ts = Utc
            .timestamp_millis_opt(time_ms as i64)
            .single()
            .unwrap_or_else(Utc::now);
        crate::DepthSnapshotData {
            bid_prices: self.bid_prices(),
            bid_sizes: self.bid_sizes(),
            ask_prices: self.ask_prices(),
            ask_sizes: self.ask_sizes(),
            time: time_ms,
            time_ts,
            ticker,
            venue,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_order_book_state_set_bid() {
        let mut state = OrderBookState::new(50);
        state.set_bid(100.0, "1.5").unwrap();
        state.set_bid(101.0, "2.0").unwrap();
        state.set_bid(99.0, "3.0").unwrap();

        let view = state.as_view();
        let prices = view.bid_prices();

        // Bids should be sorted descending (highest first)
        assert_eq!(prices[0], "101");
        assert_eq!(prices[1], "100");
        assert_eq!(prices[2], "99");
    }

    #[test]
    fn test_order_book_state_set_ask() {
        let mut state = OrderBookState::new(50);
        state.set_ask(103.0, "1.5").unwrap();
        state.set_ask(101.0, "2.0").unwrap();
        state.set_ask(102.0, "3.0").unwrap();

        let view = state.as_view();
        let prices = view.ask_prices();

        // Asks should be sorted ascending (lowest first)
        assert_eq!(prices[0], "101");
        assert_eq!(prices[1], "102");
        assert_eq!(prices[2], "103");
    }

    #[test]
    fn test_order_book_state_remove_on_zero() {
        let mut state = OrderBookState::new(50);
        state.set_bid(100.0, "1.5").unwrap();
        state.set_bid(100.0, "0").unwrap();

        assert!(state.bids().is_empty());
    }

    #[test]
    fn test_order_book_state_max_depth() {
        let mut state = OrderBookState::new(2);
        state.set_bid(100.0, "1.0").unwrap();
        state.set_bid(101.0, "2.0").unwrap();
        state.set_bid(102.0, "3.0").unwrap();
        state.set_bid(99.0, "4.0").unwrap();

        // Trim after batch of updates
        state.trim();

        // Should only keep top 2 bids (102, 101)
        assert_eq!(state.bids().len(), 2);
        let view = state.as_view();
        let prices = view.bid_prices();
        assert_eq!(prices[0], "102");
        assert_eq!(prices[1], "101");
    }

    #[test]
    fn test_order_book_state_get_bbo() {
        let mut state = OrderBookState::new(50);
        state.set_bid(100.0, "1.0").unwrap();
        state.set_bid(101.0, "2.0").unwrap();
        state.set_ask(102.0, "1.5").unwrap();
        state.set_ask(103.0, "2.5").unwrap();

        let (bid, ask) = state.get_bbo();
        assert_eq!(bid, Some(101.0));
        assert_eq!(ask, Some(102.0));
    }

    #[test]
    fn test_local_order_book_view_get_bbo() {
        let mut state = OrderBookState::new(50);
        state.set_bid(100.0, "1.0").unwrap();
        state.set_ask(101.0, "1.5").unwrap();

        let view = state.as_view();
        let (bid, ask) = view.get_bbo();
        assert_eq!(bid, "100");
        assert_eq!(ask, "101");
    }

    #[test]
    fn test_order_book_state_clear() {
        let mut state = OrderBookState::new(50);
        state.set_bid(100.0, "1.0").unwrap();
        state.set_ask(101.0, "1.5").unwrap();

        state.clear();

        assert!(state.is_empty());
    }

    #[test]
    fn test_order_book_state_eth_prices() {
        let mut state = OrderBookState::new(50);

        // Test ETH-like prices
        state.set_bid(3127.05, "65.905").unwrap();
        state.set_bid(3127.04, "0.320").unwrap();
        state.set_bid(3126.99, "1.773").unwrap();

        state.set_ask(3127.14, "0.320").unwrap();
        state.set_ask(3127.17, "1.773").unwrap();

        let view = state.as_view();
        let bid_prices = view.bid_prices();
        let ask_prices = view.ask_prices();

        println!("bid_prices: {:?}", bid_prices);
        println!("ask_prices: {:?}", ask_prices);

        assert_eq!(bid_prices.len(), 3);
        assert_eq!(ask_prices.len(), 2);

        // Check prices are not empty
        assert!(!bid_prices[0].is_empty());
        assert!(!ask_prices[0].is_empty());
    }
}