Skip to content

Commit

Permalink
Standardize InstrumentAny enum
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 14, 2024
1 parent 0a1d185 commit 3a96ebf
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 90 deletions.
50 changes: 25 additions & 25 deletions nautilus_core/accounting/src/python/cash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use nautilus_model::{
enums::{AccountType, LiquiditySide, OrderSide},
events::{account::state::AccountState, order::filled::OrderFilled},
identifiers::account_id::AccountId,
instruments::InstrumentType,
instruments::InstrumentAny,
position::Position,
python::instruments::convert_pyobject_to_instrument_type,
types::{currency::Currency, money::Money, price::Price, quantity::Quantity},
Expand Down Expand Up @@ -158,28 +158,28 @@ impl CashAccount {
) -> PyResult<Money> {
let instrument_type = convert_pyobject_to_instrument_type(py, instrument)?;
match instrument_type {
InstrumentType::CryptoFuture(inst) => Ok(self
InstrumentAny::CryptoFuture(inst) => Ok(self
.calculate_balance_locked(inst, side, quantity, price, use_quote_for_inverse)
.unwrap()),
InstrumentType::CryptoPerpetual(inst) => Ok(self
InstrumentAny::CryptoPerpetual(inst) => Ok(self
.calculate_balance_locked(inst, side, quantity, price, use_quote_for_inverse)
.unwrap()),
InstrumentType::CurrencyPair(inst) => Ok(self
InstrumentAny::CurrencyPair(inst) => Ok(self
.calculate_balance_locked(inst, side, quantity, price, use_quote_for_inverse)
.unwrap()),
InstrumentType::Equity(inst) => Ok(self
InstrumentAny::Equity(inst) => Ok(self
.calculate_balance_locked(inst, side, quantity, price, use_quote_for_inverse)
.unwrap()),
InstrumentType::FuturesContract(inst) => Ok(self
InstrumentAny::FuturesContract(inst) => Ok(self
.calculate_balance_locked(inst, side, quantity, price, use_quote_for_inverse)
.unwrap()),
InstrumentType::FuturesSpread(inst) => Ok(self
InstrumentAny::FuturesSpread(inst) => Ok(self
.calculate_balance_locked(inst, side, quantity, price, use_quote_for_inverse)
.unwrap()),
InstrumentType::OptionsContract(inst) => Ok(self
InstrumentAny::OptionsContract(inst) => Ok(self
.calculate_balance_locked(inst, side, quantity, price, use_quote_for_inverse)
.unwrap()),
InstrumentType::OptionsSpread(inst) => Ok(self
InstrumentAny::OptionsSpread(inst) => Ok(self
.calculate_balance_locked(inst, side, quantity, price, use_quote_for_inverse)
.unwrap()),
}
Expand All @@ -200,7 +200,7 @@ impl CashAccount {
}
let instrument_type = convert_pyobject_to_instrument_type(py, instrument)?;
match instrument_type {
InstrumentType::CryptoFuture(inst) => Ok(self
InstrumentAny::CryptoFuture(inst) => Ok(self
.calculate_commission(
inst,
last_qty,
Expand All @@ -209,7 +209,7 @@ impl CashAccount {
use_quote_for_inverse,
)
.unwrap()),
InstrumentType::CryptoPerpetual(inst) => Ok(self
InstrumentAny::CryptoPerpetual(inst) => Ok(self
.calculate_commission(
inst,
last_qty,
Expand All @@ -218,7 +218,7 @@ impl CashAccount {
use_quote_for_inverse,
)
.unwrap()),
InstrumentType::CurrencyPair(inst) => Ok(self
InstrumentAny::CurrencyPair(inst) => Ok(self
.calculate_commission(
inst,
last_qty,
Expand All @@ -227,7 +227,7 @@ impl CashAccount {
use_quote_for_inverse,
)
.unwrap()),
InstrumentType::Equity(inst) => Ok(self
InstrumentAny::Equity(inst) => Ok(self
.calculate_commission(
inst,
last_qty,
Expand All @@ -236,7 +236,7 @@ impl CashAccount {
use_quote_for_inverse,
)
.unwrap()),
InstrumentType::FuturesContract(inst) => Ok(self
InstrumentAny::FuturesContract(inst) => Ok(self
.calculate_commission(
inst,
last_qty,
Expand All @@ -245,7 +245,7 @@ impl CashAccount {
use_quote_for_inverse,
)
.unwrap()),
InstrumentType::FuturesSpread(inst) => Ok(self
InstrumentAny::FuturesSpread(inst) => Ok(self
.calculate_commission(
inst,
last_qty,
Expand All @@ -254,7 +254,7 @@ impl CashAccount {
use_quote_for_inverse,
)
.unwrap()),
InstrumentType::OptionsContract(inst) => Ok(self
InstrumentAny::OptionsContract(inst) => Ok(self
.calculate_commission(
inst,
last_qty,
Expand All @@ -263,7 +263,7 @@ impl CashAccount {
use_quote_for_inverse,
)
.unwrap()),
InstrumentType::OptionsSpread(inst) => Ok(self
InstrumentAny::OptionsSpread(inst) => Ok(self
.calculate_commission(
inst,
last_qty,
Expand All @@ -285,26 +285,26 @@ impl CashAccount {
) -> PyResult<Vec<Money>> {
let instrument_type = convert_pyobject_to_instrument_type(py, instrument)?;
match instrument_type {
InstrumentType::CryptoFuture(inst) => {
InstrumentAny::CryptoFuture(inst) => {
Ok(self.calculate_pnls(inst, fill, position).unwrap())
}
InstrumentType::CryptoPerpetual(inst) => {
InstrumentAny::CryptoPerpetual(inst) => {
Ok(self.calculate_pnls(inst, fill, position).unwrap())
}
InstrumentType::CurrencyPair(inst) => {
InstrumentAny::CurrencyPair(inst) => {
Ok(self.calculate_pnls(inst, fill, position).unwrap())
}
InstrumentType::Equity(inst) => Ok(self.calculate_pnls(inst, fill, position).unwrap()),
InstrumentType::FuturesContract(inst) => {
InstrumentAny::Equity(inst) => Ok(self.calculate_pnls(inst, fill, position).unwrap()),
InstrumentAny::FuturesContract(inst) => {
Ok(self.calculate_pnls(inst, fill, position).unwrap())
}
InstrumentType::FuturesSpread(inst) => {
InstrumentAny::FuturesSpread(inst) => {
Ok(self.calculate_pnls(inst, fill, position).unwrap())
}
InstrumentType::OptionsContract(inst) => {
InstrumentAny::OptionsContract(inst) => {
Ok(self.calculate_pnls(inst, fill, position).unwrap())
}
InstrumentType::OptionsSpread(inst) => {
InstrumentAny::OptionsSpread(inst) => {
Ok(self.calculate_pnls(inst, fill, position).unwrap())
}
}
Expand Down
26 changes: 13 additions & 13 deletions nautilus_core/accounting/src/python/margin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use nautilus_core::python::to_pyvalue_err;
use nautilus_model::{
events::account::state::AccountState,
identifiers::{account_id::AccountId, instrument_id::InstrumentId},
instruments::InstrumentType,
instruments::InstrumentAny,
python::instruments::convert_pyobject_to_instrument_type,
types::{money::Money, price::Price, quantity::Quantity},
};
Expand Down Expand Up @@ -170,22 +170,22 @@ impl MarginAccount {
) -> PyResult<Money> {
let instrument_type = convert_pyobject_to_instrument_type(py, instrument)?;
match instrument_type {
InstrumentType::CryptoFuture(inst) => {
InstrumentAny::CryptoFuture(inst) => {
Ok(self.calculate_initial_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::CryptoPerpetual(inst) => {
InstrumentAny::CryptoPerpetual(inst) => {
Ok(self.calculate_initial_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::CurrencyPair(inst) => {
InstrumentAny::CurrencyPair(inst) => {
Ok(self.calculate_initial_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::Equity(inst) => {
InstrumentAny::Equity(inst) => {
Ok(self.calculate_initial_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::FuturesContract(inst) => {
InstrumentAny::FuturesContract(inst) => {
Ok(self.calculate_initial_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::OptionsContract(inst) => {
InstrumentAny::OptionsContract(inst) => {
Ok(self.calculate_initial_margin(inst, quantity, price, use_quote_for_inverse))
}
_ => Err(to_pyvalue_err("Unsupported instrument type")),
Expand All @@ -203,22 +203,22 @@ impl MarginAccount {
) -> PyResult<Money> {
let instrument_type = convert_pyobject_to_instrument_type(py, instrument)?;
match instrument_type {
InstrumentType::CryptoFuture(inst) => {
InstrumentAny::CryptoFuture(inst) => {
Ok(self.calculate_maintenance_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::CryptoPerpetual(inst) => {
InstrumentAny::CryptoPerpetual(inst) => {
Ok(self.calculate_maintenance_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::CurrencyPair(inst) => {
InstrumentAny::CurrencyPair(inst) => {
Ok(self.calculate_maintenance_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::Equity(inst) => {
InstrumentAny::Equity(inst) => {
Ok(self.calculate_maintenance_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::FuturesContract(inst) => {
InstrumentAny::FuturesContract(inst) => {
Ok(self.calculate_maintenance_margin(inst, quantity, price, use_quote_for_inverse))
}
InstrumentType::OptionsContract(inst) => {
InstrumentAny::OptionsContract(inst) => {
Ok(self.calculate_maintenance_margin(inst, quantity, price, use_quote_for_inverse))
}
_ => Err(to_pyvalue_err("Unsupported instrument type")),
Expand Down
26 changes: 13 additions & 13 deletions nautilus_core/adapters/src/databento/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use nautilus_model::{
identifiers::{instrument_id::InstrumentId, trade_id::TradeId},
instruments::{
equity::Equity, futures_contract::FuturesContract, futures_spread::FuturesSpread,
options_contract::OptionsContract, options_spread::OptionsSpread, InstrumentType,
options_contract::OptionsContract, options_spread::OptionsSpread, InstrumentAny,
},
types::{currency::Currency, fixed::FIXED_SCALAR, price::Price, quantity::Quantity},
};
Expand Down Expand Up @@ -706,29 +706,29 @@ pub fn decode_instrument_def_msg_v1(
msg: &dbn::compat::InstrumentDefMsgV1,
instrument_id: InstrumentId,
ts_init: UnixNanos,
) -> anyhow::Result<InstrumentType> {
) -> anyhow::Result<InstrumentAny> {
match msg.instrument_class as u8 as char {
'K' => Ok(InstrumentType::Equity(decode_equity_v1(
'K' => Ok(InstrumentAny::Equity(decode_equity_v1(
msg,
instrument_id,
ts_init,
)?)),
'F' => Ok(InstrumentType::FuturesContract(decode_futures_contract_v1(
'F' => Ok(InstrumentAny::FuturesContract(decode_futures_contract_v1(
msg,
instrument_id,
ts_init,
)?)),
'S' => Ok(InstrumentType::FuturesSpread(decode_futures_spread_v1(
'S' => Ok(InstrumentAny::FuturesSpread(decode_futures_spread_v1(
msg,
instrument_id,
ts_init,
)?)),
'C' | 'P' => Ok(InstrumentType::OptionsContract(decode_options_contract_v1(
'C' | 'P' => Ok(InstrumentAny::OptionsContract(decode_options_contract_v1(
msg,
instrument_id,
ts_init,
)?)),
'T' | 'M' => Ok(InstrumentType::OptionsSpread(decode_options_spread_v1(
'T' | 'M' => Ok(InstrumentAny::OptionsSpread(decode_options_spread_v1(
msg,
instrument_id,
ts_init,
Expand All @@ -746,29 +746,29 @@ pub fn decode_instrument_def_msg(
msg: &dbn::InstrumentDefMsg,
instrument_id: InstrumentId,
ts_init: UnixNanos,
) -> anyhow::Result<InstrumentType> {
) -> anyhow::Result<InstrumentAny> {
match msg.instrument_class as u8 as char {
'K' => Ok(InstrumentType::Equity(decode_equity(
'K' => Ok(InstrumentAny::Equity(decode_equity(
msg,
instrument_id,
ts_init,
)?)),
'F' => Ok(InstrumentType::FuturesContract(decode_futures_contract(
'F' => Ok(InstrumentAny::FuturesContract(decode_futures_contract(
msg,
instrument_id,
ts_init,
)?)),
'S' => Ok(InstrumentType::FuturesSpread(decode_futures_spread(
'S' => Ok(InstrumentAny::FuturesSpread(decode_futures_spread(
msg,
instrument_id,
ts_init,
)?)),
'C' | 'P' => Ok(InstrumentType::OptionsContract(decode_options_contract(
'C' | 'P' => Ok(InstrumentAny::OptionsContract(decode_options_contract(
msg,
instrument_id,
ts_init,
)?)),
'T' | 'M' => Ok(InstrumentType::OptionsSpread(decode_options_spread(
'T' | 'M' => Ok(InstrumentAny::OptionsSpread(decode_options_spread(
msg,
instrument_id,
ts_init,
Expand Down
6 changes: 3 additions & 3 deletions nautilus_core/adapters/src/databento/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use nautilus_model::{
},
enums::RecordFlag,
identifiers::{instrument_id::InstrumentId, symbol::Symbol, venue::Venue},
instruments::InstrumentType,
instruments::InstrumentAny,
};
use tokio::{
sync::mpsc::{self, error::TryRecvError},
Expand Down Expand Up @@ -62,7 +62,7 @@ pub enum LiveCommand {
#[allow(clippy::large_enum_variant)] // TODO: Optimize this (largest variant 1096 vs 80 bytes)
pub enum LiveMessage {
Data(Data),
Instrument(InstrumentType),
Instrument(InstrumentAny),
Imbalance(DatabentoImbalance),
Statistics(DatabentoStatistics),
Error(anyhow::Error),
Expand Down Expand Up @@ -375,7 +375,7 @@ fn handle_instrument_def_msg(
msg: &dbn::InstrumentDefMsg,
publisher_venue_map: &IndexMap<PublisherId, Venue>,
clock: &AtomicTime,
) -> anyhow::Result<InstrumentType> {
) -> anyhow::Result<InstrumentAny> {
let c_str: &CStr = unsafe { CStr::from_ptr(msg.raw_symbol.as_ptr()) };
let raw_symbol: &str = c_str.to_str().map_err(to_pyvalue_err)?;

Expand Down
4 changes: 2 additions & 2 deletions nautilus_core/adapters/src/databento/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use indexmap::IndexMap;
use nautilus_model::{
data::Data,
identifiers::{instrument_id::InstrumentId, symbol::Symbol, venue::Venue},
instruments::InstrumentType,
instruments::InstrumentAny,
types::currency::Currency,
};
use streaming_iterator::StreamingIterator;
Expand Down Expand Up @@ -148,7 +148,7 @@ impl DatabentoDataLoader {
pub fn read_definition_records(
&mut self,
path: PathBuf,
) -> anyhow::Result<impl Iterator<Item = anyhow::Result<InstrumentType>> + '_> {
) -> anyhow::Result<impl Iterator<Item = anyhow::Result<InstrumentAny>> + '_> {
let mut decoder = Decoder::from_zstd_file(path)?;
decoder.set_upgrade_policy(dbn::VersionUpgradePolicy::Upgrade);
let mut dbn_stream = decoder.decode_stream::<InstrumentDefMsgV1>();
Expand Down
8 changes: 4 additions & 4 deletions nautilus_core/execution/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use nautilus_model::{
identifiers::{
client_id::ClientId, instrument_id::InstrumentId, strategy_id::StrategyId, venue::Venue,
},
instruments::InstrumentType,
instruments::InstrumentAny,
orders::base::OrderAny,
position::Position,
types::quantity::Quantity,
Expand Down Expand Up @@ -221,7 +221,7 @@ impl ExecutionEngine {

fn open_position(
&self,
instrument: InstrumentType,
instrument: InstrumentAny,
position: &Position,
fill: OrderFilled,
oms_type: OmsType,
Expand All @@ -231,7 +231,7 @@ impl ExecutionEngine {

fn update_position(
&self,
instrument: InstrumentType,
instrument: InstrumentAny,
position: &Position,
fill: OrderFilled,
oms_type: OmsType,
Expand All @@ -245,7 +245,7 @@ impl ExecutionEngine {

fn flip_position(
&self,
instrument: InstrumentType,
instrument: InstrumentAny,
position: &Position,
fill: OrderFilled,
oms_type: OmsType,
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/instruments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
};

#[derive(Debug)]
pub enum InstrumentType {
pub enum InstrumentAny {
CryptoFuture(CryptoFuture),
CryptoPerpetual(CryptoPerpetual),
CurrencyPair(CurrencyPair),
Expand Down
Loading

0 comments on commit 3a96ebf

Please sign in to comment.