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