nautilus_model/instruments/
any.rs1use enum_dispatch::enum_dispatch;
17use serde::{Deserialize, Serialize};
18
19use super::{
20 Instrument, betting::BettingInstrument, binary_option::BinaryOption, cfd::Cfd,
21 commodity::Commodity, crypto_future::CryptoFuture, crypto_option::CryptoOption,
22 crypto_perpetual::CryptoPerpetual, currency_pair::CurrencyPair, equity::Equity,
23 futures_contract::FuturesContract, futures_spread::FuturesSpread,
24 index_instrument::IndexInstrument, option_contract::OptionContract,
25 option_spread::OptionSpread, perpetual_contract::PerpetualContract,
26 tokenized_asset::TokenizedAsset,
27};
28use crate::types::{Price, Quantity};
29
30#[derive(Clone, Debug, Serialize, Deserialize)]
31#[enum_dispatch(Instrument)]
32pub enum InstrumentAny {
33 Betting(BettingInstrument),
34 BinaryOption(BinaryOption),
35 Cfd(Cfd),
36 Commodity(Commodity),
37 CryptoFuture(CryptoFuture),
38 CryptoOption(CryptoOption),
39 CryptoPerpetual(CryptoPerpetual),
40 CurrencyPair(CurrencyPair),
41 Equity(Equity),
42 FuturesContract(FuturesContract),
43 FuturesSpread(FuturesSpread),
44 IndexInstrument(IndexInstrument),
45 OptionContract(OptionContract),
46 OptionSpread(OptionSpread),
47 PerpetualContract(PerpetualContract),
48 TokenizedAsset(TokenizedAsset),
49}
50
51impl InstrumentAny {
53 #[must_use]
54 pub fn get_base_quantity(&self, quantity: Quantity, last_px: Price) -> Quantity {
55 match self {
56 Self::Betting(inst) => inst.calculate_base_quantity(quantity, last_px),
57 Self::BinaryOption(inst) => inst.calculate_base_quantity(quantity, last_px),
58 Self::Cfd(inst) => inst.calculate_base_quantity(quantity, last_px),
59 Self::Commodity(inst) => inst.calculate_base_quantity(quantity, last_px),
60 Self::CryptoFuture(inst) => inst.calculate_base_quantity(quantity, last_px),
61 Self::CryptoOption(inst) => inst.calculate_base_quantity(quantity, last_px),
62 Self::CryptoPerpetual(inst) => inst.calculate_base_quantity(quantity, last_px),
63 Self::CurrencyPair(inst) => inst.calculate_base_quantity(quantity, last_px),
64 Self::Equity(inst) => inst.calculate_base_quantity(quantity, last_px),
65 Self::FuturesContract(inst) => inst.calculate_base_quantity(quantity, last_px),
66 Self::FuturesSpread(inst) => inst.calculate_base_quantity(quantity, last_px),
67 Self::IndexInstrument(inst) => inst.calculate_base_quantity(quantity, last_px),
68 Self::OptionContract(inst) => inst.calculate_base_quantity(quantity, last_px),
69 Self::OptionSpread(inst) => inst.calculate_base_quantity(quantity, last_px),
70 Self::PerpetualContract(inst) => inst.calculate_base_quantity(quantity, last_px),
71 Self::TokenizedAsset(inst) => inst.calculate_base_quantity(quantity, last_px),
72 }
73 }
74
75 #[must_use]
77 pub fn is_spread(&self) -> bool {
78 matches!(self, Self::FuturesSpread(_) | Self::OptionSpread(_))
79 }
80}
81
82impl PartialEq for InstrumentAny {
83 fn eq(&self, other: &Self) -> bool {
84 self.id() == other.id()
85 }
86}
87
88impl crate::data::HasTsInit for InstrumentAny {
89 fn ts_init(&self) -> nautilus_core::UnixNanos {
90 Instrument::ts_init(self)
91 }
92}