market-maker-rs 0.2.0

A Rust library implementing quantitative market making strategies, starting with the Avellaneda-Stoikov model
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
938
939
940
941
//! Circuit breaker implementation for automatic trading halts.

use crate::Decimal;
use crate::types::error::{MMError, MMResult};
use std::collections::VecDeque;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Reason why the circuit breaker was triggered.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TriggerReason {
    /// Daily loss limit exceeded.
    MaxDailyLoss,
    /// Market volatility exceeded threshold.
    VolatilitySpike,
    /// Too many consecutive losing trades.
    ConsecutiveLosses,
    /// Equity dropped too fast within time window.
    RapidDrawdown,
    /// Manually triggered by operator.
    Manual,
}

impl std::fmt::Display for TriggerReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MaxDailyLoss => write!(f, "max daily loss exceeded"),
            Self::VolatilitySpike => write!(f, "volatility spike detected"),
            Self::ConsecutiveLosses => write!(f, "consecutive losses limit reached"),
            Self::RapidDrawdown => write!(f, "rapid drawdown detected"),
            Self::Manual => write!(f, "manually triggered"),
        }
    }
}

/// Current state of the circuit breaker.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CircuitBreakerState {
    /// Normal operation, trading allowed.
    Active,
    /// Trading halted due to trigger condition.
    Triggered {
        /// Reason for the trigger.
        reason: TriggerReason,
        /// Timestamp when triggered, in milliseconds since Unix epoch.
        triggered_at: u64,
    },
    /// Cooling down before resuming trading.
    Cooldown {
        /// Timestamp when trading can resume, in milliseconds since Unix epoch.
        resume_at: u64,
    },
}

impl CircuitBreakerState {
    /// Returns true if the state is `Active`.
    #[must_use]
    pub fn is_active(&self) -> bool {
        matches!(self, Self::Active)
    }

    /// Returns true if the state is `Triggered`.
    #[must_use]
    pub fn is_triggered(&self) -> bool {
        matches!(self, Self::Triggered { .. })
    }

    /// Returns true if the state is `Cooldown`.
    #[must_use]
    pub fn is_cooldown(&self) -> bool {
        matches!(self, Self::Cooldown { .. })
    }
}

/// Configuration for the circuit breaker.
///
/// All thresholds are configurable to match different risk tolerances
/// and trading strategies.
///
/// # Example
///
/// ```rust
/// use market_maker_rs::risk::CircuitBreakerConfig;
/// use market_maker_rs::dec;
///
/// let config = CircuitBreakerConfig::new(
///     dec!(1000.0),  // max $1,000 daily loss
///     dec!(0.05),    // max 5% volatility
///     5,             // max 5 consecutive losses
///     dec!(0.10),    // 10% rapid drawdown threshold
///     300_000,       // 5 minute drawdown window (ms)
///     60_000,        // 1 minute cooldown (ms)
/// ).unwrap();
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CircuitBreakerConfig {
    /// Maximum allowed daily loss in currency units.
    ///
    /// When cumulative daily losses exceed this threshold, trading halts.
    pub max_daily_loss: Decimal,

    /// Maximum allowed volatility (as decimal, e.g., 0.05 for 5%).
    ///
    /// When market volatility exceeds this, trading halts.
    pub max_volatility: Decimal,

    /// Maximum consecutive losing trades before halt.
    ///
    /// After this many losses in a row, trading halts.
    pub max_consecutive_losses: u32,

    /// Rapid drawdown threshold (as decimal, e.g., 0.10 for 10%).
    ///
    /// If equity drops by this percentage within the drawdown window, trading halts.
    pub rapid_drawdown_threshold: Decimal,

    /// Time window for rapid drawdown detection, in milliseconds.
    ///
    /// Drawdown is measured over this rolling window.
    pub rapid_drawdown_window_ms: u64,

    /// Cooldown duration after trigger, in milliseconds.
    ///
    /// After being triggered, the breaker enters cooldown for this duration.
    pub cooldown_duration_ms: u64,
}

