optionstratlib 0.16.5

OptionStratLib is a comprehensive Rust library for options trading and strategy development across multiple asset classes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 24/12/25
******************************************************************************/

//! # Leg Enum Module
//!
//! This module provides the `Leg` enum that unifies different position types
//! (options, spot, futures, perpetuals) into a single type for use in
//! multi-instrument strategies.
//!
//! ## Supported Leg Types
//!
//! - `Option` - Standard option positions (Call/Put)
//! - `Spot` - Direct ownership of underlying assets
//! - `Future` - Exchange-traded futures contracts
//! - `Perpetual` - Crypto perpetual swap contracts
//!
//! ## Example
//!
//! ```rust
//! use optionstratlib::model::leg::{Leg, SpotPosition};
//! use optionstratlib::model::Position;
//! use optionstratlib::model::types::Side;
//! use positive::{pos_or_panic,Positive};
//!
//! // Create a spot leg
//! let spot = SpotPosition::long("AAPL".to_string(), Positive::HUNDRED, pos_or_panic!(150.0));
//! let spot_leg = Leg::Spot(spot);
//!
//! // Check leg type
//! assert!(spot_leg.is_spot());
//! ```

use crate::error::GreeksError;
use crate::model::leg::future::FuturePosition;
use crate::model::leg::perpetual::PerpetualPosition;
use crate::model::leg::spot::SpotPosition;
use crate::model::leg::traits::LegAble;
use crate::model::position::Position;
use crate::model::types::Side;
use positive::Positive;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

/// Represents different types of legs in a trading strategy.
///
/// This enum provides a unified interface for handling various instrument types
/// in multi-leg strategies, enabling strategies like Covered Call (spot + option),
/// Cash & Carry (spot + perpetual), and other combinations.
///
/// # Variants
///
/// * `Option` - Standard option position (Call or Put)
/// * `Spot` - Direct ownership of underlying asset
/// * `Future` - Exchange-traded futures contract
/// * `Perpetual` - Crypto perpetual swap contract
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum Leg {
    /// Standard option position (Call/Put).
    Option(Box<Position>),

    /// Spot/underlying asset position.
    Spot(SpotPosition),

    /// Exchange-traded futures contract.
    Future(FuturePosition),

    /// Crypto perpetual swap contract.
    Perpetual(PerpetualPosition),
}

impl Leg {
    /// Creates a new option leg from a Position.
    #[must_use]
    pub fn option(position: Position) -> Self {
        Self::Option(Box::new(position))
    }

    /// Creates a new spot leg from a SpotPosition.
    #[must_use]
    pub fn spot(position: SpotPosition) -> Self {
        Self::Spot(position)
    }

    /// Creates a new future leg from a FuturePosition.
    #[must_use]
    pub fn future(position: FuturePosition) -> Self {
        Self::Future(position)
    }

    /// Creates a new perpetual leg from a PerpetualPosition.
    #[must_use]
    pub fn perpetual(position: PerpetualPosition) -> Self {
        Self::Perpetual(position)
    }

    /// Returns true if this is an option leg.
    #[must_use]
    pub fn is_option(&self) -> bool {
        matches!(self, Self::Option(_))
    }

    /// Returns true if this is a spot leg.
    #[must_use]
    pub fn is_spot(&self) -> bool {
        matches!(self, Self::Spot(_))
    }

    /// Returns true if this is a future leg.
    #[must_use]
    pub fn is_future(&self) -> bool {
        matches!(self, Self::Future(_))
    }

    /// Returns true if this is a perpetual leg.
    #[must_use]
    pub fn is_perpetual(&self) -> bool {
        matches!(self, Self::Perpetual(_))
    }

    /// Returns true if this is a linear instrument (spot, future, perpetual).
    ///
    /// Linear instruments have constant delta and zero gamma.
    #[must_use]
    pub fn is_linear(&self) -> bool {
        !self.is_option()
    }

    /// Returns true if this is a derivative (option, future, perpetual).
    #[must_use]
    pub fn is_derivative(&self) -> bool {
        !self.is_spot()
    }

