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))]
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    ) -> PyResult<Self> {
49        let futures_year_digits = futures_year_digits
50            .map(FuturesYearDigits::try_from)
51            .transpose()
52            .map_err(to_pyvalue_err)?
53            .unwrap_or_default();
54
55        let mut config = DatabentoDataClientConfig::new(
56            api_key,
57            publishers_filepath,
58            use_exchange_as_venue,
59            bars_timestamp_on_close,
60        );
61
62        config.futures_year_digits = futures_year_digits;
63        Self::new(client_id, config, get_atomic_clock_realtime()).map_err(to_pyruntime_err)
64    }
65
66    /// Returns the client ID.
67    #[getter]
68    pub fn client_id(&self) -> ClientId {
69        DataClient::client_id(self)
70    }
71
72    /// Returns whether the client is connected.
73    #[getter]
74    pub fn is_connected(&self) -> bool {
75        DataClient::is_connected(self)
76    }
77
78    /// Returns whether the client is disconnected.
79    #[getter]
80    pub fn is_disconnected(&self) -> bool {
81        DataClient::is_disconnected(self)
82    }
83
84    /// Returns the API key associated with this client.
85    #[getter]
86    #[pyo3(name = "api_key")]
87    pub fn py_api_key(&self) -> &str {
88        self.api_key()
89    }
90
91    /// Returns a masked version of the API key for logging purposes.
92    #[getter]
93    #[pyo3(name = "api_key_masked")]
94    pub fn py_api_key_masked(&self) -> String {
95        self.api_key_masked()
96    }
97}