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 FuturesSpread {
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 FuturesSpread {
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 FuturesSpread {
250 fn eq(&self, other: &Self) -> bool {
251 self.id == other.id
252 }
253}
254
255impl Eq for FuturesSpread {}
256
257impl Hash for FuturesSpread {
258 fn hash<H: Hasher>(&self, state: &mut H) {
259 self.id.hash(state);
260 }
261}
262
263impl Instrument for FuturesSpread {
264 fn tick_scheme(&self) -> Option<Ustr> {
265 self.tick_scheme
266 }
267 fn into_any(self) -> InstrumentAny {
268 InstrumentAny::FuturesSpread(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::FuturesSpread
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::{FuturesSpread, Instrument, stubs::*},
417 types::{Currency, Price, Quantity},
418 };
419
420 #[rstest]
421 fn test_trait_accessors(futures_spread_es: FuturesSpread) {
422 assert_eq!(futures_spread_es.id(), InstrumentId::from("ESM4-ESU4.GLBX"));
423 assert_eq!(futures_spread_es.asset_class(), AssetClass::Index);
424 assert_eq!(
425 futures_spread_es.instrument_class(),
426 InstrumentClass::FuturesSpread
427 );
428 assert_eq!(futures_spread_es.quote_currency(), Currency::USD());
429 assert!(!futures_spread_es.is_inverse());
430 assert_eq!(futures_spread_es.exchange(), Some(Ustr::from("XCME")));
431 assert_eq!(futures_spread_es.size_precision(), 0);
432 assert_eq!(futures_spread_es.size_increment(), Quantity::from("1"));
433 assert_eq!(futures_spread_es.min_quantity(), Some(Quantity::from("1")));
434 assert!(futures_spread_es.activation_ns().is_some());
435 assert!(futures_spread_es.expiration_ns().is_some());
436 }
437
438 #[rstest]
439 fn test_new_checked_price_precision_mismatch() {
440 let result = FuturesSpread::new_checked(
441 InstrumentId::from("TEST.GLBX"),
442 Symbol::from("TEST"),
443 AssetClass::Index,
444 Some(Ustr::from("XCME")),
445 Ustr::from("ES"),
446 Ustr::from("EQ"),
447 0.into(),
448 0.into(),
449 Currency::USD(),
450 4, Price::from("0.01"),
452 Quantity::from(1),
453 Quantity::from(1),
454 None,
455 None,
456 None,
457 None,
458 None,
459 None,
460 None,
461 None,
462 None,
463 None,
464 0.into(),
465 0.into(),
466 );
467 assert!(result.is_err());
468 }
469
470 #[rstest]
471 fn test_new_checked_zero_multiplier() {
472 let result = FuturesSpread::new_checked(
473 InstrumentId::from("TEST.GLBX"),
474 Symbol::from("TEST"),
475 AssetClass::Index,
476 Some(Ustr::from("XCME")),
477 Ustr::from("ES"),
478 Ustr::from("EQ"),
479 0.into(),
480 0.into(),
481 Currency::USD(),
482 2,
483 Price::from("0.01"),
484 Quantity::from("0"), Quantity::from(1),
486 None,
487 None,
488 None,
489 None,
490 None,
491 None,
492 None,
493 None,
494 None,
495 None,
496 0.into(),
497 0.into(),
498 );
499 assert!(result.is_err());
500 }
501
502 #[rstest]
503 fn test_serialization_roundtrip(futures_spread_es: FuturesSpread) {
504 let json = serde_json::to_string(&futures_spread_es).unwrap();
505 let deserialized: FuturesSpread = serde_json::from_str(&json).unwrap();
506 assert_eq!(json, serde_json::to_string(&deserialized).unwrap());
507 }
508
509 #[rstest]
510 fn test_builder_matches_new_checked() {
511 let positional = FuturesSpread::new_checked(
512 InstrumentId::from("ESM4-ESU4.GLBX"),
513 Symbol::from("ESM4-ESU4"),
514 AssetClass::Index,
515 Some(Ustr::from("XCME")),
516 Ustr::from("ES"),
517 Ustr::from("EQ"),
518 1_000.into(),
519 2_000.into(),
520 Currency::USD(),
521 2,
522 Price::from("0.01"),
523 Quantity::from(50),
524 Quantity::from(10),
525 Some(Quantity::from("10000")),
526 Some(Quantity::from("5")),
527 Some(Price::from("9999.99")),
528 Some(Price::from("0.01")),
529 Some(dec!(0.01)),
530 Some(dec!(0.02)),
531 Some(dec!(0.0002)),
532 Some(dec!(0.0004)),
533 None,
534 None,
535 1.into(),
536 2.into(),
537 )
538 .unwrap();
539
540 let built = FuturesSpread::builder()
541 .instrument_id(InstrumentId::from("ESM4-ESU4.GLBX"))
542 .raw_symbol(Symbol::from("ESM4-ESU4"))
543 .asset_class(AssetClass::Index)
544 .exchange(Ustr::from("XCME"))
545 .underlying(Ustr::from("ES"))
546 .strategy_type(Ustr::from("EQ"))
547 .activation_ns(1_000.into())
548 .expiration_ns(2_000.into())
549 .currency(Currency::USD())
550 .price_precision(2)
551 .price_increment(Price::from("0.01"))
552 .multiplier(Quantity::from(50))
553 .lot_size(Quantity::from(10))
554 .max_quantity(Quantity::from("10000"))
555 .min_quantity(Quantity::from("5"))
556 .max_price(Price::from("9999.99"))
557 .min_price(Price::from("0.01"))
558 .margin_init(dec!(0.01))
559 .margin_maint(dec!(0.02))
560 .maker_fee(dec!(0.0002))
561 .taker_fee(dec!(0.0004))
562 .ts_event(1.into())
563 .ts_init(2.into())
564 .build()
565 .unwrap();
566
567 assert_eq!(
568 serde_json::to_value(&positional).unwrap(),
569 serde_json::to_value(&built).unwrap(),
570 );
571 }
572}