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.map(|u| u.as_str()), stringify!(exchange))?;
140 check_valid_string_ascii(strategy_type.as_str(), 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 tick_scheme(&self) -> Option<Ustr> {
265 self.tick_scheme
266 }
267 fn into_any(self) -> InstrumentAny {
268 InstrumentAny::OptionSpread(self)
269 }
270
271 fn id(&self) -> InstrumentId {
272 self.id
273 }
274
275 fn raw_symbol(&self) -> Symbol {
276 self.raw_symbol
277 }
278
279 fn asset_class(&self) -> AssetClass {
280 self.asset_class
281 }
282
283 fn instrument_class(&self) -> InstrumentClass {
284 InstrumentClass::OptionSpread
285 }
286 fn underlying(&self) -> Option<Ustr> {
287 Some(self.underlying)
288 }
289
290 fn base_currency(&self) -> Option<Currency> {
291 None
292 }
293
294 fn quote_currency(&self) -> Currency {
295 self.currency
296 }
297
298 fn settlement_currency(&self) -> Currency {
299 self.currency
300 }
301
302 fn isin(&self) -> Option<Ustr> {
303 None
304 }
305
306 fn option_kind(&self) -> Option<OptionKind> {
307 None
308 }
309
310 fn exchange(&self) -> Option<Ustr> {
311 self.exchange
312 }
313
314 fn strike_price(&self) -> Option<Price> {
315 None
316 }
317
318 fn strategy_type(&self) -> Option<Ustr> {
319 Some(self.strategy_type)
320 }
321
322 fn activation_ns(&self) -> Option<UnixNanos> {
323 Some(self.activation_ns)
324 }
325
326 fn expiration_ns(&self) -> Option<UnixNanos> {
327 Some(self.expiration_ns)
328 }
329
330 fn is_inverse(&self) -> bool {
331 false
332 }
333
334 fn price_precision(&self) -> u8 {
335 self.price_precision
336 }
337
338 fn size_precision(&self) -> u8 {
339 0 }
341
342 fn price_increment(&self) -> Price {
343 self.price_increment
344 }
345
346 fn size_increment(&self) -> Quantity {
347 Quantity::from(1)
348 }
349
350 fn multiplier(&self) -> Quantity {
351 self.multiplier
352 }
353
354 fn lot_size(&self) -> Option<Quantity> {
355 Some(self.lot_size)
356 }
357
358 fn max_quantity(&self) -> Option<Quantity> {
359 self.max_quantity
360 }
361
362 fn min_quantity(&self) -> Option<Quantity> {
363 self.min_quantity
364 }
365
366 fn max_notional(&self) -> Option<Money> {
367 None
368 }
369
370 fn min_notional(&self) -> Option<Money> {
371 None
372 }
373
374 fn max_price(&self) -> Option<Price> {
375 self.max_price
376 }
377
378 fn min_price(&self) -> Option<Price> {
379 self.min_price
380 }
381
382 fn ts_event(&self) -> UnixNanos {
383 self.ts_event
384 }
385
386 fn ts_init(&self) -> UnixNanos {
387 self.ts_init
388 }
389
390 fn margin_init(&self) -> Decimal {
391 self.margin_init
392 }
393
394 fn margin_maint(&self) -> Decimal {
395 self.margin_maint
396 }
397
398 fn maker_fee(&self) -> Decimal {
399 self.maker_fee
400 }
401
402 fn taker_fee(&self) -> Decimal {
403 self.taker_fee
404 }
405}
406
407#[cfg(test)]
408mod tests {
409 use rstest::rstest;
410 use rust_decimal_macros::dec;
411 use ustr::Ustr;
412
413 use crate::{
414 enums::{AssetClass, InstrumentClass},
415 identifiers::{InstrumentId, Symbol},
416 instruments::{Instrument, OptionSpread, stubs::*},
417 types::{Currency, Price, Quantity},
418 };
419
420 #[rstest]
421 fn test_trait_accessors(option_spread: OptionSpread) {
422 assert_eq!(
423 option_spread.id(),
424 InstrumentId::from("UD:U$: GN 2534559.GLBX")
425 );
426 assert_eq!(option_spread.asset_class(), AssetClass::FX);
427 assert_eq!(
428 option_spread.instrument_class(),
429 InstrumentClass::OptionSpread
430 );
431 assert_eq!(option_spread.quote_currency(), Currency::USD());
432 assert!(!option_spread.is_inverse());
433 assert_eq!(option_spread.exchange(), Some(Ustr::from("XCME")));
434 assert_eq!(option_spread.size_precision(), 0);
435 assert_eq!(option_spread.size_increment(), Quantity::from("1"));
436 assert_eq!(option_spread.min_quantity(), Some(Quantity::from("1")));
437 }
438
439 #[rstest]
440 fn test_new_checked_price_precision_mismatch() {
441 let result = OptionSpread::new_checked(
442 InstrumentId::from("TEST.GLBX"),
443 Symbol::from("TEST"),
444 AssetClass::FX,
445 Some(Ustr::from("XCME")),
446 Ustr::from("SR3"),
447 Ustr::from("GN"),
448 0.into(),
449 0.into(),
450 Currency::USD(),
451 4, Price::from("0.01"),
453 Quantity::from(1),
454 Quantity::from(1),
455 None,
456 None,
457 None,
458 None,
459 None,
460 None,
461 None,
462 None,
463 None,
464 None,
465 0.into(),
466 0.into(),
467 );
468 assert!(result.is_err());
469 }
470
471 #[rstest]
472 fn test_serialization_roundtrip(option_spread: OptionSpread) {
473 let json = serde_json::to_string(&option_spread).unwrap();
474 let deserialized: OptionSpread = serde_json::from_str(&json).unwrap();
475 assert_eq!(json, serde_json::to_string(&deserialized).unwrap());
476 }
477
478 #[rstest]
479 fn test_builder_matches_new_checked() {
480 let positional = OptionSpread::new_checked(
481 InstrumentId::from("UD:U$: GN 2534559.GLBX"),
482 Symbol::from("UD:U$: GN 2534559"),
483 AssetClass::FX,
484 Some(Ustr::from("XCME")),
485 Ustr::from("SR3"),
486 Ustr::from("GN"),
487 1.into(),
488 2.into(),
489 Currency::USD(),
490 2,
491 Price::from("0.01"),
492 Quantity::from(10),
493 Quantity::from(5),
494 Some(Quantity::from("100")),
495 Some(Quantity::from("1")),
496 Some(Price::from("999.0")),
497 Some(Price::from("1.0")),
498 Some(dec!(0.01)),
499 Some(dec!(0.02)),
500 Some(dec!(0.0002)),
501 Some(dec!(0.0004)),
502 None,
503 None,
504 3.into(),
505 4.into(),
506 )
507 .unwrap();
508
509 let built = OptionSpread::builder()
510 .instrument_id(InstrumentId::from("UD:U$: GN 2534559.GLBX"))
511 .raw_symbol(Symbol::from("UD:U$: GN 2534559"))
512 .asset_class(AssetClass::FX)
513 .exchange(Ustr::from("XCME"))
514 .underlying(Ustr::from("SR3"))
515 .strategy_type(Ustr::from("GN"))
516 .activation_ns(1.into())
517 .expiration_ns(2.into())
518 .currency(Currency::USD())
519 .price_precision(2)
520 .price_increment(Price::from("0.01"))
521 .multiplier(Quantity::from(10))
522 .lot_size(Quantity::from(5))
523 .max_quantity(Quantity::from("100"))
524 .min_quantity(Quantity::from("1"))
525 .max_price(Price::from("999.0"))
526 .min_price(Price::from("1.0"))
527 .margin_init(dec!(0.01))
528 .margin_maint(dec!(0.02))
529 .maker_fee(dec!(0.0002))
530 .taker_fee(dec!(0.0004))
531 .ts_event(3.into())
532 .ts_init(4.into())
533 .build()
534 .unwrap();
535
536 assert_eq!(
537 serde_json::to_value(&positional).unwrap(),
538 serde_json::to_value(&built).unwrap(),
539 );
540 }
541}