nautilus_model/python/instruments/
mod.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//! Instrument definitions the trading domain model.
17
18use nautilus_core::python::to_pyvalue_err;
19use pyo3::{IntoPy, PyObject, PyResult, Python};
20
21use crate::instruments::{
22    BettingInstrument, BinaryOption, CryptoFuture, CryptoPerpetual, CurrencyPair, Equity,
23    FuturesContract, FuturesSpread, InstrumentAny, OptionContract, OptionSpread,
24};
25
26pub mod betting;
27pub mod binary_option;
28pub mod crypto_future;
29pub mod crypto_perpetual;
30pub mod currency_pair;
31pub mod equity;
32pub mod futures_contract;
33pub mod futures_spread;
34pub mod option_contract;
35pub mod option_spread;
36
37pub fn instrument_any_to_pyobject(py: Python, instrument: InstrumentAny) -> PyResult<PyObject> {
38    match instrument {
39        InstrumentAny::Betting(inst) => Ok(inst.into_py(py)),
40        InstrumentAny::BinaryOption(inst) => Ok(inst.into_py(py)),
41        InstrumentAny::CryptoFuture(inst) => Ok(inst.into_py(py)),
42        InstrumentAny::CryptoPerpetual(inst) => Ok(inst.into_py(py)),
43        InstrumentAny::CurrencyPair(inst) => Ok(inst.into_py(py)),
44        InstrumentAny::Equity(inst) => Ok(inst.into_py(py)),
45        InstrumentAny::FuturesContract(inst) => Ok(inst.into_py(py)),
46        InstrumentAny::FuturesSpread(inst) => Ok(inst.into_py(py)),
47        InstrumentAny::OptionContract(inst) => Ok(inst.into_py(py)),
48        InstrumentAny::OptionSpread(inst) => Ok(inst.into_py(py)),
49    }
50}
51
52pub fn pyobject_to_instrument_any(py: Python, instrument: PyObject) -> PyResult<InstrumentAny> {
53    match instrument.getattr(py, "type_str")?.extract::<&str>(py)? {
54        stringify!(BettingInstrument) => Ok(InstrumentAny::Betting(
55            instrument.extract::<BettingInstrument>(py)?,
56        )),
57        stringify!(BinaryOption) => Ok(InstrumentAny::BinaryOption(
58            instrument.extract::<BinaryOption>(py)?,
59        )),
60        stringify!(CryptoFuture) => Ok(InstrumentAny::CryptoFuture(
61            instrument.extract::<CryptoFuture>(py)?,
62        )),
63        stringify!(CryptoPerpetual) => Ok(InstrumentAny::CryptoPerpetual(
64            instrument.extract::<CryptoPerpetual>(py)?,
65        )),
66        stringify!(CurrencyPair) => Ok(InstrumentAny::CurrencyPair(
67            instrument.extract::<CurrencyPair>(py)?,
68        )),
69        stringify!(Equity) => Ok(InstrumentAny::Equity(instrument.extract::<Equity>(py)?)),
70        stringify!(FuturesContract) => Ok(InstrumentAny::FuturesContract(
71            instrument.extract::<FuturesContract>(py)?,
72        )),
73        stringify!(FuturesSpread) => Ok(InstrumentAny::FuturesSpread(
74            instrument.extract::<FuturesSpread>(py)?,
75        )),
76        stringify!(OptionContract) => Ok(InstrumentAny::OptionContract(
77            instrument.extract::<OptionContract>(py)?,
78        )),
79        stringify!(OptionSpread) => Ok(InstrumentAny::OptionSpread(
80            instrument.extract::<OptionSpread>(py)?,
81        )),
82        _ => Err(to_pyvalue_err(
83            "Error in conversion from `PyObject` to `InstrumentAny`",
84        )),
85    }
86}