    /// Returns true if this leg has margin requirements.
    #[must_use]
    pub fn is_margined(&self) -> bool {
        matches!(self, Self::Future(_) | Self::Perpetual(_))
    }

    /// Returns true if this leg has an expiration date.
    #[must_use]
    pub fn has_expiration(&self) -> bool {
        matches!(self, Self::Option(_) | Self::Future(_))
    }

    /// Returns the underlying option position if this is an Option leg.
    #[must_use]
    pub fn as_option(&self) -> Option<&Position> {
        match self {
            Self::Option(pos) => Some(pos),
            _ => None,
        }
    }

    /// Returns the underlying spot position if this is a Spot leg.
    #[must_use]
    pub fn as_spot(&self) -> Option<&SpotPosition> {
        match self {
            Self::Spot(pos) => Some(pos),
            _ => None,
        }
    }

    /// Returns the underlying future position if this is a Future leg.
    #[must_use]
    pub fn as_future(&self) -> Option<&FuturePosition> {
        match self {
            Self::Future(pos) => Some(pos),
            _ => None,
        }
    }

    /// Returns the underlying perpetual position if this is a Perpetual leg.
    #[must_use]
    pub fn as_perpetual(&self) -> Option<&PerpetualPosition> {
        match self {
            Self::Perpetual(pos) => Some(pos),
            _ => None,
        }
    }

    /// Returns a mutable reference to the underlying option position.
    #[must_use]
    pub fn as_option_mut(&mut self) -> Option<&mut Position> {
        match self {
            Self::Option(pos) => Some(pos),
            _ => None,
        }
    }

    /// Returns a mutable reference to the underlying spot position.
    #[must_use]
    pub fn as_spot_mut(&mut self) -> Option<&mut SpotPosition> {
        match self {
            Self::Spot(pos) => Some(pos),
            _ => None,
        }
    }

    /// Returns a mutable reference to the underlying future position.
    #[must_use]
    pub fn as_future_mut(&mut self) -> Option<&mut FuturePosition> {
        match self {
            Self::Future(pos) => Some(pos),
            _ => None,
        }
    }

    /// Returns a mutable reference to the underlying perpetual position.
    #[must_use]
    pub fn as_perpetual_mut(&mut self) -> Option<&mut PerpetualPosition> {
        match self {
            Self::Perpetual(pos) => Some(pos),
            _ => None,
        }
    }

    /// Returns a string describing the leg type.
    #[must_use]
    pub fn leg_type_name(&self) -> &'static str {
        match self {
            Self::Option(_) => "Option",
            Self::Spot(_) => "Spot",
            Self::Future(_) => "Future",
            Self::Perpetual(_) => "Perpetual",
        }
    }
}

impl LegAble for Leg {
    fn get_symbol(&self) -> &str {
        match self {
            Self::Option(pos) => &pos.option.underlying_symbol,
            Self::Spot(pos) => pos.get_symbol(),
            Self::Future(pos) => pos.get_symbol(),
            Self::Perpetual(pos) => pos.get_symbol(),
        }
    }

    fn get_quantity(&self) -> Positive {
        match self {
            Self::Option(pos) => pos.option.quantity,
            Self::Spot(pos) => pos.get_quantity(),
            Self::Future(pos) => pos.get_quantity(),
            Self::Perpetual(pos) => pos.get_quantity(),
        }
    }

    fn get_side(&self) -> Side {
        match self {
            Self::Option(pos) => pos.option.side,
            Self::Spot(pos) => pos.get_side(),
            Self::Future(pos) => pos.get_side(),
            Self::Perpetual(pos) => pos.get_side(),
        }
    }

    fn pnl_at_price(&self, price: Positive) -> Decimal {
        match self {
            Self::Option(pos) => pos
                .pnl_at_expiration(&Some(&price))
                .unwrap_or(Decimal::ZERO),
            Self::Spot(pos) => pos.pnl_at_price(price),
            Self::Future(pos) => pos.pnl_at_price(price),
            Self::Perpetual(pos) => pos.pnl_at_price(price),
        }
    }

