Files
foxhunt/crates/ml/tests/test_sell_bug.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

46 lines
1.4 KiB
Rust

// Test to verify SELL action closes long positions correctly
use ml::dqn::portfolio_tracker::{PortfolioTracker, TradeAction};
#[test]
fn test_sell_closes_long_position() {
let mut tracker = PortfolioTracker::with_default_spread(10_000.0);
// Initial state
assert_eq!(tracker.cash_balance(), 10_000.0);
assert_eq!(tracker.current_position(), 0.0);
// BUY 50 @ $100
tracker.execute_trade(TradeAction::Buy(50.0), 100.0);
println!(
"After BUY: cash={}, position={}",
tracker.cash_balance(),
tracker.current_position()
);
assert_eq!(tracker.current_position(), 50.0);
assert_eq!(tracker.cash_balance(), 5_000.0); // 10,000 - 5,000
// SELL 50 @ $50 (should close position)
tracker.execute_trade(TradeAction::Sell(50.0), 50.0);
let final_cash = tracker.cash_balance();
let final_position = tracker.current_position();
println!(
"After SELL: cash={}, position={}",
final_cash, final_position
);
// Expected results:
// Position should be closed (0)
// Cash should be: 5,000 (remaining) + 50*50 (sale proceeds) = 7,500
assert_eq!(
final_position, 0.0,
"Position should be closed, got {}",
final_position
);
assert_eq!(
final_cash, 7_500.0,
"Cash should be 7,500 after closing position at loss, got {}",
final_cash
);
}