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::{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
39pub fn instrument_any_to_pyobject(py: Python, instrument: InstrumentAny) -> PyResult<PyObject> {
40    match instrument {
41        InstrumentAny::Betting(inst) => inst.into_py_any(py),
42        InstrumentAny::BinaryOption(inst) => inst.into_py_any(py),
43        InstrumentAny::CryptoFuture(inst) => inst.into_py_any(py),
44        InstrumentAny::CryptoOption(inst) => inst.into_py_any(py),
45        InstrumentAny::CryptoPerpetual(inst) => inst.into_py_any(py),
46        InstrumentAny::CurrencyPair(inst) => inst.into_py_any(py),
47        InstrumentAny::Equity(inst) => inst.into_py_any(py),
48        InstrumentAny::FuturesContract(inst) => inst.into_py_any(py),
49        InstrumentAny::FuturesSpread(inst) => inst.into_py_any(py),
50        InstrumentAny::OptionContract(inst) => inst.into_py_any(py),
51        InstrumentAny::OptionSpread(inst) => inst.into_py_any(py),
52    }
53}
54
55pub fn pyobject_to_instrument_any(py: Python, instrument: PyObject) -> PyResult<InstrumentAny> {
56    match instrument.getattr(py, "type_str")?.extract::<&str>(py)? {
57        stringify!(BettingInstrument) => Ok(InstrumentAny::Betting(
58            instrument.extract::<BettingInstrument>(py)?,
59        )),
60        stringify!(BinaryOption) => Ok(InstrumentAny::BinaryOption(
61            instrument.extract::<BinaryOption>(py)?,
62        )),
63        stringify!(CryptoFuture) => Ok(InstrumentAny::CryptoFuture(
64            instrument.extract::<CryptoFuture>(py)?,
65        )),
66        stringify!(CryptoOption) => Ok(InstrumentAny::CryptoOption(
67            instrument.extract::<CryptoOption>(py)?,
68        )),
69        stringify!(CryptoPerpetual) => Ok(InstrumentAny::CryptoPerpetual(
70            instrument.extract::<CryptoPerpetual>(py)?,
71        )),
72        stringify!(CurrencyPair) => Ok(InstrumentAny::CurrencyPair(
73            instrument.extract::<CurrencyPair>(py)?,
74        )),
75        stringify!(Equity) => Ok(InstrumentAny::Equity(instrument.extract::<Equity>(py)?)),
76        stringify!(FuturesContract) => Ok(InstrumentAny::FuturesContract(
77            instrument.extract::<FuturesContract>(py)?,
78        )),
79        stringify!(FuturesSpread) => Ok(InstrumentAny::FuturesSpread(
80            instrument.extract::<FuturesSpread>(py)?,
81        )),
82        stringify!(OptionContract) => Ok(InstrumentAny::OptionContract(
83            instrument.extract::<OptionContract>(py)?,
84        )),
85        stringify!(OptionSpread) => Ok(InstrumentAny::OptionSpread(
86            instrument.extract::<OptionSpread>(py)?,
87        )),
88        _ => Err(to_pyvalue_err(
89            "Error in conversion from `PyObject` to `InstrumentAny`",
90        )),
91    }
92}