- Reduce CI GPU test datasets 16x for walltime reduction - Reduce early-stop epochs 50→10, add --test-threads=1 - Serialize all GPU lib tests to prevent cuBLAS init race - Align state_dim to 16 for BF16 tensor core HMMA dispatch - BF16 precision tolerance in ml-dqn tests - Enable branching DQN + tracing subscriber in smoke tests - Prevent min_replay_size > buffer_size deadlock in early-stop tests - Prevent AutoReplaySizer from breaking gradient collapse warmup - Replace racy tokio::spawn checkpoint counter with AtomicUsize - Set warmup_steps=0 and max_training_steps_per_epoch=300 in early-stop tests - RealDataLoader respects TEST_DATA_DIR for CI PVC layout - Add collapse_warmup_capacity to gpu_smoketest DQNConfig - Drain CUDA context between test binaries - Detached HEAD checkout prevents local branch corruption - GPU pipeline tests: fix BF16 dtype and rank-1 squeeze assertions - OOD input handling tests use use_gpu: true Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
202 lines
6.9 KiB
Rust
202 lines
6.9 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,
|
|
)]
|
|
//! 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;
|
|
use tracing::info;
|
|
|
|
#[test]
|
|
fn test_position_delta_sign_when_buying() {
|
|
let mut tracker = PortfolioTracker::new(100_000.0, 0.0001, 1.0);
|
|
|
|
// Trace internal state
|
|
info!(cash = tracker.cash_balance(), position = tracker.current_position(), "INITIAL STATE");
|
|
|
|
// Execute: Go from FLAT (0) to LONG (1.0 contract)
|
|
let action = FactoredAction::new(
|
|
ExposureLevel::Long100, // target_exposure = +1.0
|
|
OrderType::Market,
|
|
Urgency::Normal
|
|
);
|
|
|
|
info!("EXECUTING ACTION: Long100 (target_exposure = +1.0), Price: $5,600.00, max_position: 1.0");
|
|
|
|
// Execute the action
|
|
tracker.execute_action(action, 5600.0, 1.0);
|
|
|
|
info!(
|
|
cash = tracker.cash_balance(),
|
|
position = tracker.current_position(),
|
|
portfolio_value = tracker.total_value(5600.0),
|
|
"AFTER EXECUTION"
|
|
);
|
|
|
|
// 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
|
|
|
|
info!(
|
|
target_position,
|
|
clamped_position,
|
|
initial_position,
|
|
position_delta,
|
|
"MANUAL CALCULATION (position_delta should be POSITIVE for buying)"
|
|
);
|
|
|
|
// 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!
|
|
|
|
if position_delta < 0.0 {
|
|
info!("SIGN LOGIC: position_delta < 0.0 — Cash check would RUN (but this is a BUY!)");
|
|
} else {
|
|
info!("SIGN LOGIC: 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);
|
|
info!(expected_cash, actual_cash = tracker.cash_balance(), "EXPECTED VS ACTUAL cash");
|
|
|
|
// 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 {
|
|
info!("BUG CONFIRMED: Cash INCREASED when buying (should decrease)");
|
|
} else {
|
|
info!("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);
|
|
|
|
info!("TEST 2: GOING SHORT — Initial: FLAT (0 contracts) to 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
|
|
|
|
info!(position_delta, "position_delta (NEGATIVE for selling/going short)");
|
|
|
|
if position_delta < 0.0 {
|
|
info!("position_delta < 0.0: Cash check would RUN (but this is a SELL, not a BUY!)");
|
|
} else {
|
|
info!("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);
|
|
info!(expected_cash, actual_cash = tracker.cash_balance(), "Expected vs actual cash");
|
|
|
|
// 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 {
|
|
info!("BUG CONFIRMED: Cash DECREASED when selling short (should increase)");
|
|
} else {
|
|
info!("Cash correctly increased when selling short");
|
|
}
|
|
}
|