Files
foxhunt/ml/tests/wave16s_price_validation_test.rs
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

175 lines
7.6 KiB
Rust

/// WAVE 16S-P1: Price Validation Test Suite
///
/// Tests the production-grade price validation logic that prevents catastrophic
/// portfolio values from corrupted data (e.g., the -$1.93B bug from $1.11 price).
use ml::dqn::action_space::{ExposureLevel, FactoredAction, OrderType, Urgency};
use ml::dqn::portfolio_tracker::PortfolioTracker;
#[test]
fn test_price_validation_rejects_nan() {
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal);
// Attempt to execute with NaN price
tracker.execute_action(action, f32::NAN, 100.0);
// Portfolio should be unchanged (action rejected)
assert_eq!(tracker.current_position(), 0.0, "NaN price should be rejected");
assert_eq!(tracker.cash_balance(), 10_000.0, "Cash should be unchanged");
}
#[test]
fn test_price_validation_rejects_infinity() {
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal);
// Attempt to execute with Inf price
tracker.execute_action(action, f32::INFINITY, 100.0);
// Portfolio should be unchanged (action rejected)
assert_eq!(tracker.current_position(), 0.0, "Infinity price should be rejected");
assert_eq!(tracker.cash_balance(), 10_000.0, "Cash should be unchanged");
}
#[test]
fn test_price_validation_rejects_zero() {
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal);
// Attempt to execute with zero price
tracker.execute_action(action, 0.0, 100.0);
// Portfolio should be unchanged (action rejected)
assert_eq!(tracker.current_position(), 0.0, "Zero price should be rejected");
assert_eq!(tracker.cash_balance(), 10_000.0, "Cash should be unchanged");
}
#[test]
fn test_price_validation_rejects_negative() {
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal);
// Attempt to execute with negative price
tracker.execute_action(action, -100.0, 100.0);
// Portfolio should be unchanged (action rejected)
assert_eq!(tracker.current_position(), 0.0, "Negative price should be rejected");
assert_eq!(tracker.cash_balance(), 10_000.0, "Cash should be unchanged");
}
#[test]
fn test_price_validation_rejects_corrupted_price_1_11() {
// This is the exact bug that caused -$1.93B portfolio value
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal);
// Attempt to execute with corrupted price $1.11 (instead of ~$5000 for ES)
tracker.execute_action(action, 1.11, 100.0);
// Portfolio should be unchanged (action rejected)
assert_eq!(tracker.current_position(), 0.0, "Price $1.11 should be rejected (< $1000 ES min)");
assert_eq!(tracker.cash_balance(), 10_000.0, "Cash should be unchanged");
}
#[test]
fn test_price_validation_rejects_below_es_min() {
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal);
// Attempt to execute with price below ES minimum ($1000)
tracker.execute_action(action, 999.99, 100.0);
// Portfolio should be unchanged (action rejected)
assert_eq!(tracker.current_position(), 0.0, "Price $999.99 should be rejected (< $1000 ES min)");
assert_eq!(tracker.cash_balance(), 10_000.0, "Cash should be unchanged");
}
#[test]
fn test_price_validation_rejects_above_es_max() {
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal);
// Attempt to execute with price above ES maximum ($10,000)
tracker.execute_action(action, 10_000.01, 100.0);
// Portfolio should be unchanged (action rejected)
assert_eq!(tracker.current_position(), 0.0, "Price $10,000.01 should be rejected (> $10K ES max)");
assert_eq!(tracker.cash_balance(), 10_000.0, "Cash should be unchanged");
}
#[test]
fn test_price_validation_accepts_valid_es_price() {
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long50, OrderType::Market, Urgency::Normal);
// Execute with valid ES price (~$5000)
tracker.execute_action(action, 5000.0, 100.0);
// Portfolio should be updated (action accepted)
// Long50 = 0.5 exposure, so position = 0.5 * 100 = 50 contracts
assert_eq!(tracker.current_position(), 50.0, "Valid price $5000 should be accepted");
// Cash should be reduced by position value minus transaction costs
// 50 contracts * $5000 = $250,000 notional, but position is clamped to ±200 max
// target = 0.5 * 100 = 50 contracts (within ±200 limit)
// Cash change: 50 * 5000 = 250,000 (but we only have $10K capital)
// This will show up as negative cash (leveraged position)
assert!(tracker.cash_balance() < 10_000.0, "Cash should be reduced after buying");
}
#[test]
fn test_price_validation_accepts_boundary_prices() {
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Flat, OrderType::Market, Urgency::Normal);
// Test exact minimum boundary ($1000.00)
tracker.execute_action(action, 1000.0, 100.0);
assert_eq!(tracker.current_position(), 0.0, "Min boundary $1000 should be accepted (Flat position)");
tracker.reset();
// Test exact maximum boundary ($10,000.00)
tracker.execute_action(action, 10_000.0, 100.0);
assert_eq!(tracker.current_position(), 0.0, "Max boundary $10,000 should be accepted (Flat position)");
}
#[test]
fn test_price_validation_continuity_warning() {
// This test verifies that large price jumps (>50%) are logged but allowed
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Flat, OrderType::Market, Urgency::Normal);
// First action: establish baseline price
tracker.execute_action(action, 5000.0, 100.0);
// Second action: price jumps 60% (from $5000 to $8000)
// This should trigger WARNING but still be accepted (legitimate flash crashes can happen)
tracker.execute_action(action, 8000.0, 100.0);
// Portfolio should be updated (action accepted despite warning)
// Flat position, so position should still be 0
assert_eq!(tracker.current_position(), 0.0, "Large price jump should be allowed with warning");
}
#[test]
fn test_price_validation_multiple_rejections() {
// Verify that multiple rejected actions don't corrupt portfolio state
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 1.0);
let action = FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal);
// Attempt multiple invalid prices
tracker.execute_action(action, f32::NAN, 100.0);
tracker.execute_action(action, -50.0, 100.0);
tracker.execute_action(action, 0.5, 100.0);
tracker.execute_action(action, 15_000.0, 100.0);
// Portfolio should remain pristine
assert_eq!(tracker.current_position(), 0.0, "Multiple rejections should leave portfolio unchanged");
assert_eq!(tracker.cash_balance(), 10_000.0, "Cash should be unchanged after multiple rejections");
// Now execute a valid action to verify tracker still works
tracker.execute_action(action, 5000.0, 100.0);
assert_eq!(tracker.current_position(), 100.0, "Valid action should work after rejections");
}