impl CircuitBreakerConfig {
    /// Creates a new `CircuitBreakerConfig` with validation.
    ///
    /// # Arguments
    ///
    /// * `max_daily_loss` - Maximum daily loss (must be positive)
    /// * `max_volatility` - Maximum volatility threshold (must be positive)
    /// * `max_consecutive_losses` - Maximum consecutive losses (must be > 0)
    /// * `rapid_drawdown_threshold` - Drawdown threshold (must be in (0, 1])
    /// * `rapid_drawdown_window_ms` - Drawdown window in milliseconds (must be > 0)
    /// * `cooldown_duration_ms` - Cooldown duration in milliseconds
    ///
    /// # Errors
    ///
    /// Returns `MMError::InvalidConfiguration` if any parameter is invalid.
    pub fn new(
        max_daily_loss: Decimal,
        max_volatility: Decimal,
        max_consecutive_losses: u32,
        rapid_drawdown_threshold: Decimal,
        rapid_drawdown_window_ms: u64,
        cooldown_duration_ms: u64,
    ) -> MMResult<Self> {
        if max_daily_loss <= Decimal::ZERO {
            return Err(MMError::InvalidConfiguration(
                "max_daily_loss must be positive".to_string(),
            ));
        }

        if max_volatility <= Decimal::ZERO {
            return Err(MMError::InvalidConfiguration(
                "max_volatility must be positive".to_string(),
            ));
        }

        if max_consecutive_losses == 0 {
            return Err(MMError::InvalidConfiguration(
                "max_consecutive_losses must be greater than 0".to_string(),
            ));
        }

        if rapid_drawdown_threshold <= Decimal::ZERO || rapid_drawdown_threshold > Decimal::ONE {
            return Err(MMError::InvalidConfiguration(
                "rapid_drawdown_threshold must be between 0 and 1 (exclusive of 0)".to_string(),
            ));
        }

        if rapid_drawdown_window_ms == 0 {
            return Err(MMError::InvalidConfiguration(
                "rapid_drawdown_window_ms must be greater than 0".to_string(),
            ));
        }

        Ok(Self {
            max_daily_loss,
            max_volatility,
            max_consecutive_losses,
            rapid_drawdown_threshold,
            rapid_drawdown_window_ms,
            cooldown_duration_ms,
        })
    }
}

/// Equity snapshot for drawdown tracking.
#[derive(Debug, Clone)]
struct EquitySnapshot {
    /// Equity value at this point.
    equity: Decimal,
    /// Timestamp in milliseconds.
    timestamp: u64,
}

/// Circuit breaker for automatic trading halts.
///
/// Monitors trading activity and halts operations when adverse conditions
/// are detected. Supports multiple trigger conditions and automatic cooldown.
///
/// # State Machine
///
/// ```text
/// Active -> Triggered (on breach) -> Cooldown (after acknowledgment) -> Active
/// ```
///
/// # Example
///
/// ```rust
/// use market_maker_rs::risk::{CircuitBreaker, CircuitBreakerConfig};
/// use market_maker_rs::dec;
///
/// let config = CircuitBreakerConfig::new(
///     dec!(1000.0), dec!(0.05), 5, dec!(0.10), 300_000, 60_000
/// ).unwrap();
///
/// let mut breaker = CircuitBreaker::new(config);
///
/// // Record some losing trades
/// breaker.record_trade(dec!(-100.0), 1000);
/// breaker.record_trade(dec!(-100.0), 2000);
///
/// assert!(breaker.is_trading_allowed());
///
/// // After too many losses, trading halts
/// for i in 0..5 {
///     breaker.record_trade(dec!(-100.0), 3000 + i as u64 * 1000);
/// }
/// // Daily loss of $700 exceeds nothing yet, but 5 consecutive losses triggers
/// ```
#[derive(Debug, Clone)]
pub struct CircuitBreaker {
    /// Configuration parameters.
    config: CircuitBreakerConfig,
    /// Current state.
    state: CircuitBreakerState,
    /// Cumulative daily loss (positive value = loss).
    daily_loss: Decimal,
    /// Current consecutive losing trades count.
    consecutive_losses: u32,
    /// Current market volatility.
    current_volatility: Decimal,
    /// Equity history for drawdown calculation.
    equity_history: VecDeque<EquitySnapshot>,
    /// Peak equity for drawdown calculation.
    peak_equity: Decimal,
    /// Current equity.
    current_equity: Decimal,
}

