nautilus_tardis/python/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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 from [PyO3](https://pyo3.rs).
17
18pub mod config;
19pub mod csv;
20pub mod enums;
21pub mod http;
22pub mod machine;
23
24use nautilus_core::python::enums::parse_enum;
25use pyo3::prelude::*;
26use ustr::Ustr;
27
28use super::enums::{Exchange, InstrumentType};
29use crate::parse::normalize_symbol_str;
30
31/// Normalize a symbol string for Tardis, returning a suffix-modified symbol.
32///
33/// # Errors
34///
35/// Returns a `PyErr` if the `exchange` or `instrument_type` cannot be parsed.
36#[pyfunction(name = "tardis_normalize_symbol_str")]
37#[pyo3(signature = (symbol, exchange, instrument_type, is_inverse=None))]
38pub fn py_tardis_normalize_symbol_str(
39    symbol: String,
40    exchange: String,
41    instrument_type: String,
42    is_inverse: Option<bool>,
43) -> PyResult<String> {
44    let symbol = Ustr::from(&symbol);
45    let exchange: Exchange = parse_enum(&exchange, stringify!(exchange))?;
46    let instrument_type: InstrumentType =
47        parse_enum(&instrument_type, stringify!(instrument_type))?;
48
49    Ok(normalize_symbol_str(symbol, &exchange, &instrument_type, is_inverse).to_string())
50}
51
52/// Loaded as `nautilus_pyo3.tardis`.
53///
54/// # Errors
55///
56/// Returns a `PyErr` if registering any module components fails.
57#[pymodule]
58pub fn tardis(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
59    m.add_class::<super::machine::types::InstrumentMiniInfo>()?;
60    m.add_class::<super::machine::types::ReplayNormalizedRequestOptions>()?;
61    m.add_class::<super::machine::types::StreamNormalizedRequestOptions>()?;
62    m.add_class::<super::machine::TardisMachineClient>()?;
63    m.add_class::<super::http::client::TardisHttpClient>()?;
64    m.add_function(wrap_pyfunction!(
65        enums::py_tardis_exchange_from_venue_str,
66        m
67    )?)?;
68    m.add_function(wrap_pyfunction!(
69        config::py_bar_spec_to_tardis_trade_bar_string,
70        m
71    )?)?;
72    m.add_function(wrap_pyfunction!(machine::py_run_tardis_machine_replay, m)?)?;
73    m.add_function(wrap_pyfunction!(csv::py_load_tardis_deltas, m)?)?;
74    m.add_function(wrap_pyfunction!(
75        csv::py_load_tardis_depth10_from_snapshot5,
76        m
77    )?)?;
78    m.add_function(wrap_pyfunction!(
79        csv::py_load_tardis_depth10_from_snapshot25,
80        m
81    )?)?;
82    m.add_function(wrap_pyfunction!(csv::py_load_tardis_quotes, m)?)?;
83    m.add_function(wrap_pyfunction!(csv::py_load_tardis_trades, m)?)?;
84    m.add_function(wrap_pyfunction!(py_tardis_normalize_symbol_str, m)?)?;
85
86    Ok(())
87}