use std::hash::{Hash, Hasher};
use nautilus_core::{
Params, UnixNanos,
correctness::{
FAILED, check_equal_u8, check_valid_string_ascii, check_valid_string_ascii_optional,
},
};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use ustr::Ustr;
use super::{Instrument, any::InstrumentAny};
use crate::{
enums::{AssetClass, InstrumentClass, OptionKind},
identifiers::{InstrumentId, Symbol},
types::{
currency::Currency,
money::Money,
price::{Price, check_positive_price},
quantity::{Quantity, check_positive_quantity},
},
};
#[repr(C)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
)]
pub struct OptionSpread {
pub id: InstrumentId,
pub raw_symbol: Symbol,
pub asset_class: AssetClass,
pub exchange: Option<Ustr>,
pub underlying: Ustr,
pub strategy_type: Ustr,
pub activation_ns: UnixNanos,
pub expiration_ns: UnixNanos,
pub currency: Currency,
pub price_precision: u8,
pub price_increment: Price,
pub size_increment: Quantity,
pub size_precision: u8,
pub multiplier: Quantity,
pub lot_size: Quantity,
pub margin_init: Decimal,
pub margin_maint: Decimal,
pub maker_fee: Decimal,
pub taker_fee: Decimal,
pub max_quantity: Option<Quantity>,
pub min_quantity: Option<Quantity>,
pub max_price: Option<Price>,
pub min_price: Option<Price>,
pub info: Option<Params>,
pub ts_event: UnixNanos,
pub ts_init: UnixNanos,
}
impl OptionSpread {
#[allow(clippy::too_many_arguments)]
pub fn new_checked(
instrument_id: InstrumentId,
raw_symbol: Symbol,
asset_class: AssetClass,
exchange: Option<Ustr>,
underlying: Ustr,
strategy_type: Ustr,
activation_ns: UnixNanos,
expiration_ns: UnixNanos,
currency: Currency,
price_precision: u8,
price_increment: Price,
multiplier: Quantity,
lot_size: Quantity,
max_quantity: Option<Quantity>,
min_quantity: Option<Quantity>,
max_price: Option<Price>,
min_price: Option<Price>,
margin_init: Option<Decimal>,
margin_maint: Option<Decimal>,
maker_fee: Option<Decimal>,
taker_fee: Option<Decimal>,
info: Option<Params>,
ts_event: UnixNanos,
ts_init: UnixNanos,
) -> anyhow::Result<Self> {
check_valid_string_ascii_optional(exchange.map(|u| u.as_str()), stringify!(exchange))?;
check_valid_string_ascii(strategy_type.as_str(), stringify!(strategy_type))?;
check_equal_u8(
price_precision,
price_increment.precision,
stringify!(price_precision),
stringify!(price_increment.precision),
)?;
check_positive_price(price_increment, stringify!(price_increment))?;
check_positive_quantity(multiplier, stringify!(multiplier))?;
check_positive_quantity(lot_size, stringify!(lot_size))?;
Ok(Self {
id: instrument_id,
raw_symbol,
asset_class,
exchange,
underlying,
strategy_type,
activation_ns,
expiration_ns,
currency,
price_precision,
price_increment,
size_precision: 0,
size_increment: Quantity::from("1"),
multiplier,
lot_size,
margin_init: margin_init.unwrap_or_default(),
margin_maint: margin_maint.unwrap_or_default(),
maker_fee: maker_fee.unwrap_or_default(),
taker_fee: taker_fee.unwrap_or_default(),
max_quantity,
min_quantity: Some(min_quantity.unwrap_or(1.into())),
max_price,
min_price,
info,
ts_event,
ts_init,
})
}
#[allow(clippy::too_many_arguments)]
pub fn new(
instrument_id: InstrumentId,
raw_symbol: Symbol,
asset_class: AssetClass,
exchange: Option<Ustr>,
underlying: Ustr,
strategy_type: Ustr,
activation_ns: UnixNanos,
expiration_ns: UnixNanos,
currency: Currency,
price_precision: u8,
price_increment: Price,
multiplier: Quantity,
lot_size: Quantity,
max_quantity: Option<Quantity>,
min_quantity: Option<Quantity>,
max_price: Option<Price>,
min_price: Option<Price>,
margin_init: Option<Decimal>,
margin_maint: Option<Decimal>,
maker_fee: Option<Decimal>,
taker_fee: Option<Decimal>,
info: Option<Params>,
ts_event: UnixNanos,
ts_init: UnixNanos,
) -> Self {
Self::new_checked(
instrument_id,
raw_symbol,
asset_class,
exchange,
underlying,
strategy_type,
activation_ns,
expiration_ns,
currency,
price_precision,
price_increment,
multiplier,
lot_size,
max_quantity,
min_quantity,
max_price,
min_price,
margin_init,
margin_maint,
maker_fee,
taker_fee,
info,
ts_event,
ts_init,
)
.expect(FAILED)
}
}
impl PartialEq<Self> for OptionSpread {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for OptionSpread {}
impl Hash for OptionSpread {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl Instrument for OptionSpread {
fn into_any(self) -> InstrumentAny {
InstrumentAny::OptionSpread(self)
}
fn id(&self) -> InstrumentId {
self.id
}
fn raw_symbol(&self) -> Symbol {
self.raw_symbol
}
fn asset_class(&self) -> AssetClass {
self.asset_class
}
fn instrument_class(&self) -> InstrumentClass {
InstrumentClass::OptionSpread
}
fn underlying(&self) -> Option<Ustr> {
Some(self.underlying)
}
fn base_currency(&self) -> Option<Currency> {
None
}
fn quote_currency(&self) -> Currency {
self.currency
}
fn settlement_currency(&self) -> Currency {
self.currency
}
fn isin(&self) -> Option<Ustr> {
None
}
fn option_kind(&self) -> Option<OptionKind> {
None
}
fn exchange(&self) -> Option<Ustr> {
self.exchange
}
fn strike_price(&self) -> Option<Price> {
None
}
fn activation_ns(&self) -> Option<UnixNanos> {
Some(self.activation_ns)
}
fn expiration_ns(&self) -> Option<UnixNanos> {
Some(self.expiration_ns)
}
fn is_inverse(&self) -> bool {
false
}
fn price_precision(&self) -> u8 {
self.price_precision
}
fn size_precision(&self) -> u8 {
0
}
fn price_increment(&self) -> Price {
self.price_increment
}
fn size_increment(&self) -> Quantity {
Quantity::from(1)
}
fn multiplier(&self) -> Quantity {
self.multiplier
}
fn lot_size(&self) -> Option<Quantity> {
Some(self.lot_size)
}
fn max_quantity(&self) -> Option<Quantity> {
self.max_quantity
}
fn min_quantity(&self) -> Option<Quantity> {
self.min_quantity
}
fn max_notional(&self) -> Option<Money> {
None
}
fn min_notional(&self) -> Option<Money> {
None
}
fn max_price(&self) -> Option<Price> {
self.max_price
}
fn min_price(&self) -> Option<Price> {
self.min_price
}
fn ts_event(&self) -> UnixNanos {
self.ts_event
}
fn ts_init(&self) -> UnixNanos {
self.ts_init
}
fn margin_init(&self) -> Decimal {
self.margin_init
}
fn margin_maint(&self) -> Decimal {
self.margin_maint
}
fn maker_fee(&self) -> Decimal {
self.maker_fee
}
fn taker_fee(&self) -> Decimal {
self.taker_fee
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use ustr::Ustr;
use crate::{
enums::{AssetClass, InstrumentClass},
identifiers::{InstrumentId, Symbol},
instruments::{Instrument, OptionSpread, stubs::*},
types::{Currency, Price, Quantity},
};
#[rstest]
fn test_trait_accessors(option_spread: OptionSpread) {
assert_eq!(
option_spread.id(),
InstrumentId::from("UD:U$: GN 2534559.GLBX")
);
assert_eq!(option_spread.asset_class(), AssetClass::FX);
assert_eq!(
option_spread.instrument_class(),
InstrumentClass::OptionSpread
);
assert_eq!(option_spread.quote_currency(), Currency::USD());
assert!(!option_spread.is_inverse());
assert_eq!(option_spread.exchange(), Some(Ustr::from("XCME")));
assert_eq!(option_spread.size_precision(), 0);
assert_eq!(option_spread.size_increment(), Quantity::from("1"));
assert_eq!(option_spread.min_quantity(), Some(Quantity::from("1")));
}
#[rstest]
fn test_new_checked_price_precision_mismatch() {
let result = OptionSpread::new_checked(
InstrumentId::from("TEST.GLBX"),
Symbol::from("TEST"),
AssetClass::FX,
Some(Ustr::from("XCME")),
Ustr::from("SR3"),
Ustr::from("GN"),
0.into(),
0.into(),
Currency::USD(),
4, Price::from("0.01"),
Quantity::from(1),
Quantity::from(1),
None,
None,
None,
None,
None,
None,
None,
None,
None,
0.into(),
0.into(),
);
assert!(result.is_err());
}
#[rstest]
fn test_serialization_roundtrip(option_spread: OptionSpread) {
let json = serde_json::to_string(&option_spread).unwrap();
let deserialized: OptionSpread = serde_json::from_str(&json).unwrap();
assert_eq!(option_spread, deserialized);
}
}