impl CircuitBreaker {
    /// Creates a new circuit breaker with the given configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Circuit breaker configuration
    ///
    /// # Example
    ///
    /// ```rust
    /// use market_maker_rs::risk::{CircuitBreaker, CircuitBreakerConfig};
    /// use market_maker_rs::dec;
    ///
    /// let config = CircuitBreakerConfig::new(
    ///     dec!(1000.0), dec!(0.05), 5, dec!(0.10), 300_000, 60_000
    /// ).unwrap();
    ///
    /// let breaker = CircuitBreaker::new(config);
    /// assert!(breaker.is_trading_allowed());
    /// ```
    #[must_use]
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            config,
            state: CircuitBreakerState::Active,
            daily_loss: Decimal::ZERO,
            consecutive_losses: 0,
            current_volatility: Decimal::ZERO,
            equity_history: VecDeque::new(),
            peak_equity: Decimal::ZERO,
            current_equity: Decimal::ZERO,
        }
    }

    /// Creates a new circuit breaker with initial equity.
    ///
    /// # Arguments
    ///
    /// * `config` - Circuit breaker configuration
    /// * `initial_equity` - Starting equity for drawdown calculations
    /// * `timestamp` - Initial timestamp in milliseconds
    #[must_use]
    pub fn with_initial_equity(
        config: CircuitBreakerConfig,
        initial_equity: Decimal,
        timestamp: u64,
    ) -> Self {
        let mut breaker = Self::new(config);
        breaker.current_equity = initial_equity;
        breaker.peak_equity = initial_equity;
        breaker.equity_history.push_back(EquitySnapshot {
            equity: initial_equity,
            timestamp,
        });
        breaker
    }

    /// Returns the current state of the circuit breaker.
    #[must_use]
    pub fn state(&self) -> &CircuitBreakerState {
        &self.state
    }

    /// Returns the current daily loss.
    #[must_use]
    pub fn daily_loss(&self) -> Decimal {
        self.daily_loss
    }

    /// Returns the current consecutive losses count.
    #[must_use]
    pub fn consecutive_losses(&self) -> u32 {
        self.consecutive_losses
    }

    /// Checks if trading is currently allowed.
    ///
    /// Trading is only allowed when the circuit breaker is in `Active` state.
    ///
    /// # Example
    ///
    /// ```rust
    /// use market_maker_rs::risk::{CircuitBreaker, CircuitBreakerConfig};
    /// use market_maker_rs::dec;
    ///
    /// let config = CircuitBreakerConfig::new(
    ///     dec!(1000.0), dec!(0.05), 5, dec!(0.10), 300_000, 60_000
    /// ).unwrap();
    ///
    /// let breaker = CircuitBreaker::new(config);
    /// assert!(breaker.is_trading_allowed());
    /// ```
    #[must_use]
    pub fn is_trading_allowed(&self) -> bool {
        self.state.is_active()
    }

    /// Records a trade result and checks for trigger conditions.
    ///
    /// # Arguments
    ///
    /// * `pnl` - Profit/loss from the trade (negative = loss)
    /// * `timestamp` - Timestamp of the trade in milliseconds
    ///
    /// # Returns
    ///
    /// The current state after recording the trade.
    ///
    /// # Example
    ///
    /// ```rust
    /// use market_maker_rs::risk::{CircuitBreaker, CircuitBreakerConfig, CircuitBreakerState};
    /// use market_maker_rs::dec;
    ///
    /// let config = CircuitBreakerConfig::new(
    ///     dec!(100.0), dec!(0.05), 3, dec!(0.10), 300_000, 60_000
    /// ).unwrap();
    ///
    /// let mut breaker = CircuitBreaker::new(config);
    ///
    /// // Three consecutive losses trigger the breaker
    /// breaker.record_trade(dec!(-10.0), 1000);
    /// breaker.record_trade(dec!(-10.0), 2000);
    /// let state = breaker.record_trade(dec!(-10.0), 3000);
    ///
    /// assert!(state.is_triggered());
    /// ```
    pub fn record_trade(&mut self, pnl: Decimal, timestamp: u64) -> CircuitBreakerState {
        // Don't record if already triggered
        if !self.state.is_active() {
            return self.state.clone();
        }

        // Update daily loss
        if pnl < Decimal::ZERO {
            self.daily_loss += pnl.abs();
            self.consecutive_losses += 1;
        } else {
            self.consecutive_losses = 0;
        }

        // Update equity
        self.current_equity += pnl;
        if self.current_equity > self.peak_equity {
            self.peak_equity = self.current_equity;
        }

        // Add to equity history
        self.equity_history.push_back(EquitySnapshot {
            equity: self.current_equity,
            timestamp,
        });

        // Prune old equity history
        self.prune_equity_history(timestamp);

        // Check triggers
        self.check_triggers(timestamp);

        self.state.clone()
    }

    /// Updates the current market volatility.
    ///
    /// # Arguments
    ///
    /// * `volatility` - Current market volatility (as decimal)
    /// * `timestamp` - Current timestamp in milliseconds
    ///
    /// # Returns
    ///
    /// The current state after updating volatility.
    pub fn update_volatility(
        &mut self,
        volatility: Decimal,
        timestamp: u64,
    ) -> CircuitBreakerState {
        self.current_volatility = volatility;

        if self.state.is_active() {
            self.check_triggers(timestamp);
        }

        self.state.clone()
    }

    /// Manually triggers the circuit breaker.
    ///
    /// # Arguments
    ///
    /// * `timestamp` - Current timestamp in milliseconds
    pub fn trigger_manual(&mut self, timestamp: u64) {
        self.state = CircuitBreakerState::Triggered {
            reason: TriggerReason::Manual,
            triggered_at: timestamp,
        };
    }

    /// Starts the cooldown period after a trigger.
    ///
    /// Call this to transition from `Triggered` to `Cooldown` state.
    ///
    /// # Arguments
    ///
    /// * `timestamp` - Current timestamp in milliseconds
    pub fn start_cooldown(&mut self, timestamp: u64) {
        if self.state.is_triggered() {
            self.state = CircuitBreakerState::Cooldown {
                resume_at: timestamp + self.config.cooldown_duration_ms,
            };
        }
    }

    /// Checks if cooldown has expired and transitions to Active if so.
    ///
    /// # Arguments
    ///
    /// * `current_time` - Current timestamp in milliseconds
    ///
    /// # Returns
    ///
    /// The current state after checking cooldown.
    pub fn check_cooldown(&mut self, current_time: u64) -> CircuitBreakerState {
        if let CircuitBreakerState::Cooldown { resume_at } = self.state
            && current_time >= resume_at
        {
            self.state = CircuitBreakerState::Active;
        }
        self.state.clone()
    }

    /// Resets the circuit breaker to initial state.
    ///
    /// Typically called at the start of a new trading day.
    ///
    /// # Arguments
    ///
    /// * `initial_equity` - Starting equity for the new period
    /// * `timestamp` - Current timestamp in milliseconds
    pub fn reset(&mut self, initial_equity: Decimal, timestamp: u64) {
        self.state = CircuitBreakerState::Active;
        self.daily_loss = Decimal::ZERO;
        self.consecutive_losses = 0;
        self.current_volatility = Decimal::ZERO;
        self.equity_history.clear();
        self.peak_equity = initial_equity;
        self.current_equity = initial_equity;
        self.equity_history.push_back(EquitySnapshot {
            equity: initial_equity,
            timestamp,
        });
    }

    /// Returns the configuration.
    #[must_use]
    pub fn config(&self) -> &CircuitBreakerConfig {
        &self.config
    }

    /// Checks all trigger conditions and updates state if breached.
    fn check_triggers(&mut self, timestamp: u64) {
        // Check max daily loss
        if self.daily_loss >= self.config.max_daily_loss {
            self.state = CircuitBreakerState::Triggered {
                reason: TriggerReason::MaxDailyLoss,
                triggered_at: timestamp,
            };
            return;
        }

        // Check volatility spike
        if self.current_volatility > self.config.max_volatility {
            self.state = CircuitBreakerState::Triggered {
                reason: TriggerReason::VolatilitySpike,
                triggered_at: timestamp,
            };
            return;
        }

        // Check consecutive losses
        if self.consecutive_losses >= self.config.max_consecutive_losses {
            self.state = CircuitBreakerState::Triggered {
                reason: TriggerReason::ConsecutiveLosses,
                triggered_at: timestamp,
            };
            return;
        }

        // Check rapid drawdown
        if self.check_rapid_drawdown() {
            self.state = CircuitBreakerState::Triggered {
                reason: TriggerReason::RapidDrawdown,
                triggered_at: timestamp,
            };
        }
    }

    /// Checks if rapid drawdown threshold has been breached.
    fn check_rapid_drawdown(&self) -> bool {
        if self.equity_history.is_empty() {
            return false;
        }

        // Find the peak equity in the window
        let window_peak = self
            .equity_history
            .iter()
            .map(|s| s.equity)
            .max()
            .unwrap_or(self.current_equity);

        if window_peak <= Decimal::ZERO {
            return false;
        }

        let drawdown = (window_peak - self.current_equity) / window_peak;
        drawdown >= self.config.rapid_drawdown_threshold
    }

    /// Removes equity snapshots older than the drawdown window.
    fn prune_equity_history(&mut self, current_time: u64) {
        let cutoff = current_time.saturating_sub(self.config.rapid_drawdown_window_ms);
        while let Some(front) = self.equity_history.front() {
            if front.timestamp < cutoff {
                self.equity_history.pop_front();
            } else {
                break;
            }
        }
    }
}

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

    fn default_config() -> CircuitBreakerConfig {
        CircuitBreakerConfig::new(
            dec!(1000.0), // max daily loss
            dec!(0.05),   // max volatility
            5,            // max consecutive losses
            dec!(0.10),   // rapid drawdown threshold
            300_000,      // 5 minute window
            60_000,       // 1 minute cooldown
        )
        .unwrap()
    }

    #[test]
    fn test_config_valid() {
        let config = default_config();
        assert_eq!(config.max_daily_loss, dec!(1000.0));
        assert_eq!(config.max_consecutive_losses, 5);
    }

    #[test]
    fn test_config_invalid_max_daily_loss() {
        let result =
            CircuitBreakerConfig::new(dec!(0.0), dec!(0.05), 5, dec!(0.10), 300_000, 60_000);
        assert!(result.is_err());

        let result =
            CircuitBreakerConfig::new(dec!(-100.0), dec!(0.05), 5, dec!(0.10), 300_000, 60_000);
        assert!(result.is_err());
    }

    #[test]
    fn test_config_invalid_max_volatility() {
        let result =
            CircuitBreakerConfig::new(dec!(1000.0), dec!(0.0), 5, dec!(0.10), 300_000, 60_000);
        assert!(result.is_err());
    }

    #[test]
    fn test_config_invalid_max_consecutive_losses() {
        let result =
            CircuitBreakerConfig::new(dec!(1000.0), dec!(0.05), 0, dec!(0.10), 300_000, 60_000);
        assert!(result.is_err());
    }

    #[test]
    fn test_config_invalid_rapid_drawdown_threshold() {
        let result =
            CircuitBreakerConfig::new(dec!(1000.0), dec!(0.05), 5, dec!(0.0), 300_000, 60_000);
        assert!(result.is_err());

        let result =
            CircuitBreakerConfig::new(dec!(1000.0), dec!(0.05), 5, dec!(1.1), 300_000, 60_000);
        assert!(result.is_err());
    }

    #[test]
    fn test_config_invalid_rapid_drawdown_window() {
        let result = CircuitBreakerConfig::new(dec!(1000.0), dec!(0.05), 5, dec!(0.10), 0, 60_000);
        assert!(result.is_err());
    }

    #[test]
    fn test_new_breaker_is_active() {
        let breaker = CircuitBreaker::new(default_config());
        assert!(breaker.is_trading_allowed());
        assert!(breaker.state().is_active());
    }

    #[test]
    fn test_with_initial_equity() {
        let breaker = CircuitBreaker::with_initial_equity(default_config(), dec!(10000.0), 0);
        assert!(breaker.is_trading_allowed());
        assert_eq!(breaker.current_equity, dec!(10000.0));
        assert_eq!(breaker.peak_equity, dec!(10000.0));
    }

    #[test]
    fn test_record_winning_trade() {
        let mut breaker = CircuitBreaker::new(default_config());
        breaker.record_trade(dec!(100.0), 1000);

        assert!(breaker.is_trading_allowed());
        assert_eq!(breaker.daily_loss(), dec!(0.0));
        assert_eq!(breaker.consecutive_losses(), 0);
    }

    #[test]
    fn test_record_losing_trade() {
        let mut breaker = CircuitBreaker::new(default_config());
        breaker.record_trade(dec!(-100.0), 1000);

        assert!(breaker.is_trading_allowed());
        assert_eq!(breaker.daily_loss(), dec!(100.0));
        assert_eq!(breaker.consecutive_losses(), 1);
    }

    #[test]
    fn test_consecutive_losses_reset_on_win() {
        let mut breaker = CircuitBreaker::new(default_config());

        breaker.record_trade(dec!(-100.0), 1000);
        breaker.record_trade(dec!(-100.0), 2000);
        assert_eq!(breaker.consecutive_losses(), 2);

        breaker.record_trade(dec!(50.0), 3000);
        assert_eq!(breaker.consecutive_losses(), 0);
    }

    #[test]
    fn test_trigger_max_daily_loss() {
        let config =
            CircuitBreakerConfig::new(dec!(100.0), dec!(0.05), 10, dec!(0.10), 300_000, 60_000)
                .unwrap();
        let mut breaker = CircuitBreaker::new(config);

        // Loss of $100 should trigger
        let state = breaker.record_trade(dec!(-100.0), 1000);

        assert!(state.is_triggered());
        assert!(!breaker.is_trading_allowed());

        if let CircuitBreakerState::Triggered { reason, .. } = state {
            assert_eq!(reason, TriggerReason::MaxDailyLoss);
        } else {
            panic!("Expected Triggered state");
        }
    }

    #[test]
    fn test_trigger_consecutive_losses() {
        let config =
            CircuitBreakerConfig::new(dec!(10000.0), dec!(0.05), 3, dec!(0.10), 300_000, 60_000)
                .unwrap();
        let mut breaker = CircuitBreaker::new(config);

        breaker.record_trade(dec!(-10.0), 1000);
        breaker.record_trade(dec!(-10.0), 2000);
        assert!(breaker.is_trading_allowed());

        let state = breaker.record_trade(dec!(-10.0), 3000);
        assert!(state.is_triggered());

        if let CircuitBreakerState::Triggered { reason, .. } = state {
            assert_eq!(reason, TriggerReason::ConsecutiveLosses);
        }
    }

    #[test]
    fn test_trigger_volatility_spike() {
        let mut breaker = CircuitBreaker::new(default_config());

        // Volatility below threshold
        breaker.update_volatility(dec!(0.04), 1000);
        assert!(breaker.is_trading_allowed());

        // Volatility above threshold
        let state = breaker.update_volatility(dec!(0.06), 2000);
        assert!(state.is_triggered());

        if let CircuitBreakerState::Triggered { reason, .. } = state {
            assert_eq!(reason, TriggerReason::VolatilitySpike);
        }
    }

    #[test]
    fn test_trigger_rapid_drawdown() {
        let config =
            CircuitBreakerConfig::new(dec!(10000.0), dec!(1.0), 100, dec!(0.10), 300_000, 60_000)
                .unwrap();
        let mut breaker = CircuitBreaker::with_initial_equity(config, dec!(1000.0), 0);

        // 10% drawdown should trigger
        let state = breaker.record_trade(dec!(-100.0), 1000);

        assert!(state.is_triggered());
        if let CircuitBreakerState::Triggered { reason, .. } = state {
            assert_eq!(reason, TriggerReason::RapidDrawdown);
        }
    }

    #[test]
    fn test_manual_trigger() {
        let mut breaker = CircuitBreaker::new(default_config());

        breaker.trigger_manual(1000);

        assert!(!breaker.is_trading_allowed());
        if let CircuitBreakerState::Triggered {
            reason,
            triggered_at,
        } = breaker.state()
        {
            assert_eq!(*reason, TriggerReason::Manual);
            assert_eq!(*triggered_at, 1000);
        } else {
            panic!("Expected Triggered state");
        }
    }

    #[test]
    fn test_cooldown_transition() {
        let mut breaker = CircuitBreaker::new(default_config());

        breaker.trigger_manual(1000);
        assert!(breaker.state().is_triggered());

        breaker.start_cooldown(2000);
        assert!(breaker.state().is_cooldown());

        if let CircuitBreakerState::Cooldown { resume_at } = breaker.state() {
            assert_eq!(*resume_at, 2000 + 60_000); // cooldown is 60_000ms
        }
    }

    #[test]
    fn test_cooldown_expiry() {
        let mut breaker = CircuitBreaker::new(default_config());

        breaker.trigger_manual(1000);
        breaker.start_cooldown(2000);

        // Before cooldown expires
        breaker.check_cooldown(50_000);
        assert!(breaker.state().is_cooldown());

        // After cooldown expires
        breaker.check_cooldown(70_000);
        assert!(breaker.state().is_active());
        assert!(breaker.is_trading_allowed());
    }

    #[test]
    fn test_reset() {
        let mut breaker = CircuitBreaker::new(default_config());

        breaker.record_trade(dec!(-500.0), 1000);
        breaker.record_trade(dec!(-500.0), 2000);
        breaker.trigger_manual(3000);

        assert!(!breaker.is_trading_allowed());
        assert_eq!(breaker.daily_loss(), dec!(1000.0));

        breaker.reset(dec!(10000.0), 100_000);

        assert!(breaker.is_trading_allowed());
        assert_eq!(breaker.daily_loss(), dec!(0.0));
        assert_eq!(breaker.consecutive_losses(), 0);
    }

    #[test]
    fn test_no_recording_when_triggered() {
        let config =
            CircuitBreakerConfig::new(dec!(100.0), dec!(0.05), 10, dec!(0.10), 300_000, 60_000)
                .unwrap();
        let mut breaker = CircuitBreaker::new(config);

        breaker.record_trade(dec!(-100.0), 1000); // Triggers
        assert!(!breaker.is_trading_allowed());

        let loss_before = breaker.daily_loss();
        breaker.record_trade(dec!(-100.0), 2000); // Should be ignored

        assert_eq!(breaker.daily_loss(), loss_before);
    }

    #[test]
    fn test_state_helper_methods() {
        let active = CircuitBreakerState::Active;
        assert!(active.is_active());
        assert!(!active.is_triggered());
        assert!(!active.is_cooldown());

        let triggered = CircuitBreakerState::Triggered {
            reason: TriggerReason::Manual,
            triggered_at: 0,
        };
        assert!(!triggered.is_active());
        assert!(triggered.is_triggered());
        assert!(!triggered.is_cooldown());

        let cooldown = CircuitBreakerState::Cooldown { resume_at: 1000 };
        assert!(!cooldown.is_active());
        assert!(!cooldown.is_triggered());
        assert!(cooldown.is_cooldown());
    }

    #[test]
    fn test_trigger_reason_display() {
        assert_eq!(
            TriggerReason::MaxDailyLoss.to_string(),
            "max daily loss exceeded"
        );
        assert_eq!(
            TriggerReason::VolatilitySpike.to_string(),
            "volatility spike detected"
        );
        assert_eq!(
            TriggerReason::ConsecutiveLosses.to_string(),
            "consecutive losses limit reached"
        );
        assert_eq!(
            TriggerReason::RapidDrawdown.to_string(),
            "rapid drawdown detected"
        );
        assert_eq!(TriggerReason::Manual.to_string(), "manually triggered");
    }

    #[test]
    fn test_equity_history_pruning() {
        let config = CircuitBreakerConfig::new(
            dec!(10000.0),
            dec!(1.0),
            100,
            dec!(0.50),
            100,
            60_000, // 100ms window
        )
        .unwrap();
        let mut breaker = CircuitBreaker::with_initial_equity(config, dec!(1000.0), 0);

        breaker.record_trade(dec!(-10.0), 50);
        breaker.record_trade(dec!(-10.0), 100);
        breaker.record_trade(dec!(-10.0), 200); // Should prune entries before 100

        // Old entries should be pruned
        assert!(breaker.equity_history.len() <= 3);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serialization() {
        let config = default_config();
        let json = serde_json::to_string(&config).unwrap();
        let deserialized: CircuitBreakerConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(config, deserialized);

        let state = CircuitBreakerState::Triggered {
            reason: TriggerReason::MaxDailyLoss,
            triggered_at: 1000,
        };
        let json = serde_json::to_string(&state).unwrap();
        let deserialized: CircuitBreakerState = serde_json::from_str(&json).unwrap();
        assert_eq!(state, deserialized);
    }
}