Skip to main content

nautilus_tardis/python/
config.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
16use nautilus_core::python::to_pyvalue_err;
17use nautilus_model::{data::BarSpecification, identifiers::InstrumentId};
18use pyo3::prelude::*;
19use ustr::Ustr;
20
21use crate::{
22    common::{enums::TardisExchange, parse::bar_spec_to_tardis_trade_bar_string},
23    config::TardisDataClientConfig,
24    machine::types::{
25        ReplayNormalizedRequestOptions, StreamNormalizedRequestOptions, TardisInstrumentMiniInfo,
26    },
27};
28
29#[pymethods]
30#[pyo3_stub_gen::derive::gen_stub_pymethods]
31impl TardisInstrumentMiniInfo {
32    /// Instrument definition information necessary for stream parsing.
33    #[new]
34    fn py_new(
35        instrument_id: InstrumentId,
36        raw_symbol: &str,
37        exchange: &str,
38        price_precision: u8,
39        size_precision: u8,
40    ) -> Self {
41        let exchange: TardisExchange = exchange
42            .parse()
43            .expect("`exchange` should be Tardis convention");
44        Self::new(
45            instrument_id,
46            Some(Ustr::from(raw_symbol)),
47            exchange,
48            price_precision,
49            size_precision,
50        )
51    }
52
53    #[getter]
54    #[pyo3(name = "instrument_id")]
55    const fn py_instrument_id(&self) -> InstrumentId {
56        self.instrument_id
57    }
58
59    #[getter]
60    #[pyo3(name = "raw_symbol")]
61    fn py_raw_symbol(&self) -> String {
62        self.raw_symbol.to_string()
63    }
64
65    #[getter]
66    #[pyo3(name = "exchange")]
67    fn py_exchange(&self) -> String {
68        self.exchange.to_string()
69    }
70
71    #[getter]
72    #[pyo3(name = "price_precision")]
73    const fn py_price_precision(&self) -> u8 {
74        self.price_precision
75    }
76
77    #[getter]
78    #[pyo3(name = "size_precision")]
79    const fn py_size_precision(&self) -> u8 {
80        self.size_precision
81    }
82}
83
84/// Converts a Nautilus `BarSpecification` to the Tardis trade bar string convention.
85///
86/// # Errors
87///
88/// Returns an error if the bar aggregation kind is unsupported.
89#[pyfunction(name = "bar_spec_to_tardis_trade_bar_string")]
90#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.tardis")]
91pub fn py_bar_spec_to_tardis_trade_bar_string(bar_spec: &BarSpecification) -> PyResult<String> {
92    bar_spec_to_tardis_trade_bar_string(bar_spec).map_err(to_pyvalue_err)
93}
94
95#[pymethods]
96#[pyo3_stub_gen::derive::gen_stub_pymethods]
97impl TardisDataClientConfig {
98    /// Configuration for the Tardis data client.
99    #[new]
100    #[pyo3(signature = (
101        api_key = None,
102        tardis_ws_url = None,
103        proxy_url = None,
104        normalize_symbols = None,
105        options = None,
106        stream_options = None,
107    ))]
108    fn py_new(
109        api_key: Option<String>,
110        tardis_ws_url: Option<String>,
111        proxy_url: Option<String>,
112        normalize_symbols: Option<bool>,
113        options: Option<Vec<ReplayNormalizedRequestOptions>>,
114        stream_options: Option<Vec<StreamNormalizedRequestOptions>>,
115    ) -> Self {
116        let defaults = Self::default();
117        Self {
118            api_key,
119            tardis_ws_url,
120            proxy_url,
121            normalize_symbols: normalize_symbols.unwrap_or(defaults.normalize_symbols),
122            book_snapshot_output: defaults.book_snapshot_output,
123            options: options.unwrap_or_default(),
124            stream_options: stream_options.unwrap_or_default(),
125        }
126    }
127
128    fn __repr__(&self) -> String {
129        format!("{self:?}")
130    }
131}