Skip to main content

nautilus_deribit/
data_types.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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
16//! Deribit-specific custom data types.
17//!
18//! These types carry Deribit domain data through the Nautilus data engine as
19//! [`CustomData`](nautilus_model::data::CustomData).
20
21use nautilus_core::UnixNanos;
22use nautilus_persistence_macros::custom_data;
23
24/// Deribit volatility index (DVOL) update.
25///
26/// Emitted from the `deribit_volatility_index.{index_name}` WebSocket channel.
27#[cfg_attr(feature = "arrow", custom_data(pyo3))]
28#[cfg_attr(not(feature = "arrow"), custom_data(pyo3, no_arrow))]
29pub struct DeribitVolatilityIndex {
30    /// The index identifier (for example `"btc_usd"` or `"eth_usd"`).
31    pub index_name: String,
32    /// The DVOL value for `index_name`.
33    pub volatility: f64,
34    /// UNIX timestamp (nanoseconds) when the data event occurred.
35    pub ts_event: UnixNanos,
36    /// UNIX timestamp (nanoseconds) when the instance was initialized.
37    pub ts_init: UnixNanos,
38}
39
40/// Registers Deribit custom data types.
41///
42/// Safe to call multiple times (idempotent via internal `Once` guards).
43pub fn register_deribit_custom_data() {
44    #[cfg(feature = "arrow")]
45    nautilus_serialization::ensure_custom_data_registered::<DeribitVolatilityIndex>();
46
47    #[cfg(not(feature = "arrow"))]
48    let _ = nautilus_model::data::ensure_custom_data_json_registered::<DeribitVolatilityIndex>();
49}
50
51#[cfg(test)]
52mod tests {
53    use rstest::rstest;
54
55    use super::*;
56
57    #[rstest]
58    fn test_register_deribit_custom_data_is_idempotent() {
59        register_deribit_custom_data();
60        register_deribit_custom_data();
61    }
62
63    #[cfg(feature = "arrow")]
64    #[rstest]
65    fn test_deribit_volatility_index_arrow_schema() {
66        use arrow::datatypes::DataType;
67        use nautilus_serialization::arrow::ArrowSchemaProvider;
68
69        let schema = DeribitVolatilityIndex::get_schema(None);
70
71        assert_eq!(schema.fields().len(), 4);
72        assert_eq!(schema.field(0).name(), "index_name");
73        assert_eq!(schema.field(0).data_type(), &DataType::Utf8);
74        assert_eq!(schema.field(1).name(), "volatility");
75        assert_eq!(schema.field(1).data_type(), &DataType::Float64);
76        assert_eq!(schema.field(2).name(), "ts_event");
77        assert_eq!(schema.field(2).data_type(), &DataType::UInt64);
78        assert_eq!(schema.field(3).name(), "ts_init");
79        assert_eq!(schema.field(3).data_type(), &DataType::UInt64);
80    }
81}