dbn 0.74.0

Library for working with Databento Binary Encoding (DBN)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use std::os::raw::c_char;

use crate::{
    pretty::px_to_f64,
    record::{c_chars_to_str, ts_to_dt},
    BboRec, BidAskPair, CbboRec, Cmbp1Rec, ConsolidatedBidAskPair, Error, ErrorRec, FlagSet,
    InstrumentClass, MatchAlgorithm, MboRec, Mbp10Rec, Mbp1Rec, OhlcvRec, Result, SType,
    SecurityUpdateAction, Side, StatRec, StatusRec, SystemRec, TradeRec,
};

use super::*;

impl MboRec for MboMsg {
    fn order_id(&self) -> u64 {
        self.order_id
    }

    fn price(&self) -> i64 {
        self.price
    }

    fn size(&self) -> u32 {
        self.size
    }

    fn flags(&self) -> FlagSet {
        self.flags
    }

    fn channel_id(&self) -> u8 {
        self.channel_id
    }

    fn raw_action(&self) -> c_char {
        self.action
    }

    fn raw_side(&self) -> c_char {
        self.side
    }

    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn raw_ts_in_delta(&self) -> i32 {
        self.ts_in_delta
    }

    fn sequence(&self) -> u32 {
        self.sequence
    }
}

impl TradeRec for TradeMsg {
    fn price(&self) -> i64 {
        self.price
    }

    fn size(&self) -> u32 {
        self.size
    }

    fn raw_action(&self) -> c_char {
        self.action
    }

    fn raw_side(&self) -> c_char {
        self.side
    }

    fn flags(&self) -> FlagSet {
        self.flags
    }

    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn raw_ts_in_delta(&self) -> i32 {
        self.ts_in_delta
    }

    fn sequence(&self) -> u32 {
        self.sequence
    }
}

impl Mbp1Rec for Mbp1Msg {
    fn price(&self) -> i64 {
        self.price
    }

    fn size(&self) -> u32 {
        self.size
    }

    fn raw_action(&self) -> c_char {
        self.action
    }

    fn raw_side(&self) -> c_char {
        self.side
    }

    fn flags(&self) -> FlagSet {
        self.flags
    }

    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn raw_ts_in_delta(&self) -> i32 {
        self.ts_in_delta
    }

    fn sequence(&self) -> u32 {
        self.sequence
    }

    fn levels(&self) -> &[BidAskPair; 1] {
        &self.levels
    }
}

impl Mbp10Rec for Mbp10Msg {
    fn price(&self) -> i64 {
        self.price
    }

    fn size(&self) -> u32 {
        self.size
    }

    fn raw_action(&self) -> c_char {
        self.action
    }

    fn raw_side(&self) -> c_char {
        self.side
    }

    fn flags(&self) -> FlagSet {
        self.flags
    }

    fn depth(&self) -> u8 {
        self.depth
    }

    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn raw_ts_in_delta(&self) -> i32 {
        self.ts_in_delta
    }

    fn sequence(&self) -> u32 {
        self.sequence
    }

    fn levels(&self) -> &[BidAskPair; 10] {
        &self.levels
    }
}

impl BboRec for BboMsg {
    fn price(&self) -> i64 {
        self.price
    }

    fn size(&self) -> u32 {
        self.size
    }

    fn raw_side(&self) -> c_char {
        self.side
    }

    fn flags(&self) -> FlagSet {
        self.flags
    }

    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn sequence(&self) -> u32 {
        self.sequence
    }

    fn levels(&self) -> &[BidAskPair; 1] {
        &self.levels
    }
}

impl Cmbp1Rec for Cmbp1Msg {
    fn price(&self) -> i64 {
        self.price
    }

    fn size(&self) -> u32 {
        self.size
    }

    fn raw_action(&self) -> c_char {
        self.action
    }

    fn raw_side(&self) -> c_char {
        self.side
    }

    fn flags(&self) -> FlagSet {
        self.flags
    }

    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn raw_ts_in_delta(&self) -> i32 {
        self.ts_in_delta
    }

    fn levels(&self) -> &[ConsolidatedBidAskPair; 1] {
        &self.levels
    }
}

impl CbboRec for CbboMsg {
    fn price(&self) -> i64 {
        self.price
    }

    fn size(&self) -> u32 {
        self.size
    }

    fn raw_side(&self) -> c_char {
        self.side
    }

