1use std::hash::{Hash, Hasher};
17
18use nautilus_core::{
19 Params, UnixNanos,
20 correctness::{
21 CorrectnessResult, check_equal_u8, check_valid_string_ascii,
22 check_valid_string_ascii_optional,
23 },
24};
25use rust_decimal::Decimal;
26use serde::{Deserialize, Serialize};
27use ustr::Ustr;
28
29use super::{Instrument, any::InstrumentAny, tick_scheme::check_tick_scheme};
30use crate::{
31 enums::{AssetClass, InstrumentClass, OptionKind},
32 identifiers::{InstrumentId, Symbol},
33 types::{
34 currency::Currency,
35 money::Money,
36 price::{Price, check_positive_price},
37 quantity::{Quantity, check_positive_quantity},
38 },
39};
40
41#[repr(C)]
43#[derive(Clone, Debug, Serialize, Deserialize)]
44#[cfg_attr(
45 feature = "python",
46 pyo3::pyclass(module = "nautilus_trader.model", from_py_object)
47)]
48#[cfg_attr(
49 feature = "python",
50 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
51)]
52pub struct OptionSpread {
53 pub id: InstrumentId,
55 pub raw_symbol: Symbol,
57 pub asset_class: AssetClass,
59 pub exchange: Option<Ustr>,
61 pub underlying: Ustr,
63 pub strategy_type: Ustr,
65 pub activation_ns: UnixNanos,
67 pub expiration_ns: UnixNanos,
69 pub currency: Currency,
71 pub price_precision: u8,
73 pub price_increment: Price,
75 pub size_increment: Quantity,
77 pub size_precision: u8,
79 pub multiplier: Quantity,
81 pub lot_size: Quantity,
83 pub margin_init: Decimal,
85 pub margin_maint: Decimal,
87 pub maker_fee: Decimal,
89 pub taker_fee: Decimal,
91 pub max_quantity: Option<Quantity>,
93 pub min_quantity: Option<Quantity>,
95 pub max_price: Option<Price>,
97 pub min_price: Option<Price>,
99 pub tick_scheme: Option<Ustr>,
101 pub info: Option<Params>,
103 pub ts_event: UnixNanos,
105 pub ts_init: UnixNanos,
107}
108
109#[bon::bon]
110impl OptionSpread {
111 #[expect(clippy::too_many_arguments)]
112 fn new_checked(
113 instrument_id: InstrumentId,
114 raw_symbol: Symbol,
115 asset_class: AssetClass,
116 exchange: Option<Ustr>,
117 underlying: Ustr,
118 strategy_type: Ustr,
119 activation_ns: UnixNanos,
120 expiration_ns: UnixNanos,
121 currency: Currency,
122 price_precision: u8,
123 price_increment: Price,
124 multiplier: Quantity,
125 lot_size: Quantity,
126 max_quantity: Option<Quantity>,
127 min_quantity: Option<Quantity>,
128 max_price: Option<Price>,
129 min_price: Option<Price>,
130 margin_init: Option<Decimal>,
131 margin_maint: Option<Decimal>,
132 maker_fee: Option<Decimal>,
133 taker_fee: Option<Decimal>,
134 tick_scheme: Option<Ustr>,
135 info: Option<Params>,
136 ts_event: UnixNanos,
137 ts_init: UnixNanos,
138 ) -> CorrectnessResult<Self> {
139 check_valid_string_ascii_optional(exchange, stringify!(exchange))?;
140 check_valid_string_ascii(strategy_type, stringify!(strategy_type))?;
141 check_equal_u8(
142 price_precision,
143 price_increment.precision,
144 stringify!(price_precision),
145 stringify!(price_increment.precision),
146 )?;
147 check_positive_price(price_increment, stringify!(price_increment))?;
148 check_tick_scheme(tick_scheme)?;
149 check_positive_quantity(multiplier, stringify!(multiplier))?;
150 check_positive_quantity(lot_size, stringify!(lot_size))?;
151
152 Ok(Self {
153 id: instrument_id,
154 raw_symbol,
155 asset_class,
156 exchange,
157 underlying,
158 strategy_type,
159 activation_ns,
160 expiration_ns,
161 currency,
162 price_precision,
163 price_increment,
164 size_precision: 0,
165 size_increment: Quantity::from("1"),
166 multiplier,
167 lot_size,
168 margin_init: margin_init.unwrap_or_default(),
169 margin_maint: margin_maint.unwrap_or_default(),
170 maker_fee: maker_fee.unwrap_or_default(),
171 taker_fee: taker_fee.unwrap_or_default(),
172 max_quantity,
173 min_quantity: Some(min_quantity.unwrap_or(1.into())),
174 max_price,
175 min_price,
176 tick_scheme,
177 info,
178 ts_event,
179 ts_init,
180 })
181 }
182
183 #[builder(start_fn = builder, finish_fn = build)]
192 pub fn build_checked(
193 instrument_id: InstrumentId,
194 raw_symbol: Symbol,
195 asset_class: AssetClass,
196 exchange: Option<Ustr>,
197 underlying: Ustr,
198 strategy_type: Ustr,
199 activation_ns: UnixNanos,
200 expiration_ns: UnixNanos,
201 currency: Currency,
202 price_precision: u8,
203 price_increment: Price,
204 multiplier: Quantity,
205 lot_size: Quantity,
206 max_quantity: Option<Quantity>,
207 min_quantity: Option<Quantity>,
208 max_price: Option<Price>,
209 min_price: Option<Price>,
210 margin_init: Option<Decimal>,
211 margin_maint: Option<Decimal>,
212 maker_fee: Option<Decimal>,
213 taker_fee: Option<Decimal>,
214 tick_scheme: Option<Ustr>,
215 info: Option<Params>,
216 ts_event: UnixNanos,
217 ts_init: UnixNanos,
218 ) -> CorrectnessResult<Self> {
219 Self::new_checked(
220 instrument_id,
221 raw_symbol,
222 asset_class,
223 exchange,
224 underlying,
225 strategy_type,
226 activation_ns,
227 expiration_ns,
228 currency,
229 price_precision,
230 price_increment,
231 multiplier,
232 lot_size,
233 max_quantity,
234 min_quantity,
235 max_price,
236 min_price,
237 margin_init,
238 margin_maint,
239 maker_fee,
240 taker_fee,
241 tick_scheme,
242 info,
243 ts_event,
244 ts_init,
245 )
246 }
247}
248
249impl PartialEq<Self> for OptionSpread {
250 fn eq(&self, other: &Self) -> bool {
251 self.id == other.id
252 }
253}
254
255impl Eq for OptionSpread {}
256
257impl Hash for OptionSpread {
258 fn hash<H: Hasher>(&self, state: &mut H) {
259 self.id.hash(state);
260 }
261}
262
263impl Instrument for OptionSpread {
264 fn into_any(self) -> InstrumentAny {
265 InstrumentAny::OptionSpread(self)
266 }
267
268 fn id(&self) -> InstrumentId {
269 self.id
270 }
271
272 fn raw_symbol(&self) -> Symbol {
273 self.raw_symbol
274 }
275
276 fn asset_class(&self) -> AssetClass {
277 self.asset_class
278 }
279
280 fn instrument_class(&self) -> InstrumentClass {
281 InstrumentClass::OptionSpread
282 }
283 fn underlying(&self) -> Option<Ustr> {
284 Some(self.underlying)
285 }
286
287 fn base_currency(&self) -> Option<Currency> {
288 None
289 }
290
291 fn quote_currency(&self) -> Currency {
292 self.currency
293 }
294
295 fn settlement_currency(&self) -> Currency {
296 self.currency
297 }
298
299 fn isin(&self) -> Option<Ustr> {
300 None
301 }
302
303 fn option_kind(&self) -> Option<OptionKind> {
304 None
305 }
306
307 fn exchange(&self) -> Option<Ustr> {
308 self.exchange
309 }
310
311 fn strike_price(&self) -> Option<Price> {
312 None
313 }
314
315 fn strategy_type(&self) -> Option<Ustr> {
316 Some(self.strategy_type)
317 }
318
319 fn activation_ns(&self) -> Option<UnixNanos> {
320 Some(self.activation_ns)
321 }
322
323 fn expiration_ns(&self) -> Option<UnixNanos> {
324 Some(self.expiration_ns)
325 }
326
327 fn is_inverse(&self) -> bool {
328 false
329 }
330
331 fn price_precision(&self) -> u8 {
332 self.price_precision
333 }
334
335 fn size_precision(&self) -> u8 {
336 0 }
338
339 fn price_increment(&self) -> Price {
340 self.price_increment
341 }
342
343 fn size_increment(&self) -> Quantity {
344 Quantity::from(1)
345 }
346
347 fn multiplier(&self) -> Quantity {
348 self.multiplier
349 }
350
351 fn lot_size(&self) -> Option<Quantity> {
352 Some(self.lot_size)
353 }
354
355 fn max_quantity(&self) -> Option<Quantity> {
356 self.max_quantity
357 }
358
359 fn min_quantity(&self) -> Option<Quantity> {
360 self.min_quantity
361 }
362
363 fn max_notional(&self) -> Option<Money> {
364 None
365 }
366
367 fn min_notional(&self) -> Option<Money> {
368 None
369 }
370
371 fn max_price(&self) -> Option<Price> {
372 self.max_price
373 }
374
375 fn min_price(&self) -> Option<Price> {
376 self.min_price
377 }
378
379 fn tick_scheme(&self) -> Option<Ustr> {
380 self.tick_scheme
381 }
382
383 fn info(&self) -> Option<&Params> {
384 self.info.as_ref()
385 }
386
387 fn ts_event(&self) -> UnixNanos {
388 self.ts_event
389 }
390
391 fn ts_init(&self) -> UnixNanos {
392 self.ts_init
393 }
394
395 fn margin_init(&self) -> Decimal {
396 self.margin_init
397 }
398
399 fn margin_maint(&self) -> Decimal {
400 self.margin_maint
401 }
402
403 fn maker_fee(&self) -> Decimal {
404 self.maker_fee
405 }
406
407 fn taker_fee(&self) -> Decimal {
408 self.taker_fee
409 }
410}
411
412#[cfg(test)]
413mod tests {
414 use rstest::rstest;
415 use rust_decimal_macros::dec;
416 use ustr::Ustr;
417
418 use crate::{
419 enums::{AssetClass, InstrumentClass},
420 identifiers::{InstrumentId, Symbol},
421 instruments::{Instrument, OptionSpread, stubs::*},
422 types::{Currency, Price, Quantity},
423 };
424
425 #[rstest]
426 fn test_trait_accessors(option_spread: OptionSpread) {
427 assert_eq!(
428 option_spread.id(),
429 InstrumentId::from("UD:U$: GN 2534559.GLBX")
430 );
431 assert_eq!(option_spread.asset_class(), AssetClass::FX);
432 assert_eq!(
433 option_spread.instrument_class(),
434 InstrumentClass::OptionSpread
435 );
436 assert_eq!(option_spread.quote_currency(), Currency::USD());
437 assert!(!option_spread.is_inverse());
438 assert_eq!(option_spread.exchange(), Some(Ustr::from("XCME")));
439 assert_eq!(option_spread.size_precision(), 0);
440 assert_eq!(option_spread.size_increment(), Quantity::from("1"));
441 assert_eq!(option_spread.min_quantity(), Some(Quantity::from("1")));
442 }
443
444 #[rstest]
445 fn test_new_checked_price_precision_mismatch() {
446 let result = OptionSpread::new_checked(
447 InstrumentId::from("TEST.GLBX"),
448 Symbol::from("TEST"),
449 AssetClass::FX,
450 Some(Ustr::from("XCME")),
451 Ustr::from("SR3"),
452 Ustr::from("GN"),
453 0.into(),
454 0.into(),
455 Currency::USD(),
456 4, Price::from("0.01"),
458 Quantity::from(1),
459 Quantity::from(1),
460 None,
461 None,
462 None,
463 None,
464 None,
465 None,
466 None,
467 None,
468 None,
469 None,
470 0.into(),
471 0.into(),
472 );
473 assert!(result.is_err());
474 }
475
476 #[rstest]
477 fn test_serialization_roundtrip(option_spread: OptionSpread) {
478 let json = serde_json::to_string(&option_spread).unwrap();
479 let deserialized: OptionSpread = serde_json::from_str(&json).unwrap();
480 assert_eq!(json, serde_json::to_string(&deserialized).unwrap());
481 }
482
483 #[rstest]
484 fn test_builder_matches_new_checked() {
485 let positional = OptionSpread::new_checked(
486 InstrumentId::from("UD:U$: GN 2534559.GLBX"),
487 Symbol::from("UD:U$: GN 2534559"),
488 AssetClass::FX,
489 Some(Ustr::from("XCME")),
490 Ustr::from("SR3"),
491 Ustr::from("GN"),
492 1.into(),
493 2.into(),
494 Currency::USD(),
495 2,
496 Price::from("0.01"),
497 Quantity::from(10),
498 Quantity::from(5),
499 Some(Quantity::from("100")),
500 Some(Quantity::from("1")),
501 Some(Price::from("999.0")),
502 Some(Price::from("1.0")),
503 Some(dec!(0.01)),
504 Some(dec!(0.02)),
505 Some(dec!(0.0002)),
506 Some(dec!(0.0004)),
507 None,
508 None,
509 3.into(),
510 4.into(),
511 )
512 .unwrap();
513
514 let built = OptionSpread::builder()
515 .instrument_id(InstrumentId::from("UD:U$: GN 2534559.GLBX"))
516 .raw_symbol(Symbol::from("UD:U$: GN 2534559"))
517 .asset_class(AssetClass::FX)
518 .exchange(Ustr::from("XCME"))
519 .underlying(Ustr::from("SR3"))
520 .strategy_type(Ustr::from("GN"))
521 .activation_ns(1.into())
522 .expiration_ns(2.into())
523 .currency(Currency::USD())
524 .price_precision(2)
525 .price_increment(Price::from("0.01"))
526 .multiplier(Quantity::from(10))
527 .lot_size(Quantity::from(5))
528 .max_quantity(Quantity::from("100"))
529 .min_quantity(Quantity::from("1"))
530 .max_price(Price::from("999.0"))
531 .min_price(Price::from("1.0"))
532 .margin_init(dec!(0.01))
533 .margin_maint(dec!(0.02))
534 .maker_fee(dec!(0.0002))
535 .taker_fee(dec!(0.0004))
536 .ts_event(3.into())
537 .ts_init(4.into())
538 .build()
539 .unwrap();
540
541 assert_eq!(
542 serde_json::to_value(&positional).unwrap(),
543 serde_json::to_value(&built).unwrap(),
544 );
545 }
546}