nautilus_model/python/instruments/
mod.rs1use 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> {
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
60pub 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}