Files
foxhunt/crates/ml/tests/bug16_portfolio_features_test.rs
jgrusewski 448b61d095 refactor: collapse 9-level to 7-level ExposureLevel — eliminate degenerate Flat variants
The 4-branch DQN (direction x magnitude) had 3 degenerate variants
(Short25, Flat, Long25) that all mapped to 0.0 target exposure when
direction=Flat, causing 82% Flat collapse. Collapse these into a
single Flat variant, giving 7 levels (ShortSmall/Half/Full, Flat,
LongSmall/Half/Full) and 63 total factored actions (7x3x3).

- ExposureLevel enum: 9 variants -> 7 (add direction/magnitude/from_dir_mag)
- FactoredAction: 81 -> 63 total actions, from_index/to_index updated
- DQN epsilon-greedy: use from_dir_mag() instead of dir*3+mag indexing
- DQN config: num_actions default 9 -> 7
- PPO action space: 45 -> 63 actions, action masking updated
- Signal adapter CUDA kernel: 5-bin -> 7-bin exposure aggregation
- All tests updated for new variant names and index ranges

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 11:54:09 +02:00

302 lines
10 KiB
Rust

#![allow(
clippy::assertions_on_constants,
clippy::assertions_on_result_states,
clippy::clone_on_copy,
clippy::decimal_literal_representation,
clippy::doc_markdown,
clippy::empty_line_after_doc_comments,
clippy::field_reassign_with_default,
clippy::get_unwrap,
clippy::identity_op,
clippy::inconsistent_digit_grouping,
clippy::indexing_slicing,
clippy::integer_division,
clippy::len_zero,
clippy::let_underscore_must_use,
clippy::manual_div_ceil,
clippy::manual_let_else,
clippy::manual_range_contains,
clippy::modulo_arithmetic,
clippy::needless_range_loop,
clippy::non_ascii_literal,
clippy::redundant_clone,
clippy::shadow_reuse,
clippy::shadow_same,
clippy::shadow_unrelated,
clippy::single_match_else,
clippy::str_to_string,
clippy::string_slice,
clippy::tests_outside_test_module,
clippy::too_many_lines,
clippy::unnecessary_wraps,
clippy::unseparated_literal_suffix,
clippy::use_debug,
clippy::useless_vec,
clippy::wildcard_enum_match_arm,
clippy::else_if_without_else,
clippy::expect_used,
clippy::missing_const_for_fn,
clippy::similar_names,
clippy::type_complexity,
clippy::collapsible_else_if,
clippy::doc_lazy_continuation,
clippy::items_after_test_module,
clippy::map_clone,
clippy::multiple_unsafe_ops_per_block,
clippy::unwrap_or_default,
clippy::assign_op_pattern,
clippy::needless_borrow,
clippy::println_empty_string,
clippy::unnecessary_cast,
clippy::used_underscore_binding,
clippy::create_dir,
clippy::implicit_saturating_sub,
clippy::exit,
clippy::expect_fun_call,
clippy::too_many_arguments,
clippy::unnecessary_map_or,
clippy::unwrap_used,
dead_code,
unused_imports,
unused_variables,
clippy::cloned_ref_to_slice_refs,
clippy::neg_multiply,
clippy::while_let_loop,
clippy::bool_assert_comparison,
clippy::excessive_precision,
clippy::trivially_copy_pass_by_ref,
clippy::op_ref,
clippy::redundant_closure,
clippy::unnecessary_lazy_evaluations,
clippy::if_then_some_else_none,
clippy::unnecessary_to_owned,
clippy::single_component_path_imports,
)]
//! Bug #16 Portfolio Features Test
//!
//! Verifies that portfolio_features are populated from PortfolioTracker during training,
//! not just hardcoded defaults.
//!
//! # Bug #16 Context
//! Before fix: portfolio_features were set based on previous tracker state in train_step()
//! After fix: portfolio_features should update correctly during training to reflect
//! actual portfolio changes (position, value, spread)
//!
//! This test verifies:
//! 1. PortfolioTracker state changes when actions are executed
//! 2. Portfolio features reflect actual tracker state (not hardcoded [1.0, 0.0, 0.0001])
//! 3. Values update correctly across multiple actions
#![allow(unused_crate_dependencies)]
use ml::dqn::action_space::{ExposureLevel, FactoredAction, OrderType, Urgency};
use ml::dqn::portfolio_tracker::PortfolioTracker;
use tracing::info;
/// Helper: Create a BUY action (Long100)
fn create_buy_action() -> FactoredAction {
FactoredAction::new(ExposureLevel::LongFull, OrderType::Market, Urgency::Normal)
}
/// Helper: Create a SELL action (Short100)
fn create_sell_action() -> FactoredAction {
FactoredAction::new(ExposureLevel::ShortSmall, OrderType::Market, Urgency::Normal)
}
/// Helper: Create a HOLD action (Flat)
fn create_hold_action() -> FactoredAction {
FactoredAction::new(ExposureLevel::Flat, OrderType::Market, Urgency::Normal)
}
#[test]
fn test_bug16_portfolio_tracker_state_changes_on_action() {
// Verify that PortfolioTracker state changes when actions are executed
// This is the foundation for Bug #16 fix
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 0.0);
// Initial state
let initial_value = tracker.total_value(4500.0);
let initial_position = tracker.current_position();
assert_eq!(initial_position, 0.0, "Initial position should be 0");
assert!((initial_value - 10_000.0).abs() < 1.0, "Initial value should be ~$10,000");
// Execute BUY action at $4500
let buy_action = create_buy_action();
tracker.execute_action(buy_action, 4500.0, 2.0);
// Position should now be non-zero
let position_after_buy = tracker.current_position();
assert_ne!(position_after_buy, 0.0, "Position should be non-zero after BUY");
assert!(position_after_buy > 0.0, "Position should be positive (long) after BUY");
// Get portfolio features
let features_after_buy = tracker.get_portfolio_features(4500.0);
info!(?features_after_buy, "Portfolio features after BUY");
assert_eq!(features_after_buy.len(), 3, "Should have 3 portfolio features");
assert_ne!(
features_after_buy[1], 0.0,
"Position feature should be non-zero after BUY"
);
}
#[test]
fn test_bug16_portfolio_features_not_hardcoded() {
// Verify that portfolio features reflect actual tracker state, not hardcoded values
// This directly tests the Bug #16 fix
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 0.0);
// Execute BUY action
let buy_action = create_buy_action();
tracker.execute_action(buy_action, 4500.0, 2.0);
// Get portfolio features at same price
let features_at_4500 = tracker.get_portfolio_features(4500.0);
info!(?features_at_4500, "Features at $4500");
// Verify features[0] is portfolio value (not hardcoded 1.0)
assert_ne!(
features_at_4500[0], 1.0,
"Bug #16 fix should populate portfolio_features[0] from tracker, not hardcoded 1.0"
);
// Verify features[1] is position (not hardcoded 0.0)
assert_ne!(
features_at_4500[1], 0.0,
"Bug #16 fix should populate portfolio_features[1] from tracker, not hardcoded 0.0"
);
// Verify features[2] is spread (can be default 0.0001)
assert!(
(features_at_4500[2] - 0.0001).abs() < 0.00001,
"portfolio_features[2] should be spread 0.0001"
);
// Now check features at different price (value should change)
let features_at_4510 = tracker.get_portfolio_features(4510.0);
info!(?features_at_4510, "Features at $4510");
// Portfolio value should change with price (we're long)
assert_ne!(
features_at_4510[0], features_at_4500[0],
"Portfolio value should change when price changes (holding long position)"
);
// Position feature may differ slightly due to rounding/normalization
// but should still be non-zero (we're still long)
assert!(
features_at_4510[1].abs() > 0.0,
"Position should still be non-zero (we're holding a long position)"
);
}
#[test]
fn test_bug16_portfolio_features_update_across_actions() {
// Verify portfolio features update correctly across multiple actions
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 0.0);
// Step 1: BUY at $4500 (Long100)
let buy_action = create_buy_action();
tracker.execute_action(buy_action, 4500.0, 2.0);
let features_after_buy = tracker.get_portfolio_features(4500.0);
let position_after_buy = features_after_buy[1];
let value_after_buy = features_after_buy[0];
info!(?features_after_buy, "After BUY - Features");
assert!(position_after_buy > 0.0, "Position should be positive (long) after BUY");
// Step 2: HOLD/Flat at $4510 (closes position to 0)
// Note: Flat means "close position" (0% exposure), not "maintain position"
let hold_action = create_hold_action();
tracker.execute_action(hold_action, 4510.0, 2.0);
let features_after_hold = tracker.get_portfolio_features(4510.0);
let position_after_hold = features_after_hold[1];
let value_after_hold = features_after_hold[0];
info!(?features_after_hold, "After HOLD/Flat - Features");
// Position should be 0 (Flat closes positions)
assert_eq!(
position_after_hold, 0.0,
"Position should be 0 after Flat action (closes position)"
);
// Value should still be positive (we made profit from $4500→$4510 move)
assert!(
value_after_hold > value_after_buy,
"Portfolio value should be higher (profited from long position before closing)"
);
// Step 3: SELL at $4510 (opens short position)
let sell_action = create_sell_action();
tracker.execute_action(sell_action, 4510.0, 2.0);
let features_after_sell = tracker.get_portfolio_features(4510.0);
let position_after_sell = features_after_sell[1];
info!(?features_after_sell, "After SELL - Features");
// After SELL (Short100), position should be negative
assert!(
position_after_sell < 0.0,
"Position should be negative after SELL (short position)"
);
assert_ne!(
position_after_sell, position_after_hold,
"Position should change from 0 to negative after SELL action"
);
}
#[test]
fn test_bug16_portfolio_value_reflects_pnl() {
// Verify that portfolio value reflects actual P&L from trading
let mut tracker = PortfolioTracker::new(10_000.0, 0.0001, 0.0);
let initial_value = tracker.total_value(4500.0);
info!(initial_value, "Initial portfolio value");
// BUY at $4500
let buy_action = create_buy_action();
tracker.execute_action(buy_action, 4500.0, 2.0);
// Check value at $4510 (price went up $10)
let value_at_4510 = tracker.total_value(4510.0);
info!(value_at_4510, "Portfolio value at $4510 (after buy)");
// Since we're long, value should be higher than initial
// (we profited from the $10 price increase)
assert!(
value_at_4510 > initial_value,
"Portfolio value should increase when long position profits"
);
// Check value at $4490 (price went down $10 from entry)
let value_at_4490 = tracker.total_value(4490.0);
info!(value_at_4490, "Portfolio value at $4490 (after buy)");
// Since we're long, value should be lower than initial
// (we lost from the $10 price decrease)
assert!(
value_at_4490 < initial_value,
"Portfolio value should decrease when long position loses"
);
}
#[test]
fn test_bug16_spread_feature_populated() {
// Verify spread feature is populated (even if default)
let tracker = PortfolioTracker::new(10_000.0, 0.0001, 0.0);
let features = tracker.get_portfolio_features(4500.0);
assert_eq!(features.len(), 3, "Should have 3 portfolio features");
assert!(
(features[2] - 0.0001).abs() < 0.00001,
"Spread feature should be populated with default 0.0001"
);
}