Move 17 library crates into crates/, CLI binary into bin/fxt, consolidate 10 test crates into testing/, split config crate from deployment config files. Root directory reduced from 38+ to ~17 directories. All Cargo.toml paths and build.rs proto refs updated. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
227 lines
8.3 KiB
Rust
227 lines
8.3 KiB
Rust
//! 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;
|
|
|
|
/// Helper: Create a BUY action (Long100)
|
|
fn create_buy_action() -> FactoredAction {
|
|
FactoredAction::new(ExposureLevel::Long100, OrderType::Market, Urgency::Normal)
|
|
}
|
|
|
|
/// Helper: Create a SELL action (Short100)
|
|
fn create_sell_action() -> FactoredAction {
|
|
FactoredAction::new(ExposureLevel::Short100, 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);
|
|
println!("Portfolio features after BUY: {:?}", 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);
|
|
println!("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);
|
|
println!("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];
|
|
println!("After BUY - Features: {:?}", features_after_buy);
|
|
|
|
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];
|
|
println!("After HOLD/Flat - Features: {:?}", features_after_hold);
|
|
|
|
// 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];
|
|
println!("After SELL - Features: {:?}", features_after_sell);
|
|
|
|
// 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);
|
|
println!("Initial portfolio value: ${:.2}", initial_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);
|
|
println!("Portfolio value at $4510 (after buy): ${:.2}", value_at_4510);
|
|
|
|
// 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);
|
|
println!("Portfolio value at $4490 (after buy): ${:.2}", value_at_4490);
|
|
|
|
// 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"
|
|
);
|
|
}
|