Files
foxhunt/crates/ml/tests/debug_position_delta_test.rs
jgrusewski 9c3d741a08 refactor: restructure repo — crates/, bin/, testing/ layout
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>
2026-02-25 11:56:00 +01:00

133 lines
5.1 KiB
Rust

//! Debug test to trace position_delta sign logic
//!
//! This test verifies whether position_delta is positive or negative
//! when going from FLAT to LONG.
#![allow(unused_crate_dependencies)]
use ml::dqn::action_space::{ExposureLevel, FactoredAction, OrderType, Urgency};
use ml::dqn::portfolio_tracker::PortfolioTracker;
#[test]
fn test_position_delta_sign_when_buying() {
let mut tracker = PortfolioTracker::new(100_000.0, 0.0001, 1.0);
// Trace internal state
println!("=== INITIAL STATE ===");
println!("Cash: ${:.2}", tracker.cash_balance());
println!("Position: {:.2} contracts", tracker.current_position());
// Execute: Go from FLAT (0) to LONG (1.0 contract)
let action = FactoredAction::new(
ExposureLevel::Long100, // target_exposure = +1.0
OrderType::Market,
Urgency::Normal
);
println!("\n=== EXECUTING ACTION ===");
println!("Action: Long100 (target_exposure = +1.0)");
println!("Price: $5,600.00");
println!("max_position parameter: 1.0");
// Execute the action
tracker.execute_action(action, 5600.0, 1.0);
println!("\n=== AFTER EXECUTION ===");
println!("Cash: ${:.2}", tracker.cash_balance());
println!("Position: {:.2} contracts", tracker.current_position());
println!("Portfolio Value: ${:.2}", tracker.total_value(5600.0));
// Manually calculate what position_delta should have been
let target_exposure: f32 = 1.0; // Long100
let max_position: f32 = 1.0;
let target_position: f32 = target_exposure * max_position; // 1.0
let clamped_position: f32 = target_position.clamp(-1.0, 1.0); // 1.0
let initial_position: f32 = 0.0;
let position_delta: f32 = clamped_position - initial_position; // 1.0 - 0.0 = +1.0
println!("\n=== MANUAL CALCULATION ===");
println!("target_position: {:.2}", target_position);
println!("clamped_position: {:.2}", clamped_position);
println!("initial_position: {:.2}", initial_position);
println!("position_delta: {:.2} (should be POSITIVE for buying)", position_delta);
// Expected behavior:
// - position_delta = +1.0 (POSITIVE when buying/going long)
// - Line 268 checks `if position_delta < 0.0` → FALSE
// - Cash check BYPASSED for buying!
println!("\n=== SIGN LOGIC ANALYSIS ===");
if position_delta < 0.0 {
println!("❌ position_delta < 0.0: Cash check would RUN (but this is a BUY!)");
} else {
println!("✅ position_delta >= 0.0: Cash check BYPASSED (THIS IS THE BUG!)");
}
// Expected cash after buying 1 contract at $5,600:
// cash = 100,000 + (+1.0 * 5,600) - transaction_cost
// But V6 formula says: cash += position_delta * price - cost
// = 100,000 + (1.0 * 5,600) - cost = 105,600 - cost (ADDS cash when buying!)
let expected_cash = 100_000.0 - (1.0 * 5600.0) - (1.0 * 5600.0 * 0.0015);
println!("\n=== EXPECTED VS ACTUAL ===");
println!("Expected cash (correct): ${:.2}", expected_cash);
println!("Actual cash: ${:.2}", tracker.cash_balance());
// The bug: position_delta is POSITIVE when buying, so:
// 1. Line 268 check fails (position_delta < 0.0 → false)
// 2. Cash check bypassed
// 3. Line 310: cash += position_delta * price - cost
// = 100,000 + (+1.0 * 5,600) - 8.4 = 105,591.60 (ADDS cash!)
if tracker.cash_balance() > 100_000.0 {
println!("\n❌ BUG CONFIRMED: Cash INCREASED when buying (should decrease)");
} else {
println!("\n✅ Cash correctly decreased when buying");
}
}
#[test]
fn test_position_delta_sign_when_selling() {
let mut tracker = PortfolioTracker::new(100_000.0, 0.0001, 1.0);
println!("\n=== TEST 2: GOING SHORT ===");
println!("Initial: FLAT (0 contracts) → Short100 (-1.0 contracts)");
let action = FactoredAction::new(
ExposureLevel::Short100, // target_exposure = -1.0
OrderType::Market,
Urgency::Normal
);
tracker.execute_action(action, 5600.0, 1.0);
// Calculate position_delta
let target_exposure: f32 = -1.0; // Short100
let target_position: f32 = target_exposure * 1.0; // -1.0
let clamped_position: f32 = target_position.clamp(-1.0, 1.0); // -1.0
let position_delta: f32 = clamped_position - 0.0; // -1.0 - 0.0 = -1.0
println!("position_delta: {:.2} (NEGATIVE for selling/going short)", position_delta);
if position_delta < 0.0 {
println!("✅ position_delta < 0.0: Cash check would RUN (but this is a SELL, not a BUY!)");
} else {
println!("❌ position_delta >= 0.0: Cash check BYPASSED");
}
// Expected cash after shorting 1 contract at $5,600:
// Should GAIN $5,600 minus transaction cost
let expected_cash = 100_000.0 + (1.0 * 5600.0) - (1.0 * 5600.0 * 0.0015);
println!("Expected cash: ${:.2}", expected_cash);
println!("Actual cash: ${:.2}", tracker.cash_balance());
// Line 310: cash += position_delta * price - cost
// = 100,000 + (-1.0 * 5,600) - 8.4 = 94,391.60 (SUBTRACTS cash!)
if tracker.cash_balance() < 100_000.0 {
println!("❌ BUG CONFIRMED: Cash DECREASED when selling short (should increase)");
} else {
println!("✅ Cash correctly increased when selling short");
}
}