    fn total_cost(&self) -> Positive {
        match self {
            Self::Option(pos) => pos.total_cost().unwrap_or(Positive::ZERO),
            Self::Spot(pos) => pos.total_cost(),
            Self::Future(pos) => pos.total_cost(),
            Self::Perpetual(pos) => pos.total_cost(),
        }
    }

    fn fees(&self) -> Positive {
        match self {
            Self::Option(pos) => pos.open_fee + pos.close_fee,
            Self::Spot(pos) => pos.fees(),
            Self::Future(pos) => pos.fees(),
            Self::Perpetual(pos) => pos.fees(),
        }
    }

    fn delta(&self) -> Result<Decimal, GreeksError> {
        match self {
            Self::Option(pos) => {
                use crate::greeks::Greeks;
                pos.delta()
            }
            Self::Spot(pos) => pos.delta(),
            Self::Future(pos) => pos.delta(),
            Self::Perpetual(pos) => pos.delta(),
        }
    }

    fn gamma(&self) -> Result<Decimal, GreeksError> {
        match self {
            Self::Option(pos) => {
                use crate::greeks::Greeks;
                pos.gamma()
            }
            Self::Spot(_) | Self::Future(_) | Self::Perpetual(_) => Ok(Decimal::ZERO),
        }
    }

    fn theta(&self) -> Result<Decimal, GreeksError> {
        match self {
            Self::Option(pos) => {
                use crate::greeks::Greeks;
                pos.theta()
            }
            Self::Spot(pos) => pos.theta(),
            Self::Future(pos) => pos.theta(),
            Self::Perpetual(pos) => pos.theta(),
        }
    }

    fn vega(&self) -> Result<Decimal, GreeksError> {
        match self {
            Self::Option(pos) => {
                use crate::greeks::Greeks;
                pos.vega()
            }
            Self::Spot(_) | Self::Future(_) | Self::Perpetual(_) => Ok(Decimal::ZERO),
        }
    }

    fn rho(&self) -> Result<Decimal, GreeksError> {
        match self {
            Self::Option(pos) => {
                use crate::greeks::Greeks;
                pos.rho()
            }
            Self::Spot(pos) => pos.rho(),
            Self::Future(pos) => pos.rho(),
            Self::Perpetual(pos) => pos.rho(),
        }
    }
}

impl std::fmt::Display for Leg {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Option(pos) => write!(f, "[Option] {}", pos),
            Self::Spot(pos) => write!(f, "[Spot] {}", pos),
            Self::Future(pos) => write!(f, "[Future] {}", pos),
            Self::Perpetual(pos) => write!(f, "[Perpetual] {}", pos),
        }
    }
}

impl From<Position> for Leg {
    fn from(position: Position) -> Self {
        Self::Option(Box::new(position))
    }
}

impl From<SpotPosition> for Leg {
    fn from(position: SpotPosition) -> Self {
        Self::Spot(position)
    }
}

impl From<FuturePosition> for Leg {
    fn from(position: FuturePosition) -> Self {
        Self::Future(position)
    }
}

