nautilus_databento/
types.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::ffi::c_char;
17
18use databento::dbn;
19use nautilus_core::UnixNanos;
20use nautilus_model::{
21    enums::OrderSide,
22    identifiers::InstrumentId,
23    types::{Price, Quantity},
24};
25use serde::Deserialize;
26use ustr::Ustr;
27
28use super::enums::{DatabentoStatisticType, DatabentoStatisticUpdateAction};
29
30/// Represents a Databento publisher ID.
31pub type PublisherId = u16;
32
33/// Represents a Databento dataset code.
34pub type Dataset = Ustr;
35
36/// Represents a Databento publisher.
37#[cfg_attr(
38    feature = "python",
39    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.databento")
40)]
41#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
42pub struct DatabentoPublisher {
43    /// The publisher ID assigned by Databento, which denotes the dataset and venue.
44    pub publisher_id: PublisherId,
45    /// The Databento dataset code for the publisher.
46    pub dataset: dbn::Dataset,
47    /// The venue for the publisher.
48    pub venue: dbn::Venue,
49    /// The publisher description.
50    pub description: String,
51}
52
53/// Represents an auction imbalance.
54///
55/// This data type includes the populated data fields provided by `Databento`,
56/// excluding `publisher_id` and `instrument_id`.
57#[cfg_attr(
58    feature = "python",
59    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.databento")
60)]
61#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
62pub struct DatabentoImbalance {
63    // The instrument ID for the imbalance data.
64    pub instrument_id: InstrumentId,
65    // The reference price at which the imbalance shares are calculated.
66    pub ref_price: Price,
67    // The hypothetical auction-clearing price for both cross and continuous orders.
68    pub cont_book_clr_price: Price,
69    // The hypothetical auction-clearing price for cross orders only.
70    pub auct_interest_clr_price: Price,
71    // The quantity of shares which are eligible to be matched at `ref_price`.
72    pub paired_qty: Quantity,
73    // The quantity of shares which are not paired at `ref_price`.
74    pub total_imbalance_qty: Quantity,
75    // The market side of the `total_imbalance_qty` (can be `NO_ORDER_SIDE`).
76    pub side: OrderSide,
77    // A venue-specific character code. For Nasdaq, contains the raw Price Variation Indicator.
78    pub significant_imbalance: c_char,
79    // UNIX timestamp (nanoseconds) when the data event occurred.
80    pub ts_event: UnixNanos,
81    // UNIX timestamp (nanoseconds) when the data object was received by Databento.
82    pub ts_recv: UnixNanos,
83    // UNIX timestamp (nanoseconds) when the data object was initialized.
84    pub ts_init: UnixNanos,
85}
86
87impl DatabentoImbalance {
88    /// Creates a new [`DatabentoImbalance`] instance.
89    ///
90    /// # Errors
91    ///
92    /// This function never returns an error (TBD).
93    #[allow(clippy::too_many_arguments)]
94    pub const fn new(
95        instrument_id: InstrumentId,
96        ref_price: Price,
97        cont_book_clr_price: Price,
98        auct_interest_clr_price: Price,
99        paired_qty: Quantity,
100        total_imbalance_qty: Quantity,
101        side: OrderSide,
102        significant_imbalance: c_char,
103        ts_event: UnixNanos,
104        ts_recv: UnixNanos,
105        ts_init: UnixNanos,
106    ) -> anyhow::Result<Self> {
107        Ok(Self {
108            instrument_id,
109            ref_price,
110            cont_book_clr_price,
111            auct_interest_clr_price,
112            paired_qty,
113            total_imbalance_qty,
114            side,
115            significant_imbalance,
116            ts_event,
117            ts_recv,
118            ts_init,
119        })
120    }
121}
122
123/// Represents a market statistics snapshot.
124///
125/// This data type includes the populated data fields provided by `Databento`,
126/// excluding `publisher_id` and `instrument_id`.
127#[cfg_attr(
128    feature = "python",
129    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.databento")
130)]
131#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
132pub struct DatabentoStatistics {
133    // The instrument ID for the statistics message.
134    pub instrument_id: InstrumentId,
135    // The type of statistic value contained in the message.
136    pub stat_type: DatabentoStatisticType,
137    // Indicates if the statistic is newly added (1) or deleted (2). (Deleted is only used with some stat_types).
138    pub update_action: DatabentoStatisticUpdateAction,
139    // The statistics price.
140    pub price: Option<Price>,
141    // The value for non-price statistics.
142    pub quantity: Option<Quantity>,
143    // The channel ID within the venue.
144    pub channel_id: u16,
145    // Additional flags associated with certain stat types.
146    pub stat_flags: u8,
147    // The message sequence number assigned at the venue.
148    pub sequence: u32,
149    // UNIX timestamp (nanoseconds) Databento `ts_ref` reference timestamp).
150    pub ts_ref: UnixNanos,
151    // The matching-engine-sending timestamp expressed as the number of nanoseconds before the Databento `ts_recv`.
152    pub ts_in_delta: i32,
153    // UNIX timestamp (nanoseconds) when the data event occurred.
154    pub ts_event: UnixNanos,
155    // UNIX timestamp (nanoseconds) when the data object was received by Databento.
156    pub ts_recv: UnixNanos,
157    // UNIX timestamp (nanoseconds) when the data object was initialized.
158    pub ts_init: UnixNanos,
159}
160
161impl DatabentoStatistics {
162    /// Creates a new [`DatabentoStatistics`] instance.
163    ///
164    /// # Errors
165    ///
166    /// This function never returns an error (TBD).
167    #[allow(clippy::too_many_arguments)]
168    pub const fn new(
169        instrument_id: InstrumentId,
170        stat_type: DatabentoStatisticType,
171        update_action: DatabentoStatisticUpdateAction,
172        price: Option<Price>,
173        quantity: Option<Quantity>,
174        channel_id: u16,
175        stat_flags: u8,
176        sequence: u32,
177        ts_ref: UnixNanos,
178        ts_in_delta: i32,
179        ts_event: UnixNanos,
180        ts_recv: UnixNanos,
181        ts_init: UnixNanos,
182    ) -> anyhow::Result<Self> {
183        Ok(Self {
184            instrument_id,
185            stat_type,
186            update_action,
187            price,
188            quantity,
189            channel_id,
190            stat_flags,
191            sequence,
192            ts_ref,
193            ts_in_delta,
194            ts_event,
195            ts_recv,
196            ts_init,
197        })
198    }
199}