// 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 ); }