    fn flags(&self) -> FlagSet {
        self.flags
    }

    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn levels(&self) -> &[ConsolidatedBidAskPair; 1] {
        &self.levels
    }
}

impl OhlcvRec for OhlcvMsg {
    fn open(&self) -> i64 {
        self.open
    }

    fn high(&self) -> i64 {
        self.high
    }

    fn low(&self) -> i64 {
        self.low
    }

    fn close(&self) -> i64 {
        self.close
    }

    fn volume(&self) -> u64 {
        self.volume
    }
}

impl StatusRec for StatusMsg {
    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn raw_action(&self) -> u16 {
        self.action
    }

    fn raw_reason(&self) -> u16 {
        self.reason
    }

    fn raw_trading_event(&self) -> u16 {
        self.trading_event
    }

    fn raw_is_trading(&self) -> c_char {
        self.is_trading
    }

    fn raw_is_quoting(&self) -> c_char {
        self.is_quoting
    }

    fn raw_is_short_sell_restricted(&self) -> c_char {
        self.is_short_sell_restricted
    }
}

impl InstrumentDefMsg {
    /// Parses the capture-server-received timestamp into a datetime.
    /// Returns `None` if `ts_recv` contains the sentinel for a null timestamp.
    pub fn ts_recv(&self) -> Option<time::OffsetDateTime> {
        ts_to_dt(self.ts_recv)
    }

    /// Converts the minimum constant tick to a floating point.
    ///
    /// `UNDEF_PRICE` will be converted to NaN.
    ///
    /// <div class="warning">
    /// This may introduce floating-point error.
    /// </div>
    pub fn min_price_increment_f64(&self) -> f64 {
        px_to_f64(self.min_price_increment)
    }

    /// Converts the display factor to a floating point.
    ///
    /// `UNDEF_PRICE` will be converted to NaN.
    ///
    /// <div class="warning">
    /// This may introduce floating-point error.
    /// </div>
    pub fn display_factor_f64(&self) -> f64 {
        px_to_f64(self.display_factor)
    }

    /// Parses the security update action into an enum.
    ///
    /// # Errors
    /// This function returns an error if the `security_update_action` field does not
    /// contain a valid [`SecurityUpdateAction`].
    #[allow(
        clippy::unnecessary_cast,
        reason = "c_char is u8 on some targets like aarch64 Linux"
    )]
    pub fn security_update_action(&self) -> crate::Result<SecurityUpdateAction> {
        SecurityUpdateAction::try_from(self.security_update_action as u8).map_err(|_| {
            Error::conversion::<SecurityUpdateAction>(format_args!(
                "{:#04X}",
                self.security_update_action as u8
            ))
        })
    }

    /// Parses the instrument class into an enum.
    ///
    /// # Errors
    /// This function returns an error if the `instrument_class` field does not
    /// contain a valid [`InstrumentClass`].
    #[allow(
        clippy::unnecessary_cast,
        reason = "c_char is u8 on some targets like aarch64 Linux"
    )]
    pub fn instrument_class(&self) -> crate::Result<InstrumentClass> {
        InstrumentClass::try_from(self.instrument_class as u8).map_err(|_| {
            Error::conversion::<InstrumentClass>(format_args!(
                "{:#04X}",
                self.instrument_class as u8
            ))
        })
    }

    /// Parses the match algorithm into an enum.
    ///
    /// # Errors
    /// This function returns an error if the `match_algorithm` field does not
    /// contain a valid [`MatchAlgorithm`].
    #[allow(
        clippy::unnecessary_cast,
        reason = "c_char is u8 on some targets like aarch64 Linux"
    )]
    pub fn match_algorithm(&self) -> crate::Result<MatchAlgorithm> {
        MatchAlgorithm::try_from(self.match_algorithm as u8).map_err(|_| {
            Error::conversion::<MatchAlgorithm>(format_args!("{:#04X}", self.match_algorithm as u8))
        })
    }
}

impl ImbalanceMsg {
    /// Parses the capture-server-received timestamp into a datetime.
    /// Returns `None` if `ts_recv` contains the sentinel for a null timestamp.
    pub fn ts_recv(&self) -> Option<time::OffsetDateTime> {
        ts_to_dt(self.ts_recv)
    }

