optionstratlib 0.18.0

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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 27/3/25
******************************************************************************/
use crate::ExpirationDate;
use crate::chains::OptionChain;
use crate::chains::utils::OptionChainBuildParams;
use crate::error::ChainError;
use crate::simulation::steps::Step;
use crate::simulation::{WalkParams, walk_steps_par};
use core::option::Option;
use positive::Positive;
#[cfg(test)]
use positive::pos_or_panic;
use rust_decimal_macros::dec;
use std::sync::Mutex;

/// Creates a new `OptionChain` from pre-derived build parameters and a new price.
///
/// This function clones the provided build parameters and overrides the underlying
/// price, and optionally the implied volatility and expiration date, before building
/// the new chain. Deriving the parameters once (outside the simulation loop) instead
/// of from each freshly built chain keeps every step's chain shape anchored to the
/// initial chain and avoids re-scanning the chain on every step.
///
/// Greeks (delta/gamma) are computed by `OptionChain::build_chain` itself for every
/// strike, so no separate greek pass is needed here.
///
/// # Arguments
///
/// * `build_params` - Build parameters derived from the initial `OptionChain`.
/// * `new_price` - A reference to the new underlying price.
/// * `volatility` - An optional new implied volatility.
/// * `expiration_date` - An optional new expiration date.
///
/// # Returns
///
/// * `Ok(OptionChain)` - A new `OptionChain` with the updated parameters.
/// * `Err(ChainError)` - If an error occurs during the creation of the new `OptionChain`.
///
fn create_chain_from_step(
    build_params: &OptionChainBuildParams,
    new_price: &Positive,
    volatility: Option<Positive>,
    expiration_date: Option<ExpirationDate>,
) -> Result<OptionChain, ChainError> {
    let mut chain_params = build_params.clone();
    chain_params.set_underlying_price(Some(Box::new(*new_price)));
    if let Some(volatility) = volatility {
        // `build_chain` rejects IV > 100% and IV == 0; simulated
        // stochastic-vol paths can spike above 100% or touch the zero
        // boundary (CIR truncation), so clamp into (0, 1] to keep the
        // walk alive. The floor literal is compile-time positive, so the
        // fallback branch is unreachable.
        let min_walk_iv = Positive::new_decimal(dec!(0.0001)).unwrap_or(Positive::ONE);
        let volatility = volatility.min(Positive::ONE).max(min_walk_iv);
        chain_params.set_implied_volatility(volatility);
    }
    if let Some(exp_date) = expiration_date {
        chain_params.price_params.expiration_date = Some(exp_date);
    }

    OptionChain::build_chain(&chain_params)
}

