# Wave D Efficient Implementation Plan **Date**: 2025-10-17 **Principle**: REUSE existing infrastructure, implement ONLY missing components ## Research Summary (5 Parallel Agents Complete) ### Code Reuse Analysis: 93.1% Existing Infrastructure **Existing Production-Ready Code** (10,019+ lines): - adaptive-strategy/src/regime/mod.rs: 4,800 lines (framework complete) - adaptive-strategy/src/ensemble/mod.rs: 757 lines (regime-aware) - adaptive-strategy/src/risk/mod.rs: 1,442 lines (regime-aware position sizing) - ml/src/features/*: 3,000+ lines (all statistical utilities) **Missing Components** (7% new code, ~400 lines): 1. CUSUM structural break detector 2. ADX technical indicator 3. Integration wiring --- ## Implementation Strategy: 3 Focused Agents (NOT 20) ### Agent D1: CUSUM Detector (TDD, 2 days) **File**: `adaptive-strategy/src/regime/cusum_detector.rs` **Reuses**: `RegimeDetectionModel` trait (already exists) **Lines**: 200-300 **Tests**: 15 tests (following existing patterns in `adaptive-strategy/tests/`) **Implementation**: ```rust pub struct CUSUMDetector { target_mean: f64, positive_sum: f64, // Two-sided CUSUM negative_sum: f64, drift_threshold: f64, detection_threshold: f64, } impl RegimeDetectionModel for CUSUMDetector { fn detect(&self, features: &[f64]) -> MarketRegime { // Use existing MarketRegime::StructuralBreak } } ``` **Reuses**: - `MarketRegime` enum (add `StructuralBreak` variant if missing) - `RegimeDetectionModel` trait - Existing test patterns from `regime_transition_tests.rs` --- ### Agent D2: ADX Indicator (TDD, 1 day) **File**: `ml/src/features/feature_extraction.rs` (extend existing) **Reuses**: ATR implementation (already exists at line 267-300) **Lines**: 50-80 **Tests**: 8 tests (following Wave C patterns) **Implementation**: ```rust pub fn compute_adx(bars: &VecDeque, period: usize) -> f64 { // Reuse compute_atr() for TR calculation let atr = compute_atr(bars, period); // Implement +DI, -DI, DX, ADX // Pattern: Same as compute_rsi() at line 132-177 } ``` **Reuses**: - `compute_atr()` function (lines 267-300) - `VecDeque` pattern (same as RSI, ATR, Bollinger) - Test structure from `test_compute_rsi()` and `test_compute_atr()` --- ### Agent D3: Integration Wiring (TDD, 1 day) **File**: `adaptive-strategy/src/regime/mod.rs` (extend) **Reuses**: `StrategyAdaptationManager` (90% complete) **Lines**: 100-150 **Tests**: 12 tests (extend `regime_transition_tests.rs`) **Tasks**: 1. Wire CUSUM detector into `RegimeDetector` 2. Add ADX to feature extraction pipeline 3. Update `StrategyAdaptationManager` configuration 4. Extend tests with structural break scenarios **Reuses**: - Entire `StrategyAdaptationManager` class (no modifications needed) - `RegimeTransitionTracker` (no modifications needed) - `DynamicRiskAdjuster` (no modifications needed) - Existing test data generators from `tests/common/mod.rs` --- ## TDD Red-Green-Refactor Workflow ### Agent D1 (CUSUM): **Day 1 - Red**: 1. Write 15 failing tests in `adaptive-strategy/tests/cusum_detector_test.rs` 2. Copy test structure from `regime_transition_tests.rs` 3. Use existing `generate_price_series()` helper **Day 1-2 - Green**: 1. Implement `CUSUMDetector` struct 2. Implement `RegimeDetectionModel` trait 3. All 15 tests pass **Day 2 - Refactor**: 1. Extract common code to utilities 2. Add documentation 3. Performance benchmark (<100μs target) ### Agent D2 (ADX): **Day 1 - Red**: 1. Write 8 failing tests in `ml/tests/adx_test.rs` 2. Follow `test_compute_rsi()` pattern **Day 1 - Green**: 1. Implement `compute_adx()` function 2. Reuse `compute_atr()` for TR 3. All 8 tests pass **Day 1 - Refactor**: 1. Optimize with existing `MonotonicDeque` utilities 2. Add to feature extraction pipeline ### Agent D3 (Integration): **Day 1 - Red**: 1. Write 12 failing integration tests 2. Test structural break detection end-to-end **Day 1 - Green**: 1. Wire CUSUM into `RegimeDetector` 2. Add ADX to feature pipeline 3. All 12 tests pass **Day 1 - Refactor**: 1. Update configuration schema 2. Add documentation 3. Performance validation --- ## File Organization ### New Files (3 total): ``` adaptive-strategy/src/regime/cusum_detector.rs (200-300 lines) adaptive-strategy/tests/cusum_detector_test.rs (150-200 lines) ml/tests/adx_test.rs (80-100 lines) ``` ### Modified Files (2 total): ``` ml/src/features/feature_extraction.rs (+50-80 lines for ADX) adaptive-strategy/tests/regime_transition_tests.rs (+100-150 lines) ``` **Total New Code**: ~700 lines (vs 10,000+ reused) --- ## Testing Strategy (Following Existing Patterns) ### Unit Tests (35 total): - CUSUM detector: 15 tests (pattern: `cusum_test.rs`) - ADX indicator: 8 tests (pattern: `test_compute_rsi()`) - Integration: 12 tests (pattern: `regime_transition_tests.rs`) ### Test Helpers (Already Exist): ```rust // From tests/common/mod.rs pub fn generate_price_series() -> Vec // Synthetic data pub fn generate_ohlcv_bars() -> VecDeque // OHLCV data pub fn assert_approx_eq(a: f64, b: f64, epsilon: f64) // Float comparison ``` ### Property-Based Tests: ```rust // Already exists in Wave C tests use proptest::prelude::*; proptest! { #[test] fn test_cusum_invariants(data in vec(-10.0..10.0, 100..1000)) { // CUSUM >= 0, changepoint detection accuracy } } ``` --- ## Performance Targets (Already Met by Existing Code) | Component | Target | Existing Performance | New Code | |-----------|--------|---------------------|----------| | Autocorrelation | <50μs | ✅ <50μs | Reuse | | Volatility (3 types) | <100μs | ✅ <100μs | Reuse | | Rolling Stats | <100μs | ✅ O(1) amortized | Reuse | | Hurst Exponent | <200μs | ✅ <200μs | Reuse | | **CUSUM** | <100μs | 🟡 Not implemented | **Implement** | | **ADX** | <150μs | 🟡 Not implemented | **Implement** | | Regime Classification | <200μs | ✅ Framework ready | Wire | | **Total Pipeline** | <1.2ms | ✅ <1ms (Wave C) | <200μs overhead | --- ## Timeline: 4 Days (NOT 10-13 hours from original plan) **Day 1**: Agent D1 (CUSUM) - Red phase + partial Green **Day 2**: Agent D1 (CUSUM) - Green + Refactor, Agent D2 (ADX) - Red/Green/Refactor **Day 3**: Agent D3 (Integration) - Red/Green/Refactor **Day 4**: E2E testing, validation, documentation **Total**: 4 days, 3 agents, ~700 lines new code --- ## Success Criteria ### Technical: - ✅ All 35 tests passing (100% pass rate) - ✅ CUSUM detects structural breaks within 5 bars - ✅ ADX calculation matches TA-Lib reference (<1% error) - ✅ Pipeline latency <1.2ms per bar (Wave C 1ms + Wave D 200μs) - ✅ Zero code duplication (use existing utilities) ### Business: - ✅ Sharpe improvement: 1.0 → 1.5+ (50% gain) - ✅ Regime classification accuracy >70% - ✅ No regressions from Wave C (1101/1101 tests still passing) --- ## Next Steps 1. **Spawn 3 focused agents** (D1: CUSUM, D2: ADX, D3: Integration) 2. **Follow TDD red-green-refactor** strictly 3. **Reuse existing test patterns** from Wave C and adaptive-strategy 4. **No code duplication** - use 50+ existing utility functions 5. **4-day delivery** with production-ready code --- **Efficiency Gain**: 93% code reuse (10,000+ lines) vs original 20-agent plan **Development Time**: 4 days vs 10-13 hours (more realistic) **Code Quality**: Production-ready (follows existing patterns)