Skip to main content

nautilus_model/python/instruments/
mod.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//! Instrument definitions the trading domain model.
17
18use nautilus_core::python::to_pyvalue_err;
19use pyo3::{IntoPyObjectExt, Py, PyAny, PyResult, Python};
20
21use crate::instruments::{
22    BettingInstrument, BinaryOption, Cfd, Commodity, CryptoFuture, CryptoPerpetual, CurrencyPair,
23    Equity, FuturesContract, FuturesSpread, IndexInstrument, InstrumentAny, OptionContract,
24    OptionSpread, PerpetualContract, crypto_option::CryptoOption,
25};
26
27pub mod betting;
28pub mod binary_option;
29pub mod cfd;
30pub mod commodity;
31pub mod crypto_future;
32pub mod crypto_option;
33pub mod crypto_perpetual;
34pub mod currency_pair;
35pub mod equity;
36pub mod futures_contract;
37pub mod futures_spread;
38pub mod index_instrument;
39pub mod option_contract;
40pub mod option_spread;
41pub mod perpetual_contract;
42pub mod synthetic;
43
44/// Converts an [`InstrumentAny`] into a Python object.
45///
46/// # Errors
47///
48/// Returns a `PyErr` if conversion to a Python object fails.
49pub fn instrument_any_to_pyobject(py: Python, instrument: InstrumentAny) -> PyResult<Py<PyAny>> {
50    match instrument {
51        InstrumentAny::Betting(inst) => inst.into_py_any(py),
52        InstrumentAny::BinaryOption(inst) => inst.into_py_any(py),
53        InstrumentAny::Cfd(inst) => inst.into_py_any(py),
54        InstrumentAny::Commodity(inst) => inst.into_py_any(py),
55        InstrumentAny::CryptoFuture(inst) => inst.into_py_any(py),
56        InstrumentAny::CryptoOption(inst) => inst.into_py_any(py),
57        InstrumentAny::CryptoPerpetual(inst) => inst.into_py_any(py),
58        InstrumentAny::CurrencyPair(inst) => inst.into_py_any(py),
59        InstrumentAny::Equity(inst) => inst.into_py_any(py),
60        InstrumentAny::FuturesContract(inst) => inst.into_py_any(py),
61        InstrumentAny::FuturesSpread(inst) => inst.into_py_any(py),
62        InstrumentAny::IndexInstrument(inst) => inst.into_py_any(py),
63        InstrumentAny::OptionContract(inst) => inst.into_py_any(py),
64        InstrumentAny::OptionSpread(inst) => inst.into_py_any(py),
65        InstrumentAny::PerpetualContract(inst) => inst.into_py_any(py),
66    }
67}
68
69/// Converts a Python object into an [`InstrumentAny`] enum.
70///
71/// # Errors
72///
73/// Returns a `PyErr` if extraction fails or the instrument type is unsupported.
74pub fn pyobject_to_instrument_any(py: Python, instrument: Py<PyAny>) -> PyResult<InstrumentAny> {
75    match instrument.getattr(py, "type_str")?.extract::<&str>(py)? {
76        stringify!(BettingInstrument) => Ok(InstrumentAny::Betting(
77            instrument.extract::<BettingInstrument>(py)?,
78        )),
79        stringify!(BinaryOption) => Ok(InstrumentAny::BinaryOption(
80            instrument.extract::<BinaryOption>(py)?,
81        )),
82        stringify!(Cfd) => Ok(InstrumentAny::Cfd(instrument.extract::<Cfd>(py)?)),
83        stringify!(Commodity) => Ok(InstrumentAny::Commodity(
84            instrument.extract::<Commodity>(py)?,
85        )),
86        stringify!(CryptoFuture) => Ok(InstrumentAny::CryptoFuture(
87            instrument.extract::<CryptoFuture>(py)?,
88        )),
89        stringify!(CryptoOption) => Ok(InstrumentAny::CryptoOption(
90            instrument.extract::<CryptoOption>(py)?,
91        )),
92        stringify!(CryptoPerpetual) => Ok(InstrumentAny::CryptoPerpetual(
93            instrument.extract::<CryptoPerpetual>(py)?,
94        )),
95        stringify!(CurrencyPair) => Ok(InstrumentAny::CurrencyPair(
96            instrument.extract::<CurrencyPair>(py)?,
97        )),
98        stringify!(Equity) => Ok(InstrumentAny::Equity(instrument.extract::<Equity>(py)?)),
99        stringify!(FuturesContract) => Ok(InstrumentAny::FuturesContract(
100            instrument.extract::<FuturesContract>(py)?,
101        )),
102        stringify!(FuturesSpread) => Ok(InstrumentAny::FuturesSpread(
103            instrument.extract::<FuturesSpread>(py)?,
104        )),
105        stringify!(IndexInstrument) => Ok(InstrumentAny::IndexInstrument(
106            instrument.extract::<IndexInstrument>(py)?,
107        )),
108        stringify!(OptionContract) => Ok(InstrumentAny::OptionContract(
109            instrument.extract::<OptionContract>(py)?,
110        )),
111        stringify!(OptionSpread) => Ok(InstrumentAny::OptionSpread(
112            instrument.extract::<OptionSpread>(py)?,
113        )),
114        stringify!(PerpetualContract) => Ok(InstrumentAny::PerpetualContract(
115            instrument.extract::<PerpetualContract>(py)?,
116        )),
117        _ => Err(to_pyvalue_err(
118            "Error in conversion from `Py<PyAny>` to `InstrumentAny`",
119        )),
120    }
121}