Skip to main content

nautilus_databento/python/
data.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//! Python bindings for the Databento data client.
17
18use std::path::PathBuf;
19
20use nautilus_common::clients::DataClient;
21use nautilus_core::{
22    python::{to_pyruntime_err, to_pyvalue_err},
23    time::get_atomic_clock_realtime,
24};
25use nautilus_model::identifiers::{ClientId, futures::FuturesYearDigits};
26use pyo3::prelude::*;
27
28use crate::data::{DatabentoDataClient, DatabentoDataClientConfig};
29
30#[cfg(feature = "python")]
31#[pymethods]
32#[pyo3_stub_gen::derive::gen_stub_pymethods]
33impl DatabentoDataClient {
34    /// A Databento data client that combines live streaming and historical data functionality.
35    ///
36    /// This client uses the existing `DatabentoFeedHandler` for live data subscriptions
37    /// and `DatabentoHistoricalClient` for historical data requests. It supports multiple
38    /// datasets simultaneously, with separate feed handlers per dataset.
39    #[new]
40    #[pyo3(signature = (client_id, api_key, publishers_filepath, use_exchange_as_venue = true, bars_timestamp_on_close = true, futures_year_digits=None, share_class_space=true))]
41    pub fn py_new(
42        client_id: ClientId,
43        api_key: String,
44        publishers_filepath: PathBuf,
45        use_exchange_as_venue: bool,
46        bars_timestamp_on_close: bool,
47        futures_year_digits: Option<u8>,
48        share_class_space: bool,
49    ) -> PyResult<Self> {
50        let futures_year_digits = futures_year_digits
51            .map(FuturesYearDigits::try_from)
52            .transpose()
53            .map_err(to_pyvalue_err)?
54            .unwrap_or_default();
55
56        let mut config = DatabentoDataClientConfig::new(
57            api_key,
58            publishers_filepath,
59            use_exchange_as_venue,
60            bars_timestamp_on_close,
61        );
62
63        config.futures_year_digits = futures_year_digits;
64        config.share_class_space = share_class_space;
65        Self::new(client_id, config, get_atomic_clock_realtime()).map_err(to_pyruntime_err)
66    }
67
68    /// Returns the client ID.
69    #[getter]
70    pub fn client_id(&self) -> ClientId {
71        DataClient::client_id(self)
72    }
73
74    /// Returns whether the client is connected.
75    #[getter]
76    pub fn is_connected(&self) -> bool {
77        DataClient::is_connected(self)
78    }
79
80    /// Returns whether the client is disconnected.
81    #[getter]
82    pub fn is_disconnected(&self) -> bool {
83        DataClient::is_disconnected(self)
84    }
85
86    /// Returns the API key associated with this client.
87    #[getter]
88    #[pyo3(name = "api_key")]
89    pub fn py_api_key(&self) -> &str {
90        self.api_key()
91    }
92
93    /// Returns a masked version of the API key for logging purposes.
94    #[getter]
95    #[pyo3(name = "api_key_masked")]
96    pub fn py_api_key_masked(&self) -> String {
97        self.api_key_masked()
98    }
99}