use nautilus_core::python::to_pyvalue_err;
use pyo3::{IntoPyObjectExt, Py, PyAny, PyResult, Python};
use crate::{
instruments::{
BettingInstrument, BinaryOption, Cfd, Commodity, CryptoFuture, CryptoPerpetual,
CurrencyPair, Equity, FuturesContract, FuturesSpread, IndexInstrument, InstrumentAny,
OptionContract, OptionSpread, PerpetualContract, TokenizedAsset,
crypto_option::CryptoOption,
},
types::{Money, Price, Quantity},
};
macro_rules! impl_instrument_common_pymethods {
($type:ty) => {
#[pyo3::pymethods]
impl $type {
fn __repr__(&self) -> String {
use crate::instruments::Instrument;
format!(
"{}(id={}, price_precision={}, size_precision={})",
stringify!($type),
self.id(),
self.price_precision(),
self.size_precision(),
)
}
#[pyo3(name = "make_price")]
fn py_make_price(&self, value: f64) -> pyo3::PyResult<Price> {
use crate::instruments::Instrument;
self.try_make_price(value)
.map_err(nautilus_core::python::to_pyvalue_err)
}
#[pyo3(name = "make_qty")]
#[pyo3(signature = (value, round_down=false))]
fn py_make_qty(&self, value: f64, round_down: bool) -> pyo3::PyResult<Quantity> {
use crate::instruments::Instrument;
self.try_make_qty(value, Some(round_down))
.map_err(nautilus_core::python::to_pyvalue_err)
}
#[pyo3(name = "notional_value")]
#[pyo3(signature = (quantity, price, use_quote_for_inverse=false))]
fn py_notional_value(
&self,
quantity: Quantity,
price: Price,
use_quote_for_inverse: bool,
) -> Money {
use crate::instruments::Instrument;
self.calculate_notional_value(quantity, price, Some(use_quote_for_inverse))
}
}
};
}
impl_instrument_common_pymethods!(BettingInstrument);
impl_instrument_common_pymethods!(BinaryOption);
impl_instrument_common_pymethods!(Cfd);
impl_instrument_common_pymethods!(Commodity);
impl_instrument_common_pymethods!(CryptoFuture);
impl_instrument_common_pymethods!(CryptoOption);
impl_instrument_common_pymethods!(CryptoPerpetual);
impl_instrument_common_pymethods!(CurrencyPair);
impl_instrument_common_pymethods!(Equity);
impl_instrument_common_pymethods!(FuturesContract);
impl_instrument_common_pymethods!(FuturesSpread);
impl_instrument_common_pymethods!(IndexInstrument);
impl_instrument_common_pymethods!(OptionContract);
impl_instrument_common_pymethods!(OptionSpread);
impl_instrument_common_pymethods!(PerpetualContract);
impl_instrument_common_pymethods!(TokenizedAsset);
pub mod betting;
pub mod binary_option;
pub mod cfd;
pub mod commodity;
pub mod crypto_future;
pub mod crypto_option;
pub mod crypto_perpetual;
pub mod currency_pair;
pub mod equity;
pub mod futures_contract;
pub mod futures_spread;
pub mod index_instrument;
pub mod option_contract;
pub mod option_spread;
pub mod perpetual_contract;
pub mod synthetic;
pub mod tokenized_asset;
pub fn instrument_any_to_pyobject(py: Python, instrument: InstrumentAny) -> PyResult<Py<PyAny>> {
match instrument {
InstrumentAny::Betting(inst) => inst.into_py_any(py),
InstrumentAny::BinaryOption(inst) => inst.into_py_any(py),
InstrumentAny::Cfd(inst) => inst.into_py_any(py),
InstrumentAny::Commodity(inst) => inst.into_py_any(py),
InstrumentAny::CryptoFuture(inst) => inst.into_py_any(py),
InstrumentAny::CryptoOption(inst) => inst.into_py_any(py),
InstrumentAny::CryptoPerpetual(inst) => inst.into_py_any(py),
InstrumentAny::CurrencyPair(inst) => inst.into_py_any(py),
InstrumentAny::Equity(inst) => inst.into_py_any(py),
InstrumentAny::FuturesContract(inst) => inst.into_py_any(py),
InstrumentAny::FuturesSpread(inst) => inst.into_py_any(py),
InstrumentAny::IndexInstrument(inst) => inst.into_py_any(py),
InstrumentAny::OptionContract(inst) => inst.into_py_any(py),
InstrumentAny::OptionSpread(inst) => inst.into_py_any(py),
InstrumentAny::PerpetualContract(inst) => inst.into_py_any(py),
InstrumentAny::TokenizedAsset(inst) => inst.into_py_any(py),
}
}
#[allow(clippy::needless_pass_by_value)]
pub fn pyobject_to_instrument_any(py: Python, instrument: Py<PyAny>) -> PyResult<InstrumentAny> {
match instrument.getattr(py, "type_name")?.extract::<&str>(py)? {
stringify!(BettingInstrument) => Ok(InstrumentAny::Betting(
instrument.extract::<BettingInstrument>(py)?,
)),
stringify!(BinaryOption) => Ok(InstrumentAny::BinaryOption(
instrument.extract::<BinaryOption>(py)?,
)),
stringify!(Cfd) => Ok(InstrumentAny::Cfd(instrument.extract::<Cfd>(py)?)),
stringify!(Commodity) => Ok(InstrumentAny::Commodity(
instrument.extract::<Commodity>(py)?,
)),
stringify!(CryptoFuture) => Ok(InstrumentAny::CryptoFuture(
instrument.extract::<CryptoFuture>(py)?,
)),
stringify!(CryptoOption) => Ok(InstrumentAny::CryptoOption(
instrument.extract::<CryptoOption>(py)?,
)),
stringify!(CryptoPerpetual) => Ok(InstrumentAny::CryptoPerpetual(
instrument.extract::<CryptoPerpetual>(py)?,
)),
stringify!(CurrencyPair) => Ok(InstrumentAny::CurrencyPair(
instrument.extract::<CurrencyPair>(py)?,
)),
stringify!(Equity) => Ok(InstrumentAny::Equity(instrument.extract::<Equity>(py)?)),
stringify!(FuturesContract) => Ok(InstrumentAny::FuturesContract(
instrument.extract::<FuturesContract>(py)?,
)),
stringify!(FuturesSpread) => Ok(InstrumentAny::FuturesSpread(
instrument.extract::<FuturesSpread>(py)?,
)),
stringify!(IndexInstrument) => Ok(InstrumentAny::IndexInstrument(
instrument.extract::<IndexInstrument>(py)?,
)),
stringify!(OptionContract) => Ok(InstrumentAny::OptionContract(
instrument.extract::<OptionContract>(py)?,
)),
stringify!(OptionSpread) => Ok(InstrumentAny::OptionSpread(
instrument.extract::<OptionSpread>(py)?,
)),
stringify!(PerpetualContract) => Ok(InstrumentAny::PerpetualContract(
instrument.extract::<PerpetualContract>(py)?,
)),
stringify!(TokenizedAsset) => Ok(InstrumentAny::TokenizedAsset(
instrument.extract::<TokenizedAsset>(py)?,
)),
_ => Err(to_pyvalue_err(
"Error in conversion from `Py<PyAny>` to `InstrumentAny`",
)),
}
}