Files
foxhunt/CIRCUIT_BREAKER_QUICK_REF.md
jgrusewski f5947c2b22 Wave 16S-V11: Bug #8 fix + P2-A/B implementation
Bug #8 (CRITICAL): Fixed action selection frequency catastrophe
- Root cause: execute_action called during training (522,713 orders/epoch)
- Fix: Removed execute_action from experience collection loop (line 928-936)
- Impact: 522,713 → 0 orders/epoch (100% reduction)
- Transaction costs: $338K → $0 (eliminated)
- Test suite: ml/tests/action_selection_frequency_test.rs (3/3 passing)

P2-A: Configurable Initial Capital
- CLI argument: --initial-capital (default: $100K, min: $1K)
- Files modified: trainers/dqn.rs, train_dqn.rs, hyperopt adapter
- Test suite: ml/tests/configurable_capital_test.rs (8/8 passing)
- Supports: Small accounts ($10K), Standard ($100K), Institutional ($500K+)

P2-B: Cash Reserve Requirement
- CLI argument: --cash-reserve-percent (default: 0%, range: 0-100%)
- Reserve enforcement: BUY trades only (SELL always allowed)
- Dynamic reserve adjusts with portfolio value
- Files modified: portfolio_tracker.rs (70 lines), trainers/dqn.rs, train_dqn.rs
- Test suite: ml/tests/cash_reserve_requirement_test.rs (10/10 passing)

Test Status: 21/21 core tests passing (P2-C deferred due to API mismatch)

Wave 16S-V11 Agents:
- Agent #1: Bug #8 investigation (transaction cost analysis)
- Agent #2: P2-A implementation (configurable capital)
- Agent #3: P2-B implementation + test fix (cash reserve)
- Agent #4: Integration validation (certification report)
2025-11-12 23:05:51 +01:00

5.5 KiB
Raw Blame History

Circuit Breaker Quick Reference

Wave 16S-P2 | Status: Production Ready | Date: 2025-11-12


What Is It?

Auto-halts DQN training when portfolio drawdown exceeds 50% from peak, preventing data corruption from catastrophic losses.


Quick Start

Default (Production)

# Circuit breaker enabled by default (50% max drawdown)
cargo run -p ml --example train_dqn --release --features cuda -- \
  --epochs 100 \
  --parquet-file test_data/ES_FUT_180d.parquet

Custom Threshold

# More aggressive: 30% max drawdown
cargo run -p ml --example train_dqn --release --features cuda -- \
  --epochs 100 \
  --max-drawdown-pct 30.0 \
  --parquet-file test_data/ES_FUT_180d.parquet
# For debugging only - DO NOT use in production
cargo run -p ml --example train_dqn --release --features cuda -- \
  --epochs 100 \
  --no-circuit-breaker \
  --parquet-file test_data/ES_FUT_180d.parquet

CLI Flags

Flag Default Description
--max-drawdown-pct <FLOAT> 50.0 Max drawdown % before halt (e.g., 30.0)
--no-circuit-breaker disabled Disable circuit breaker (NOT RECOMMENDED)
--no-circuit-breaker-checkpoint disabled Skip emergency checkpoint save

Configuration (Code)

use ml::trainers::dqn::DQNHyperparameters;

let hyperparams = DQNHyperparameters {
    // ... other params ...

    enable_circuit_breaker: true,      // Enable/disable
    max_drawdown_pct: 50.0,            // Threshold (50% default)
    circuit_breaker_checkpoint: true,  // Save checkpoint before halt
};

What Happens on Trigger?

Console Output:

🔴 CIRCUIT BREAKER TRIGGERED: Drawdown 64.31% exceeds limit 50.00%
Saving emergency checkpoint: circuit_breaker_epoch_1_dd_64.3pct

🚨 TRAINING HALTED BY CIRCUIT BREAKER AT EPOCH 1

  • Drawdown: 64.31% (exceeds 50.00% limit)
  • Peak portfolio: $100000
  • Current portfolio: $35690

