Skip to main content

nautilus_databento/python/
factories.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 factory.
17
18use std::path::PathBuf;
19
20use indexmap::IndexMap;
21use nautilus_core::{python::to_pyruntime_err, time::get_atomic_clock_realtime};
22use nautilus_model::identifiers::ClientId;
23use pyo3::prelude::*;
24
25use crate::{
26    data::{DatabentoDataClient, DatabentoDataClientConfig},
27    factories::DatabentoDataClientFactory,
28};
29
30#[pymethods]
31#[pyo3_stub_gen::derive::gen_stub_pymethods]
32impl DatabentoDataClientConfig {
33    /// Configuration for the Databento data client.
34    #[new]
35    #[pyo3(signature = (api_key, publishers_filepath, use_exchange_as_venue=false, bars_timestamp_on_close=true, venue_dataset_map=None, historical_base_url=None, live_gateway_addr=None))]
36    fn py_new(
37        api_key: String,
38        publishers_filepath: PathBuf,
39        use_exchange_as_venue: bool,
40        bars_timestamp_on_close: bool,
41        venue_dataset_map: Option<IndexMap<String, String>>,
42        historical_base_url: Option<String>,
43        live_gateway_addr: Option<String>,
44    ) -> Self {
45        let mut config = Self::new(
46            api_key,
47            publishers_filepath,
48            use_exchange_as_venue,
49            bars_timestamp_on_close,
50        );
51
52        if let Some(venue_dataset_map) = venue_dataset_map {
53            config.venue_dataset_map = venue_dataset_map;
54        }
55
56        config.historical_base_url = historical_base_url;
57        config.live_gateway_addr = live_gateway_addr;
58
59        config
60    }
61
62    fn __repr__(&self) -> String {
63        format!("{self:?}")
64    }
65}
66
67#[pymethods]
68#[pyo3_stub_gen::derive::gen_stub_pymethods]
69impl DatabentoDataClientFactory {
70    /// Factory for creating Databento data clients.
71    #[new]
72    fn py_new() -> Self {
73        Self
74    }
75
76    #[pyo3(name = "name")]
77    fn py_name(&self) -> &'static str {
78        "DATABENTO"
79    }
80
81    /// Creates a new `DatabentoDataClient` instance.
82    ///
83    /// # Errors
84    ///
85    /// Returns an error if the client cannot be created or publisher configuration cannot be loaded.
86    #[staticmethod]
87    #[pyo3(name = "create_live_data_client")]
88    #[pyo3(signature = (client_id, api_key, publishers_filepath, use_exchange_as_venue = true, bars_timestamp_on_close = true))]
89    pub fn py_create_live_data_client(
90        client_id: ClientId,
91        api_key: String,
92        publishers_filepath: PathBuf,
93        use_exchange_as_venue: bool,
94        bars_timestamp_on_close: bool,
95    ) -> PyResult<DatabentoDataClient> {
96        Self::create_live_data_client(
97            client_id,
98            api_key,
99            publishers_filepath,
100            use_exchange_as_venue,
101            bars_timestamp_on_close,
102            get_atomic_clock_realtime(),
103        )
104        .map_err(to_pyruntime_err)
105    }
106}