/// Generates a vector of `Step`s containing `Positive` x-values and `OptionChain` y-values.
///
/// Simulates the stochastic process selected by `walk_params.walk_type` (Brownian,
/// GeometricBrownian, LogReturns, MeanReverting, JumpDiffusion, Garch, Heston, Custom,
/// Telegraph, or Historical) for the underlying price and rebuilds the option chain at
/// each step with the new price, the step's volatility, and the decayed expiration date.
/// For constant-volatility walks the chain IV is the walk type's `volatility`
/// parameter; for stochastic-volatility walks (`Garch`, `Heston`, `Custom`,
/// `Telegraph`) it follows the simulated per-step volatility path; for `Historical`
/// it is an expanding-window estimate over the walked prices that uses no future
/// data (capped at 100% before stamping the chain).
///
/// Implemented on top of the shared walk driver
/// ([`crate::simulation::walk_steps`]); this function only supplies the chain-rebuild
/// closure.
///
/// # Contract (shared with [`crate::simulation::generator_positive`] and
/// [`crate::series::generator_optionseries`])
///
/// * The returned vector always starts with `walk_params.init_step`.
/// * If the walker yields no values beyond the initial one (e.g. a size-1 walk),
///   only the initial step is returned.
/// * The walk is truncated when the x-step reaches expiration
///   (`SimulationError::ExpirationReached`); any other step-advance error is propagated.
/// * The result is truncated to at most `walk_params.size` steps.
///
/// # Arguments
///
/// * `walk_params` - A reference to the `WalkParams` struct containing the walk parameters.
///
/// # Returns
///
/// * `Ok(Vec<Step<Positive, OptionChain>>)` - A vector of `Step`s representing the simulated walk.
/// * `Err(ChainError)` - If the underlying simulator, volatility helpers, or chain construction
///   fails for any reason.
///
/// # Errors
///
/// Returns [`ChainError::Simulation`] (via the `From<SimulationError>` conversion) if the
/// random-walk generator returns an error — including
/// `SimulationError::InsufficientHistoricalData` when a `Historical` walk has fewer
/// prices than `walk_params.size` — and propagates errors from the historical helpers
/// (`calculate_log_returns`, `constant_volatility`, `adjust_volatility`) or from
/// rebuilding the chain.
pub fn generator_optionchain(
    walk_params: &WalkParams<Positive, OptionChain>,
) -> Result<Vec<Step<Positive, OptionChain>>, ChainError> {
    // Derived lazily on the first rebuilt step so that walks that never
    // rebuild (size <= 1) do not require a parameterizable initial chain.
    // Steps are independent once the params are derived, so the per-step
    // chain builds (the dominant cost) run on the rayon pool.
    let build_params: Mutex<Option<OptionChainBuildParams>> = Mutex::new(None);
    // Capture only Sync data in the closure (the boxed walker inside
    // WalkParams is not Sync).
    let init_ystep = walk_params.ystep_ref();
    walk_steps_par(walk_params, |new_price, volatility, x_step| {
        let params = {
            let mut guard = build_params.lock().map_err(|_| {
                ChainError::invalid_parameters("build_params", "params cache lock poisoned")
            })?;
            match &*guard {
                Some(params) => params.clone(),
                // Derive the build parameters once from the initial chain;
                // every step's chain is anchored to the initial chain's
                // shape instead of feeding back params re-derived from the
                // previous rebuilt chain on each iteration.
                None => {
                    let params = init_ystep.value().to_build_params()?;
                    *guard = Some(params.clone());
                    params
                }
            }
        };
        let chain =
            create_chain_from_step(&params, new_price, volatility, Some(*x_step.datetime()))?;
        Ok(Some(chain))
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::ExpirationDate;

    use crate::simulation::randomwalk::RandomWalk;
    use crate::simulation::steps::{Xstep, Ystep};
    use crate::simulation::{WalkType, WalkTypeAble};
    use crate::utils::time::{convert_time_frame, get_x_days_formatted};
    use crate::utils::{Len, TimeFrame};
    use rust_decimal_macros::dec;

    #[test]
    fn test_create_chain_from_step() {
        let mut initial_price =
            OptionChain::load_from_json("examples/Chains/SP500-18-oct-2024-5781.88.json").unwrap();
        initial_price.update_expiration_date(get_x_days_formatted(2));
        let new_price: Positive = pos_or_panic!(5790.0);
        let step = Step {
            x: Xstep::new(
                Positive::ONE,
                TimeFrame::Minute,
                ExpirationDate::Days(pos_or_panic!(30.0)),
            ),
            y: Ystep::new(0, initial_price),
        };

        let build_params = match step.y.value().to_build_params() {
            Ok(params) => params,
            Err(e) => panic!("to_build_params failed: {e}"),
        };
        let result = create_chain_from_step(&build_params, &new_price, None, None);
        assert!(result.is_ok());

        let new_chain = result.unwrap();
        assert!(new_chain.len() > 1);
        assert_eq!(*new_chain.atm_strike().unwrap(), pos_or_panic!(5790.0));
        for option in new_chain.get_single_iter() {
            assert!(option.valid_put() || option.valid_call());
        }
    }

    #[derive(Clone)]
    struct WalkerOptionChain {}
    impl WalkerOptionChain {
        fn new() -> Self {
            WalkerOptionChain {}
        }
    }
    impl WalkTypeAble<Positive, OptionChain> for WalkerOptionChain {}

    #[test]
    fn test_create_chain_from_step_with_volatility_change() {
        let n_steps = 4;
        let mut initial_chain =
            OptionChain::load_from_json("examples/Chains/SP500-18-oct-2024-5781.88.json").unwrap();
        initial_chain.update_expiration_date(get_x_days_formatted(2));
        let days = pos_or_panic!(30.0);
        let std_dev = pos_or_panic!(20.0);
        let walker = Box::new(WalkerOptionChain::new());

        let walk_params = WalkParams {
            size: n_steps,
            init_step: Step {
                x: Xstep::new(Positive::ONE, TimeFrame::Minute, ExpirationDate::Days(days)),
                y: Ystep::new(0, initial_chain),
            },
            walk_type: WalkType::GeometricBrownian {
                dt: convert_time_frame(Positive::ONE / days, &TimeFrame::Minute, &TimeFrame::Day),
                drift: dec!(0.0),
                volatility: std_dev / 100.0,
            },
            walker,
        };

        let random_walk = RandomWalk::new(
            "Random Walk".to_string(),
            &walk_params,
            generator_optionchain,
        )
        .expect("random walk construction");
        assert_eq!(random_walk.len(), n_steps);
    }

    /// Regression for #408: under a stochastic-volatility walk the rebuilt
    /// chains must follow the simulated per-step vol path instead of being
    /// frozen at the walk's initial volatility.
    #[test]
    fn test_generator_optionchain_heston_vol_reaches_chains() {
        use crate::chains::utils::{OptionChainBuildParams, OptionDataPriceParams};

        let price_params = OptionDataPriceParams::new(
            Some(Box::new(Positive::HUNDRED)),
            Some(ExpirationDate::Days(pos_or_panic!(60.0))),
            Some(dec!(0.05)),
            Some(pos_or_panic!(0.02)),
            Some("TEST".to_string()),
        );
        let chain_params = OptionChainBuildParams::new(
            "TEST".to_string(),
            None,
            10,
            Some(pos_or_panic!(5.0)),
            dec!(-0.2),
            dec!(0.1),
            pos_or_panic!(0.02),
            2,
            price_params,
            pos_or_panic!(0.2),
        );
        let initial_chain = match OptionChain::build_chain(&chain_params) {
            Ok(chain) => chain,
            Err(e) => panic!("initial chain build failed: {e}"),
        };

        let walk_params = WalkParams {
            size: 6,
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Day,
                    ExpirationDate::Days(pos_or_panic!(60.0)),
                ),
                y: Ystep::new(0, initial_chain),
            },
            walk_type: WalkType::Heston {
                dt: pos_or_panic!(0.004),
                drift: dec!(0.0),
                volatility: pos_or_panic!(0.2),
                kappa: Positive::TWO,
                theta: pos_or_panic!(0.04),
                xi: Positive::TWO, // high vol-of-vol: the vol path must move
                rho: dec!(-0.5),
            },
            walker: Box::new(WalkerOptionChain::new()),
        };

        let steps = match generator_optionchain(&walk_params) {
            Ok(steps) => steps,
            Err(e) => panic!("generator_optionchain failed: {e}"),
        };
        assert_eq!(steps.len(), 6);

        let atm_ivs: Vec<Positive> = steps
            .iter()
            .skip(1)
            .map(|step| match step.y.value().get_atm_implied_volatility() {
                Ok(iv) => *iv,
                Err(e) => panic!("rebuilt chain has no ATM IV: {e}"),
            })
            .collect();
        let varying = atm_ivs.windows(2).any(|w| w[0] != w[1]);
        assert!(
            varying,
            "chain IVs are frozen despite a stochastic-vol walk: {atm_ivs:?}"
        );
    }

    /// Multi-step behavior under a deterministic ramp walker: rebuilt chains
    /// must track the walked price, keep the walk volatility, decay their
    /// expiration with the x-step, and increment the y index.
    #[test]
    fn test_generator_optionchain_multi_step_behavior() {
        use crate::chains::utils::{OptionChainBuildParams, OptionDataPriceParams};
        use crate::simulation::walk_test_support::RampWalker;

        let init_days = 60.0;
        let price_params = OptionDataPriceParams::new(
            Some(Box::new(Positive::HUNDRED)),
            Some(ExpirationDate::Days(pos_or_panic!(init_days))),
            Some(dec!(0.05)),
            Some(pos_or_panic!(0.02)),
            Some("TEST".to_string()),
        );
        let chain_params = OptionChainBuildParams::new(
            "TEST".to_string(),
            None,
            10,
            Some(pos_or_panic!(5.0)),
            dec!(-0.2),
            dec!(0.1),
            pos_or_panic!(0.02),
            2,
            price_params,
            pos_or_panic!(0.25),
        );
        let initial_chain = match OptionChain::build_chain(&chain_params) {
            Ok(chain) => chain,
            Err(e) => panic!("initial chain build failed: {e}"),
        };

        let size = 4;
        let walk_params = WalkParams {
            size,
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Day,
                    ExpirationDate::Days(pos_or_panic!(init_days)),
                ),
                y: Ystep::new(0, initial_chain),
            },
            walk_type: WalkType::GeometricBrownian {
                dt: pos_or_panic!(0.01),
                drift: dec!(0.0),
                volatility: pos_or_panic!(0.25),
            },
            walker: Box::new(RampWalker {
                delta: Positive::TWO,
            }),
        };

        let steps = match generator_optionchain(&walk_params) {
            Ok(steps) => steps,
            Err(e) => panic!("generator_optionchain failed: {e}"),
        };
        assert_eq!(steps.len(), size);

        for (i, step) in steps.iter().enumerate().skip(1) {
            let chain = step.y.value();
            // Underlying tracks the deterministic ramp exactly.
            let expected_price = Positive::HUNDRED + Positive::TWO * i as f64;
            assert_eq!(
                chain.underlying_price, expected_price,
                "underlying at step {i}"
            );
            // Y index increments.
            assert_eq!(*step.y.index(), i as i32, "y index at step {i}");
            // X time-to-expiry decreases one day per step.
            let x_days = match step.x.days_left() {
                Ok(days) => days,
                Err(e) => panic!("days_left failed at step {i}: {e}"),
            };
            assert_eq!(x_days, pos_or_panic!(init_days - i as f64));
            // The rebuilt chain's expiration follows the x-step (parsed back
            // from a date string, so allow a one-day rounding tolerance).
            let chain_days = match chain.get_expiration() {
                Some(exp) => match exp.get_days() {
                    Ok(days) => days,
                    Err(e) => panic!("chain expiration days failed at step {i}: {e}"),
                },
                None => panic!("rebuilt chain has no expiration at step {i}"),
            };
            let diff = (chain_days.to_dec() - x_days.to_dec()).abs();
            assert!(
                diff <= rust_decimal::Decimal::ONE,
                "chain expiration {chain_days} != x-step days {x_days} at step {i}"
            );
            // ATM IV tracks the constant walk volatility (ATM moneyness is
            // near zero, so the skew factor is ~1).
            let atm_iv = match chain.get_atm_implied_volatility() {
                Ok(iv) => *iv,
                Err(e) => panic!("ATM IV missing at step {i}: {e}"),
            };
            let iv_diff = (atm_iv.to_dec() - dec!(0.25)).abs();
            assert!(
                iv_diff < dec!(0.01),
                "ATM IV {atm_iv} does not track walk volatility at step {i}"
            );
        }
    }

    /// Historical multi-step behavior: underlying prices must replay the
    /// provided history and the final step's IV must match the historical
    /// estimate computed over exactly the walked prices (no look-ahead).
    #[test]
    fn test_generator_optionchain_historical_multi_step() {
        use crate::chains::utils::{OptionChainBuildParams, OptionDataPriceParams};
        use crate::simulation::walk_test_support::RampWalker;
        use crate::utils::others::calculate_log_returns;
        use crate::volatility::{adjust_volatility as annualize, constant_volatility};
        use rust_decimal::Decimal;

        let prices = vec![
            Positive::HUNDRED,
            pos_or_panic!(104.0),
            pos_or_panic!(98.0),
            pos_or_panic!(103.0),
            pos_or_panic!(101.0),
            pos_or_panic!(99.0), // beyond the walked slice: must not be used
        ];
        let size = 5;

        let price_params = OptionDataPriceParams::new(
            Some(Box::new(Positive::HUNDRED)),
            Some(ExpirationDate::Days(pos_or_panic!(60.0))),
            Some(dec!(0.05)),
            Some(pos_or_panic!(0.02)),
            Some("TEST".to_string()),
        );
        let chain_params = OptionChainBuildParams::new(
            "TEST".to_string(),
            None,
            10,
            Some(pos_or_panic!(5.0)),
            dec!(-0.2),
            dec!(0.1),
            pos_or_panic!(0.02),
            2,
            price_params,
            pos_or_panic!(0.25),
        );
        let initial_chain = match OptionChain::build_chain(&chain_params) {
            Ok(chain) => chain,
            Err(e) => panic!("initial chain build failed: {e}"),
        };

        let walk_params = WalkParams {
            size,
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Day,
                    ExpirationDate::Days(pos_or_panic!(60.0)),
                ),
                y: Ystep::new(0, initial_chain),
            },
            walk_type: WalkType::Historical {
                timeframe: TimeFrame::Day,
                prices: prices.clone(),
                symbol: None,
            },
            walker: Box::new(RampWalker {
                delta: Positive::ONE, // unused: Historical keeps the default
            }),
        };

        let steps = match generator_optionchain(&walk_params) {
            Ok(steps) => steps,
            Err(e) => panic!("generator_optionchain failed: {e}"),
        };
        assert_eq!(steps.len(), size);

        // Underlying prices replay the provided history.
        for (i, step) in steps.iter().enumerate().skip(1) {
            let expected = match prices.get(i) {
                Some(p) => *p,
                None => panic!("missing price {i}"),
            };
            assert_eq!(
                step.y.value().underlying_price,
                expected,
                "underlying at step {i}"
            );
        }

        // Final step vol == expanding-window estimate over the WALKED slice
        // (prices[..size]), not the full vector.
        let walked = match prices.get(..size) {
            Some(w) => w,
            None => panic!("short prices"),
        };
        let log_returns: Vec<Decimal> = match calculate_log_returns(walked) {
            Ok(returns) => returns.iter().map(|p| p.to_dec()).collect(),
            Err(e) => panic!("log returns failed: {e}"),
        };
        let expected_vol = match constant_volatility(&log_returns) {
            Ok(vol) => match annualize(vol, TimeFrame::Day, TimeFrame::Year) {
                Ok(vol) => vol,
                Err(e) => panic!("annualize failed: {e}"),
            },
            Err(e) => panic!("constant_volatility failed: {e}"),
        };
        let last_iv = match steps.last() {
            Some(step) => match step.y.value().get_atm_implied_volatility() {
                Ok(iv) => *iv,
                Err(e) => panic!("last ATM IV missing: {e}"),
            },
            None => panic!("empty steps"),
        };
        let capped_expected = expected_vol.min(Positive::ONE);
        let diff = (last_iv.to_dec() - capped_expected.to_dec()).abs();
        assert!(
            diff < dec!(0.02),
            "last-step IV {last_iv} != expected historical estimate {capped_expected}"
        );
    }

    /// Contract: an empty walker output yields the init-only walk (no panic,
    /// no error) for the chain generator too.
    #[test]
    fn test_generator_optionchain_empty_walker_output() {
        use crate::simulation::walk_test_support::EmptyWalker;
        use crate::utils::time::get_tomorrow_formatted;
        use positive::spos;

        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );
        let walk_params = WalkParams {
            size: 5,
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Day,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::GeometricBrownian {
                dt: pos_or_panic!(0.01),
                drift: dec!(0.0),
                volatility: pos_or_panic!(0.2),
            },
            walker: Box::new(EmptyWalker),
        };
        let steps = match generator_optionchain(&walk_params) {
            Ok(steps) => steps,
            Err(e) => panic!("empty walker output must not error: {e}"),
        };
        assert_eq!(steps.len(), 1);
    }
}