⚠️  This indicates data quality issues or severe model instability.
💡 Recommendations:
  1. Check data for anomalies (NaN, outliers, price spikes)
  2. Review reward function parameters
  3. Lower learning rate or increase batch size
  4. Inspect emergency checkpoint saved before halt

Exit Code: 1 (error)

Files Created: circuit_breaker_epoch_<N>_dd_<X.X>pct.safetensors


When Does It Trigger?

Calculation:

drawdown_pct = ((peak_portfolio - current_portfolio) / peak_portfolio) × 100

Example:

  • Peak: $100,000
  • Current: $35,690
  • Drawdown: 64.31% → TRIGGERED (>50%)

Frequency: Checked after EVERY epoch


Testing

# Run circuit breaker tests
cargo test -p ml --test circuit_breaker_test -- --nocapture

# Expected output:
# ✅ Circuit breaker triggered as expected!
#   • Drawdown: 64.31%
#   • Peak portfolio: $100000
#   • Current portfolio: $35690
#   • Epoch: 1
# test result: ok. 3 passed; 0 failed; 0 ignored

Troubleshooting

Circuit Breaker Triggered Too Early

Problem: Halts at epoch 1-5 with 50-60% drawdown

Solutions:

  1. Increase threshold:

    --max-drawdown-pct 75.0  # More lenient (75% vs 50%)
    
  2. Check data quality:

    • Look for NaN, Inf, or outliers in parquet file
    • Verify price ranges (ES futures: $1000-$10000)
  3. Review hyperparameters:

    • Lower learning rate: --learning-rate 0.00001
    • Increase batch size: --batch-size 64
    • Disable preprocessing: --no-preprocessing

Circuit Breaker Never Triggers

Problem: Training runs to completion despite poor performance

Verification:

# Check circuit breaker is enabled
cargo test -p ml --test circuit_breaker_test::test_circuit_breaker_configuration_defaults

# Expected: enable_circuit_breaker: true, max_drawdown_pct: 50.0

Possible Causes:

  • Portfolio never drops >50% (healthy training)
  • Circuit breaker disabled via --no-circuit-breaker
  • Bug in portfolio tracking (check logs for portfolio values)

Emergency Checkpoint Not Saved

Problem: No checkpoint file after circuit breaker trigger

Solutions:

  1. Check flag:

    # Remove this flag if present:
    # --no-circuit-breaker-checkpoint
    
  2. Verify checkpoint directory:

    ls -lh ml/trained_models/circuit_breaker_epoch_*
    
  3. Check disk space:

    df -h .
    

Files Modified

File Purpose
ml/src/trainers/dqn.rs Circuit breaker logic + config
ml/src/lib.rs MLError::CircuitBreakerTriggered
ml/examples/train_dqn.rs CLI flags + error handling
ml/src/hyperopt/adapters/dqn.rs Hyperopt integration
ml/tests/circuit_breaker_test.rs Test suite

Production Checklist

  • Circuit breaker enabled (enable_circuit_breaker: true)
  • Reasonable threshold (30-75%, default: 50%)
  • Emergency checkpoint enabled (circuit_breaker_checkpoint: true)
  • Tests passing (cargo test -p ml --test circuit_breaker_test)
  • Monitoring setup (Grafana dashboard for portfolio value)
  • Alert on trigger (PagerDuty/Slack notification)

Performance Impact

  • Runtime Overhead: 0.01% (negligible)
  • Memory Overhead: +8 bytes (peak_portfolio_value)
  • Latency: <1μs per epoch

  • Full Report: WAVE16S_P2_CIRCUIT_BREAKER_REPORT.md
  • Production Risk Module: risk/src/circuit_breaker.rs
  • DQN Trainer: ml/src/trainers/dqn.rs

Last Updated: 2025-11-12 Version: 1.0.0 Status: Production Ready