nautilus_pyo3/
lib.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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//! [NautilusTrader](http://nautilustrader.io) is an open-source, high-performance, production-grade
17//! algorithmic trading platform, providing quantitative traders with the ability to backtest
18//! portfolios of automated trading strategies on historical data with an event-driven engine,
19//! and also deploy those same strategies live, with no code changes.
20//!
21//! # Feature flags
22//!
23//! This crate provides feature flags to control source code inclusion during compilation,
24//! depending on the intended use case, i.e. whether to provide Python bindings
25//! for the [nautilus_trader](https://pypi.org/project/nautilus_trader) Python package,
26//! or as part of a Rust only build.
27//!
28//! - `ffi`: Enables the C foreign function interface (FFI) from [cbindgen](https://github.com/mozilla/cbindgen).
29//! - `high-precision`: Enables [high-precision mode](https://nautilustrader.io/docs/nightly/getting_started/installation#precision-mode) to use 128-bit value types.
30
31#![warn(rustc::all)]
32#![deny(unsafe_code)]
33#![deny(nonstandard_style)]
34#![deny(rustdoc::broken_intra_doc_links)]
35#![deny(clippy::missing_errors_doc)]
36
37use pyo3::prelude::*;
38
39/// We modify sys modules so that submodule can be loaded directly as
40/// import supermodule.submodule
41///
42/// Also re-exports all submodule attributes so they can be imported directly from `nautilus_pyo3`
43/// refer: https://github.com/PyO3/pyo3/issues/2644
44#[pymodule]
45fn nautilus_pyo3(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
46    let sys = PyModule::import(py, "sys")?;
47    let modules = sys.getattr("modules")?;
48    let sys_modules: &Bound<'_, PyAny> = modules.downcast()?;
49    let module_name = "nautilus_trader.core.nautilus_pyo3";
50
51    // Set pyo3_nautilus to be recognized as a subpackage
52    sys_modules.set_item(module_name, m)?;
53
54    let n = "core";
55    let submodule = pyo3::wrap_pymodule!(nautilus_core::python::core);
56    m.add_wrapped(submodule)?;
57    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
58    re_export_module_attributes(m, n)?;
59
60    let n = "common";
61    let submodule = pyo3::wrap_pymodule!(nautilus_common::python::common);
62    m.add_wrapped(submodule)?;
63    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
64    re_export_module_attributes(m, n)?;
65
66    let n = "cryptography";
67    let submodule = pyo3::wrap_pymodule!(nautilus_cryptography::python::cryptography);
68    m.add_wrapped(submodule)?;
69    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
70    re_export_module_attributes(m, n)?;
71
72    let n = "execution";
73    let submodule = pyo3::wrap_pymodule!(nautilus_execution::python::execution);
74    m.add_wrapped(submodule)?;
75    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
76    re_export_module_attributes(m, n)?;
77
78    let n = "model";
79    let submodule = pyo3::wrap_pymodule!(nautilus_model::python::model);
80    m.add_wrapped(submodule)?;
81    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
82    re_export_module_attributes(m, n)?;
83
84    let n = "indicators";
85    let submodule = pyo3::wrap_pymodule!(nautilus_indicators::python::indicators);
86    m.add_wrapped(submodule)?;
87    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
88    re_export_module_attributes(m, n)?;
89
90    let n = "infrastructure";
91    let submodule = pyo3::wrap_pymodule!(nautilus_infrastructure::python::infrastructure);
92    m.add_wrapped(submodule)?;
93    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
94    re_export_module_attributes(m, n)?;
95
96    let n = "network";
97    let submodule = pyo3::wrap_pymodule!(nautilus_network::python::network);
98    m.add_wrapped(submodule)?;
99    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
100    re_export_module_attributes(m, n)?;
101
102    let n = "persistence";
103    let submodule = pyo3::wrap_pymodule!(nautilus_persistence::python::persistence);
104    m.add_wrapped(submodule)?;
105    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
106    re_export_module_attributes(m, n)?;
107
108    let n = "serialization";
109    let submodule = pyo3::wrap_pymodule!(nautilus_serialization::python::serialization);
110    m.add_wrapped(submodule)?;
111    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
112    re_export_module_attributes(m, n)?;
113
114    let n = "testkit";
115    let submodule = pyo3::wrap_pymodule!(nautilus_testkit::python::testkit);
116    m.add_wrapped(submodule)?;
117    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
118    re_export_module_attributes(m, n)?;
119
120    let n = "trading";
121    let submodule = pyo3::wrap_pymodule!(nautilus_trading::python::trading);
122    m.add_wrapped(submodule)?;
123    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
124    re_export_module_attributes(m, n)?;
125
126    // Adapters
127
128    let n = "coinbase_intx";
129    let submodule = pyo3::wrap_pymodule!(nautilus_coinbase_intx::python::coinbase_intx);
130    m.add_wrapped(submodule)?;
131    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
132    re_export_module_attributes(m, n)?;
133
134    let n = "databento";
135    let submodule = pyo3::wrap_pymodule!(nautilus_databento::python::databento);
136    m.add_wrapped(submodule)?;
137    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
138    re_export_module_attributes(m, n)?;
139
140    let n = "tardis";
141    let submodule = pyo3::wrap_pymodule!(nautilus_tardis::python::tardis);
142    m.add_wrapped(submodule)?;
143    sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
144    re_export_module_attributes(m, n)?;
145
146    Ok(())
147}
148
149fn re_export_module_attributes(
150    parent_module: &Bound<'_, PyModule>,
151    submodule_name: &str,
152) -> PyResult<()> {
153    let submodule = parent_module.getattr(submodule_name)?;
154    for item_name in submodule.dir()? {
155        let item_name_str: &str = item_name.extract()?;
156        if let Ok(attr) = submodule.getattr(item_name_str) {
157            parent_module.add(item_name_str, attr)?;
158        }
159    }
160
161    Ok(())
162}