impl From<PerpetualPosition> for Leg {
    fn from(position: PerpetualPosition) -> Self {
        Self::Perpetual(position)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::OptionStyle;
    use crate::model::ExpirationDate;
    use crate::model::utils::create_sample_option_simplest;

    use chrono::Utc;
    use positive::pos_or_panic;

    fn create_test_option_position() -> Position {
        let option = create_sample_option_simplest(OptionStyle::Call, Side::Long);
        Position::new(
            option,
            pos_or_panic!(5.0),
            Utc::now(),
            pos_or_panic!(0.5),
            pos_or_panic!(0.5),
            None,
            None,
        )
    }

    fn create_test_spot_position() -> SpotPosition {
        SpotPosition::long("AAPL".to_string(), Positive::HUNDRED, pos_or_panic!(150.0))
    }

    fn create_test_future_position() -> FuturePosition {
        FuturePosition::long(
            "ES".to_string(),
            Positive::ONE,
            pos_or_panic!(4500.0),
            ExpirationDate::Days(pos_or_panic!(30.0)),
            pos_or_panic!(50.0),
            pos_or_panic!(15000.0),
        )
    }

    fn create_test_perpetual_position() -> PerpetualPosition {
        PerpetualPosition::long(
            "BTC-USDT-PERP".to_string(),
            Positive::ONE,
            pos_or_panic!(50000.0),
            pos_or_panic!(10.0),
            pos_or_panic!(5000.0),
        )
    }

    #[test]
    fn test_leg_option_creation() {
        let pos = create_test_option_position();
        let leg = Leg::option(pos.clone());

        assert!(leg.is_option());
        assert!(!leg.is_spot());
        assert!(!leg.is_future());
        assert!(!leg.is_perpetual());
        assert!(!leg.is_linear());
        assert!(leg.is_derivative());
        assert!(leg.has_expiration());
    }

    #[test]
    fn test_leg_spot_creation() {
        let pos = create_test_spot_position();
        let leg = Leg::spot(pos);

        assert!(!leg.is_option());
        assert!(leg.is_spot());
        assert!(!leg.is_future());
        assert!(!leg.is_perpetual());
        assert!(leg.is_linear());
        assert!(!leg.is_derivative());
        assert!(!leg.has_expiration());
        assert!(!leg.is_margined());
    }

    #[test]
    fn test_leg_future_creation() {
        let pos = create_test_future_position();
        let leg = Leg::future(pos);

        assert!(!leg.is_option());
        assert!(!leg.is_spot());
        assert!(leg.is_future());
        assert!(!leg.is_perpetual());
        assert!(leg.is_linear());
        assert!(leg.is_derivative());
        assert!(leg.has_expiration());
        assert!(leg.is_margined());
    }

    #[test]
    fn test_leg_perpetual_creation() {
        let pos = create_test_perpetual_position();
        let leg = Leg::perpetual(pos);

        assert!(!leg.is_option());
        assert!(!leg.is_spot());
        assert!(!leg.is_future());
        assert!(leg.is_perpetual());
        assert!(leg.is_linear());
        assert!(leg.is_derivative());
        assert!(!leg.has_expiration());
        assert!(leg.is_margined());
    }

    #[test]
    fn test_leg_from_position() {
        let pos = create_test_option_position();
        let leg: Leg = pos.into();
        assert!(leg.is_option());
    }

    #[test]
    fn test_leg_from_spot() {
        let pos = create_test_spot_position();
        let leg: Leg = pos.into();
        assert!(leg.is_spot());
    }

    #[test]
    fn test_leg_from_future() {
        let pos = create_test_future_position();
        let leg: Leg = pos.into();
        assert!(leg.is_future());
    }

    #[test]
    fn test_leg_from_perpetual() {
        let pos = create_test_perpetual_position();
        let leg: Leg = pos.into();
        assert!(leg.is_perpetual());
    }

    #[test]
    fn test_leg_as_option() {
        let pos = create_test_option_position();
        let leg = Leg::option(pos);

        assert!(leg.as_option().is_some());
        assert!(leg.as_spot().is_none());
        assert!(leg.as_future().is_none());
        assert!(leg.as_perpetual().is_none());
    }

    #[test]
    fn test_leg_as_spot() {
        let pos = create_test_spot_position();
        let leg = Leg::spot(pos);

        assert!(leg.as_option().is_none());
        assert!(leg.as_spot().is_some());
        assert!(leg.as_future().is_none());
        assert!(leg.as_perpetual().is_none());
    }

    #[test]
    fn test_leg_get_symbol() {
        let spot = create_test_spot_position();
        let leg = Leg::spot(spot);
        assert_eq!(leg.get_symbol(), "AAPL");

        let future = create_test_future_position();
        let leg = Leg::future(future);
        assert_eq!(leg.get_symbol(), "ES");

        let perp = create_test_perpetual_position();
        let leg = Leg::perpetual(perp);
        assert_eq!(leg.get_symbol(), "BTC-USDT-PERP");
    }

    #[test]
    fn test_leg_get_quantity() {
        let spot = SpotPosition::long("AAPL".to_string(), Positive::HUNDRED, pos_or_panic!(150.0));
        let leg = Leg::spot(spot);
        assert_eq!(leg.get_quantity(), Positive::HUNDRED);
    }

    #[test]
    fn test_leg_get_side() {
        let long_spot =
            SpotPosition::long("AAPL".to_string(), Positive::HUNDRED, pos_or_panic!(150.0));
        let leg = Leg::spot(long_spot);
        assert_eq!(leg.get_side(), Side::Long);

        let short_spot =
            SpotPosition::short("AAPL".to_string(), Positive::HUNDRED, pos_or_panic!(150.0));
        let leg = Leg::spot(short_spot);
        assert_eq!(leg.get_side(), Side::Short);
    }

    #[test]
    fn test_leg_delta_spot() {
        let long_spot =
            SpotPosition::long("AAPL".to_string(), Positive::HUNDRED, pos_or_panic!(150.0));
        let leg = Leg::spot(long_spot);
        assert_eq!(leg.delta().unwrap(), Decimal::from(100));

        let short_spot =
            SpotPosition::short("AAPL".to_string(), Positive::HUNDRED, pos_or_panic!(150.0));
        let leg = Leg::spot(short_spot);
        assert_eq!(leg.delta().unwrap(), Decimal::from(-100));
    }

    #[test]
    fn test_leg_gamma_linear() {
        let spot = create_test_spot_position();
        let leg = Leg::spot(spot);
        assert_eq!(leg.gamma().unwrap(), Decimal::ZERO);

        let future = create_test_future_position();
        let leg = Leg::future(future);
        assert_eq!(leg.gamma().unwrap(), Decimal::ZERO);

        let perp = create_test_perpetual_position();
        let leg = Leg::perpetual(perp);
        assert_eq!(leg.gamma().unwrap(), Decimal::ZERO);
    }

    #[test]
    fn test_leg_pnl_spot() {
        let spot = SpotPosition::long("AAPL".to_string(), Positive::HUNDRED, pos_or_panic!(150.0));
        let leg = Leg::spot(spot);

        let pnl = leg.pnl_at_price(pos_or_panic!(160.0));
        assert_eq!(pnl, Decimal::from(1000));
    }

    #[test]
    fn test_leg_type_name() {
        let option_leg = Leg::option(create_test_option_position());
        assert_eq!(option_leg.leg_type_name(), "Option");

        let spot_leg = Leg::spot(create_test_spot_position());
        assert_eq!(spot_leg.leg_type_name(), "Spot");

        let future_leg = Leg::future(create_test_future_position());
        assert_eq!(future_leg.leg_type_name(), "Future");

        let perp_leg = Leg::perpetual(create_test_perpetual_position());
        assert_eq!(perp_leg.leg_type_name(), "Perpetual");
    }

    #[test]
    fn test_leg_display() {
        let spot = create_test_spot_position();
        let leg = Leg::spot(spot);
        let display = format!("{}", leg);
        assert!(display.contains("[Spot]"));
        assert!(display.contains("AAPL"));
    }

    #[test]
    fn test_leg_total_cost() {
        let spot = SpotPosition::new(
            "AAPL".to_string(),
            Positive::HUNDRED,
            pos_or_panic!(150.0),
            Side::Long,
            Utc::now(),
            pos_or_panic!(10.0),
            pos_or_panic!(10.0),
        );
        let leg = Leg::spot(spot);
        assert_eq!(leg.total_cost(), pos_or_panic!(15020.0));
    }

    #[test]
    fn test_leg_fees() {
        let spot = SpotPosition::new(
            "AAPL".to_string(),
            Positive::HUNDRED,
            pos_or_panic!(150.0),
            Side::Long,
            Utc::now(),
            pos_or_panic!(10.0),
            pos_or_panic!(15.0),
        );
        let leg = Leg::spot(spot);
        assert_eq!(leg.fees(), pos_or_panic!(25.0));
    }
}