    /// Converts the ref price to a floating point.
    ///
    /// `UNDEF_PRICE` will be converted to NaN.
    ///
    /// <div class="warning">
    /// This may introduce floating-point error.
    /// </div>
    pub fn ref_price_f64(&self) -> f64 {
        px_to_f64(self.ref_price)
    }

    /// Converts the cont book clr price to a floating point.
    ///
    /// `UNDEF_PRICE` will be converted to NaN.
    ///
    /// <div class="warning">
    /// This may introduce floating-point error.
    /// </div>
    pub fn cont_book_clr_price_f64(&self) -> f64 {
        px_to_f64(self.cont_book_clr_price)
    }

    /// Converts the auct interest clr price to a floating point.
    ///
    /// `UNDEF_PRICE` will be converted to NaN.
    ///
    /// <div class="warning">
    /// This may introduce floating-point error.
    /// </div>
    pub fn auct_interest_clr_price_f64(&self) -> f64 {
        px_to_f64(self.auct_interest_clr_price)
    }

    /// Parses the side into an enum.
    ///
    /// # Errors
    /// This function returns an error if the `side` field does not
    /// contain a valid [`Side`].
    #[allow(
        clippy::unnecessary_cast,
        reason = "c_char is u8 on some targets like aarch64 Linux"
    )]
    pub fn side(&self) -> crate::Result<Side> {
        Side::try_from(self.side as u8)
            .map_err(|_| Error::conversion::<Side>(format_args!("{:#04X}", self.side as u8)))
    }
}

impl StatRec for StatMsg {
    fn raw_ts_recv(&self) -> u64 {
        self.ts_recv
    }

    fn raw_ts_ref(&self) -> u64 {
        self.ts_ref
    }

    fn price(&self) -> i64 {
        self.price
    }

    fn quantity(&self) -> i64 {
        self.quantity
    }

    fn sequence(&self) -> u32 {
        self.sequence
    }

    fn raw_ts_in_delta(&self) -> i32 {
        self.ts_in_delta
    }

    fn raw_stat_type(&self) -> u16 {
        self.stat_type
    }

    fn channel_id(&self) -> u8 {
        self.channel_id
    }

    fn raw_update_action(&self) -> u8 {
        self.update_action
    }

    fn stat_flags(&self) -> u8 {
        self.stat_flags
    }
}

impl ErrorRec for ErrorMsg {
    fn subscription_id(&self) -> u32 {
        self.subscription_id
    }

    fn raw_schema(&self) -> u16 {
        self.schema
    }

    fn raw_code(&self) -> u8 {
        self.code
    }

    fn is_last(&self) -> u8 {
        self.is_last
    }

    fn err(&self) -> Result<&str> {
        c_chars_to_str(&self.err)
    }
}

impl SymbolMappingMsg {
    /// Parses the start of the mapping interval into a datetime.
    /// Returns `None` if `start_ts` contains the sentinel for a null timestamp.
    pub fn start_ts(&self) -> Option<time::OffsetDateTime> {
        ts_to_dt(self.start_ts)
    }

    /// Parses the end of the mapping interval into a datetime.
    /// Returns `None` if `end_ts` contains the sentinel for a null timestamp.
    pub fn end_ts(&self) -> Option<time::OffsetDateTime> {
        ts_to_dt(self.end_ts)
    }

    /// Parses the stype in into an enum.
    ///
    /// # Errors
    /// This function returns an error if the `stype_in` field does not
    /// contain a valid [`SType`].
    pub fn stype_in(&self) -> crate::Result<SType> {
        SType::try_from(self.stype_in)
            .map_err(|_| Error::conversion::<SType>(format_args!("{:#04X}", self.stype_in)))
    }

    /// Parses the stype out into an enum.
    ///
    /// # Errors
    /// This function returns an error if the `stype_out` field does not
    /// contain a valid [`SType`].
    pub fn stype_out(&self) -> crate::Result<SType> {
        SType::try_from(self.stype_out)
            .map_err(|_| Error::conversion::<SType>(format_args!("{:#04X}", self.stype_out)))
    }
}

impl SystemRec for SystemMsg {
    fn subscription_id(&self) -> u32 {
        self.subscription_id
    }

    fn raw_schema(&self) -> u16 {
        self.schema
    }

    fn raw_code(&self) -> u8 {
        self.code
    }

    fn msg(&self) -> Result<&str> {
        c_chars_to_str(&self.msg)
    }
}