nautilus_model/instruments/
any.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
16use enum_dispatch::enum_dispatch;
17use serde::{Deserialize, Serialize};
18
19use super::{
20    Instrument, betting::BettingInstrument, binary_option::BinaryOption,
21    crypto_future::CryptoFuture, crypto_option::CryptoOption, crypto_perpetual::CryptoPerpetual,
22    currency_pair::CurrencyPair, equity::Equity, futures_contract::FuturesContract,
23    futures_spread::FuturesSpread, option_contract::OptionContract, option_spread::OptionSpread,
24};
25use crate::types::{Price, Quantity};
26
27#[derive(Clone, Debug, Serialize, Deserialize)]
28#[enum_dispatch(Instrument)]
29pub enum InstrumentAny {
30    Betting(BettingInstrument),
31    BinaryOption(BinaryOption),
32    CryptoFuture(CryptoFuture),
33    CryptoOption(CryptoOption),
34    CryptoPerpetual(CryptoPerpetual),
35    CurrencyPair(CurrencyPair),
36    Equity(Equity),
37    FuturesContract(FuturesContract),
38    FuturesSpread(FuturesSpread),
39    OptionContract(OptionContract),
40    OptionSpread(OptionSpread),
41}
42
43// TODO: Probably move this to the `Instrument` trait too
44impl InstrumentAny {
45    #[must_use]
46    pub fn get_base_quantity(&self, quantity: Quantity, last_px: Price) -> Quantity {
47        match self {
48            Self::Betting(inst) => inst.calculate_base_quantity(quantity, last_px),
49            Self::BinaryOption(inst) => inst.calculate_base_quantity(quantity, last_px),
50            Self::CryptoFuture(inst) => inst.calculate_base_quantity(quantity, last_px),
51            Self::CryptoOption(inst) => inst.calculate_base_quantity(quantity, last_px),
52            Self::CryptoPerpetual(inst) => inst.calculate_base_quantity(quantity, last_px),
53            Self::CurrencyPair(inst) => inst.calculate_base_quantity(quantity, last_px),
54            Self::Equity(inst) => inst.calculate_base_quantity(quantity, last_px),
55            Self::FuturesContract(inst) => inst.calculate_base_quantity(quantity, last_px),
56            Self::FuturesSpread(inst) => inst.calculate_base_quantity(quantity, last_px),
57            Self::OptionContract(inst) => inst.calculate_base_quantity(quantity, last_px),
58            Self::OptionSpread(inst) => inst.calculate_base_quantity(quantity, last_px),
59        }
60    }
61}
62
63impl PartialEq for InstrumentAny {
64    fn eq(&self, other: &Self) -> bool {
65        self.id() == other.id()
66    }
67}