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 #[allow(clippy::too_many_arguments)]
90 #[must_use]
91 pub const fn new(
92 instrument_id: InstrumentId,
93 ref_price: Price,
94 cont_book_clr_price: Price,
95 auct_interest_clr_price: Price,
96 paired_qty: Quantity,
97 total_imbalance_qty: Quantity,
98 side: OrderSide,
99 significant_imbalance: c_char,
100 ts_event: UnixNanos,
101 ts_recv: UnixNanos,
102 ts_init: UnixNanos,
103 ) -> Self {
104 Self {
105 instrument_id,
106 ref_price,
107 cont_book_clr_price,
108 auct_interest_clr_price,
109 paired_qty,
110 total_imbalance_qty,
111 side,
112 significant_imbalance,
113 ts_event,
114 ts_recv,
115 ts_init,
116 }
117 }
118}
119
120/// Represents a market statistics snapshot.
121///
122/// This data type includes the populated data fields provided by `Databento`,
123/// excluding `publisher_id` and `instrument_id`.
124#[cfg_attr(
125 feature = "python",
126 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.databento")
127)]
128#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
129pub struct DatabentoStatistics {
130 // The instrument ID for the statistics message.
131 pub instrument_id: InstrumentId,
132 // The type of statistic value contained in the message.
133 pub stat_type: DatabentoStatisticType,
134 // Indicates if the statistic is newly added (1) or deleted (2). (Deleted is only used with some stat_types).
135 pub update_action: DatabentoStatisticUpdateAction,
136 // The statistics price.
137 pub price: Option<Price>,
138 // The value for non-price statistics.
139 pub quantity: Option<Quantity>,
140 // The channel ID within the venue.
141 pub channel_id: u16,
142 // Additional flags associated with certain stat types.
143 pub stat_flags: u8,
144 // The message sequence number assigned at the venue.
145 pub sequence: u32,
146 // UNIX timestamp (nanoseconds) Databento `ts_ref` reference timestamp).
147 pub ts_ref: UnixNanos,
148 // The matching-engine-sending timestamp expressed as the number of nanoseconds before the Databento `ts_recv`.
149 pub ts_in_delta: i32,
150 // UNIX timestamp (nanoseconds) when the data event occurred.
151 pub ts_event: UnixNanos,
152 // UNIX timestamp (nanoseconds) when the data object was received by Databento.
153 pub ts_recv: UnixNanos,
154 // UNIX timestamp (nanoseconds) when the data object was initialized.
155 pub ts_init: UnixNanos,
156}
157
158impl DatabentoStatistics {
159 /// Creates a new [`DatabentoStatistics`] instance.
160 #[allow(clippy::too_many_arguments)]
161 #[must_use]
162 pub const fn new(
163 instrument_id: InstrumentId,
164 stat_type: DatabentoStatisticType,
165 update_action: DatabentoStatisticUpdateAction,
166 price: Option<Price>,
167 quantity: Option<Quantity>,
168 channel_id: u16,
169 stat_flags: u8,
170 sequence: u32,
171 ts_ref: UnixNanos,
172 ts_in_delta: i32,
173 ts_event: UnixNanos,
174 ts_recv: UnixNanos,
175 ts_init: UnixNanos,
176 ) -> Self {
177 Self {
178 instrument_id,
179 stat_type,
180 update_action,
181 price,
182 quantity,
183 channel_id,
184 stat_flags,
185 sequence,
186 ts_ref,
187 ts_in_delta,
188 ts_event,
189 ts_recv,
190 ts_init,
191 }
192 }
193}