CRITICAL FINDINGS from 3-trial validation: - 85,120 gradient clipping warnings (81.6% of logs) - REGRESSION - Rainbow features DISABLED: use_dueling=false, use_distributional=false, use_noisy_nets=false - Negative Q-values confirmed: HOLD -1000 to -3250 - Performance: Sharpe 0.29 (target 0.77) Changes: - Fixed N-Step compilation (7/7 tests passing) - Fixed Distributional compilation (6/6 tests passing) - Fixed Dueling CUDA errors (10/10 tests passing) - Added TDD validation for state_dim=225 - Total: 23/23 Wave 11 tests passing (100%) Issues requiring investigation: 1. Why are Dueling/Distributional/Noisy disabled in hyperopt? 2. Why gradient explosion despite previous fixes? 3. Test coverage gaps - unit tests pass but integration fails 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
223 lines
7.3 KiB
Rust
223 lines
7.3 KiB
Rust
//! Position Limit Tests for PPO
|
|
//!
|
|
//! Test-driven development (TDD) - Tests written FIRST before implementation.
|
|
//! These tests verify position limit enforcement for risk management.
|
|
|
|
use ml::ppo::position_limits::{enforce_cash_reserve, enforce_position_limit};
|
|
|
|
#[test]
|
|
fn test_position_limit_enforcement() {
|
|
let max_position = 2.0; // Max position of ±2.0
|
|
|
|
// Test case 1: Within limits (long direction)
|
|
let current_position = 1.0;
|
|
let proposed_delta = 0.5; // Would go to 1.5 (within limit)
|
|
assert!(
|
|
enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Position 1.0 + 0.5 = 1.5 should be allowed (max: 2.0)"
|
|
);
|
|
|
|
// Test case 2: Within limits (short direction)
|
|
let current_position = -1.0;
|
|
let proposed_delta = -0.5; // Would go to -1.5 (within limit)
|
|
assert!(
|
|
enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Position -1.0 + (-0.5) = -1.5 should be allowed (max: 2.0)"
|
|
);
|
|
|
|
// Test case 3: At limit (exactly)
|
|
let current_position = 1.5;
|
|
let proposed_delta = 0.5; // Would go to 2.0 (exactly at limit)
|
|
assert!(
|
|
enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Position 1.5 + 0.5 = 2.0 should be allowed (exactly at limit)"
|
|
);
|
|
|
|
// Test case 4: Exceeds limit (long)
|
|
let current_position = 1.8;
|
|
let proposed_delta = 0.3; // Would go to 2.1 (exceeds limit)
|
|
assert!(
|
|
!enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Position 1.8 + 0.3 = 2.1 should be BLOCKED (exceeds max: 2.0)"
|
|
);
|
|
|
|
// Test case 5: Exceeds limit (short)
|
|
let current_position = -1.5;
|
|
let proposed_delta = -0.6; // Would go to -2.1 (exceeds limit)
|
|
assert!(
|
|
!enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Position -1.5 + (-0.6) = -2.1 should be BLOCKED (exceeds max: 2.0)"
|
|
);
|
|
|
|
// Test case 6: Zero delta (always allowed)
|
|
let current_position = 1.9;
|
|
let proposed_delta = 0.0; // No change
|
|
assert!(
|
|
enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Zero delta should always be allowed"
|
|
);
|
|
|
|
// Test case 7: Reducing position (always allowed)
|
|
let current_position = 2.5; // Over limit (shouldn't happen, but test defensively)
|
|
let proposed_delta = -0.5; // Reducing position
|
|
assert!(
|
|
enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Reducing position should always be allowed (risk reduction)"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_position_limit_zero_position() {
|
|
let max_position = 2.0;
|
|
let current_position = 0.0;
|
|
|
|
// Going long from zero
|
|
let proposed_delta = 1.5;
|
|
assert!(
|
|
enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Going long 1.5 from zero should be allowed"
|
|
);
|
|
|
|
// Going short from zero
|
|
let proposed_delta = -1.5;
|
|
assert!(
|
|
enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Going short -1.5 from zero should be allowed"
|
|
);
|
|
|
|
// Exceeding limit from zero (long)
|
|
let proposed_delta = 2.5;
|
|
assert!(
|
|
!enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Going long 2.5 from zero should be BLOCKED (exceeds 2.0)"
|
|
);
|
|
|
|
// Exceeding limit from zero (short)
|
|
let proposed_delta = -2.5;
|
|
assert!(
|
|
!enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Going short -2.5 from zero should be BLOCKED (exceeds 2.0)"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_position_limit_fractional_max() {
|
|
// Test with smaller max position (e.g., 0.6 for testing)
|
|
let max_position = 0.6;
|
|
|
|
// Within limits
|
|
let current_position = 0.3;
|
|
let proposed_delta = 0.2; // Would go to 0.5 (within 0.6)
|
|
assert!(
|
|
enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Position 0.3 + 0.2 = 0.5 should be allowed (max: 0.6)"
|
|
);
|
|
|
|
// Exceeds limits
|
|
let current_position = 0.5;
|
|
let proposed_delta = 0.15; // Would go to 0.65 (exceeds 0.6)
|
|
assert!(
|
|
!enforce_position_limit(current_position, proposed_delta, max_position),
|
|
"Position 0.5 + 0.15 = 0.65 should be BLOCKED (exceeds max: 0.6)"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_cash_reserve_enforcement() {
|
|
let min_reserve_pct = 0.20; // 20% minimum cash reserve
|
|
|
|
// Test case 1: Sufficient cash (30% reserve)
|
|
let current_cash = 30_000.0;
|
|
let total_portfolio = 100_000.0;
|
|
assert!(
|
|
enforce_cash_reserve(current_cash, total_portfolio, min_reserve_pct),
|
|
"30% cash reserve should be allowed (min: 20%)"
|
|
);
|
|
|
|
// Test case 2: Exactly at minimum (20% reserve)
|
|
let current_cash = 20_000.0;
|
|
let total_portfolio = 100_000.0;
|
|
assert!(
|
|
enforce_cash_reserve(current_cash, total_portfolio, min_reserve_pct),
|
|
"20% cash reserve should be allowed (exactly at min)"
|
|
);
|
|
|
|
// Test case 3: Below minimum (15% reserve)
|
|
let current_cash = 15_000.0;
|
|
let total_portfolio = 100_000.0;
|
|
assert!(
|
|
!enforce_cash_reserve(current_cash, total_portfolio, min_reserve_pct),
|
|
"15% cash reserve should be BLOCKED (below min: 20%)"
|
|
);
|
|
|
|
// Test case 4: Near-zero cash
|
|
let current_cash = 1_000.0;
|
|
let total_portfolio = 100_000.0;
|
|
assert!(
|
|
!enforce_cash_reserve(current_cash, total_portfolio, min_reserve_pct),
|
|
"1% cash reserve should be BLOCKED (below min: 20%)"
|
|
);
|
|
|
|
// Test case 5: 100% cash (edge case)
|
|
let current_cash = 100_000.0;
|
|
let total_portfolio = 100_000.0;
|
|
assert!(
|
|
enforce_cash_reserve(current_cash, total_portfolio, min_reserve_pct),
|
|
"100% cash reserve should be allowed"
|
|
);
|
|
|
|
// Test case 6: Zero portfolio (edge case - should allow)
|
|
let current_cash = 0.0;
|
|
let total_portfolio = 0.0;
|
|
assert!(
|
|
enforce_cash_reserve(current_cash, total_portfolio, min_reserve_pct),
|
|
"Zero portfolio should be allowed (no trading happening)"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_cash_reserve_different_thresholds() {
|
|
let total_portfolio = 50_000.0;
|
|
|
|
// Test 10% threshold
|
|
let min_reserve_10pct = 0.10;
|
|
let cash_5pct = 2_500.0; // 5% cash
|
|
let cash_12pct = 6_000.0; // 12% cash
|
|
|
|
assert!(
|
|
!enforce_cash_reserve(cash_5pct, total_portfolio, min_reserve_10pct),
|
|
"5% cash should be BLOCKED with 10% minimum"
|
|
);
|
|
assert!(
|
|
enforce_cash_reserve(cash_12pct, total_portfolio, min_reserve_10pct),
|
|
"12% cash should be allowed with 10% minimum"
|
|
);
|
|
|
|
// Test 30% threshold
|
|
let min_reserve_30pct = 0.30;
|
|
let cash_25pct = 12_500.0; // 25% cash
|
|
let cash_35pct = 17_500.0; // 35% cash
|
|
|
|
assert!(
|
|
!enforce_cash_reserve(cash_25pct, total_portfolio, min_reserve_30pct),
|
|
"25% cash should be BLOCKED with 30% minimum"
|
|
);
|
|
assert!(
|
|
enforce_cash_reserve(cash_35pct, total_portfolio, min_reserve_30pct),
|
|
"35% cash should be allowed with 30% minimum"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_cash_reserve_negative_values() {
|
|
let min_reserve_pct = 0.20;
|
|
let total_portfolio = 100_000.0;
|
|
|
|
// Negative cash (margin/leverage scenario)
|
|
let negative_cash = -10_000.0;
|
|
assert!(
|
|
!enforce_cash_reserve(negative_cash, total_portfolio, min_reserve_pct),
|
|
"Negative cash should always be BLOCKED"
|
|
);
|
|
}
|