Skip to main content

nautilus_model/instruments/
any.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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
16use 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
51// TODO: Probably move this to the `Instrument` trait too
52impl 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    /// Returns true if the instrument is a spread instrument.
76    #[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}