#[cfg(test)]
mod generators_coverage_tests {
    use super::*;
    use positive::{Positive, spos};

    use crate::ExpirationDate;
    use crate::chains::generators::generator_optionchain;
    use crate::simulation::steps::{Step, Xstep, Ystep};
    use crate::simulation::{WalkParams, WalkType, WalkTypeAble};
    use crate::utils::TimeFrame;
    use crate::utils::time::get_tomorrow_formatted;
    use rust_decimal_macros::dec;

    #[derive(Clone)]
    struct TestWalker {}
    impl TestWalker {
        fn new() -> Self {
            TestWalker {}
        }
    }
    impl WalkTypeAble<Positive, OptionChain> for TestWalker {}

    #[test]
    fn test_generator_optionchain_early_return() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::GeometricBrownian {
                dt: pos_or_panic!(0.01),
                drift: dec!(0.0),
                volatility: pos_or_panic!(0.2),
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }

    #[test]
    fn test_generator_optionchain_brownian() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::Brownian {
                dt: pos_or_panic!(0.01),
                drift: dec!(0.0),
                volatility: pos_or_panic!(0.2),
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }

    #[test]
    fn test_generator_optionchain_log_returns() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::LogReturns {
                dt: pos_or_panic!(0.01),
                expected_return: Default::default(),
                volatility: pos_or_panic!(0.2),
                autocorrelation: None,
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }

    #[test]
    fn test_generator_optionchain_mean_reverting() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::MeanReverting {
                dt: pos_or_panic!(0.01),
                volatility: pos_or_panic!(0.2),
                speed: Default::default(),
                mean: Default::default(),
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }

    #[test]
    fn test_generator_optionchain_jump_diffusion() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::JumpDiffusion {
                dt: pos_or_panic!(0.01),
                drift: Default::default(),
                volatility: pos_or_panic!(0.2),
                intensity: Default::default(),
                jump_mean: Default::default(),
                jump_volatility: Default::default(),
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }

    #[test]
    fn test_generator_optionchain_garch() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::Garch {
                dt: pos_or_panic!(0.01),
                drift: Default::default(),
                volatility: pos_or_panic!(0.2),
                alpha: Default::default(),
                beta: Default::default(),
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }

    #[test]
    fn test_generator_optionchain_heston() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::Heston {
                dt: pos_or_panic!(0.01),
                drift: Default::default(),
                volatility: pos_or_panic!(0.2),
                kappa: Default::default(),
                theta: Default::default(),
                xi: Default::default(),
                rho: Default::default(),
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }

    #[test]
    fn test_generator_optionchain_custom() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::Custom {
                dt: pos_or_panic!(0.01),
                drift: Default::default(),
                volatility: pos_or_panic!(0.2),
                vov: Default::default(),
                vol_speed: Default::default(),
                vol_mean: Default::default(),
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }

    #[test]
    fn test_generator_optionchain_historical() {
        // Create a small walk with only one step to test early return
        let chain = OptionChain::new(
            "TEST",
            Positive::HUNDRED,
            get_tomorrow_formatted(),
            Some(dec!(0.05)),
            spos!(0.02),
        );

        let walker = Box::new(TestWalker::new());

        let walk_params = WalkParams {
            size: 1, // Just one step to trigger early return
            init_step: Step {
                x: Xstep::new(
                    Positive::ONE,
                    TimeFrame::Minute,
                    ExpirationDate::Days(pos_or_panic!(30.0)),
                ),
                y: Ystep::new(0, chain),
            },
            walk_type: WalkType::Historical {
                timeframe: TimeFrame::Microsecond,
                prices: vec![
                    Positive::HUNDRED,
                    pos_or_panic!(101.0),
                    pos_or_panic!(102.0),
                ],
                symbol: None,
            },
            walker,
        };

        let steps = generator_optionchain(&walk_params).unwrap();

        // We should just get the initial step back
        assert_eq!(steps.len(), 1);
    }
}