use chrono::{DateTime, Utc}; use parking_lot::RwLock; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::Arc; use crate::{ orderbook::OrderBookManager, positions::PositionTracker, risk::RiskEngine, ExecutionHandler, Fill, MarketDataSource, MarketDataType, MarketUpdate, Order, Side, TimeProvider, TradingMode, }; use super::{ BacktestConfig, BacktestEvent, BacktestResult, BacktestState, EventQueue, EventType, Signal, SignalType, Strategy, TradeTracker, }; pub struct BacktestEngine { config: BacktestConfig, state: Arc>, event_queue: Arc>, strategies: Arc>>>, // Core components position_tracker: Arc, risk_engine: Arc, orderbook_manager: Arc, time_provider: Arc>, pub market_data_source: Arc>>, execution_handler: Arc>>, // Metrics total_trades: usize, profitable_trades: usize, total_pnl: f64, // Price tracking - single source of truth // Maps symbol -> (timestamp, price) last_prices: HashMap, f64)>, // Store latest bar data for each symbol to ensure accurate closing prices latest_bars: HashMap, // Store all OHLC data processed by the engine ohlc_history: HashMap, crate::Bar)>>, // Trade tracking trade_tracker: TradeTracker, } impl BacktestEngine { pub fn new( config: BacktestConfig, mode: TradingMode, time_provider: Box, market_data_source: Box, execution_handler: Box, ) -> Self { let state = Arc::new(RwLock::new(BacktestState::new( config.initial_capital, config.start_time, ))); Self { config, state, event_queue: Arc::new(RwLock::new(EventQueue::new())), strategies: Arc::new(RwLock::new(Vec::new())), position_tracker: Arc::new(PositionTracker::new()), risk_engine: Arc::new(RiskEngine::new()), orderbook_manager: Arc::new(OrderBookManager::new()), time_provider: Arc::new(time_provider), market_data_source: Arc::new(RwLock::new(market_data_source)), execution_handler: Arc::new(RwLock::new(execution_handler)), total_trades: 0, profitable_trades: 0, total_pnl: 0.0, last_prices: HashMap::new(), latest_bars: HashMap::new(), ohlc_history: HashMap::new(), trade_tracker: TradeTracker::new(), } } pub fn update_risk_limits(&self, limits: crate::RiskLimits) { self.risk_engine.update_limits(limits); } pub fn add_strategy(&mut self, strategy: Box) { self.strategies.write().push(strategy); } pub async fn run(&mut self) -> Result { // Initialize start time if let Some(simulated_time) = self .time_provider .as_any() .downcast_ref::() { simulated_time.advance_to(self.config.start_time); } // Load market data self.load_market_data().await?; let queue_len = self.event_queue.read().len(); if queue_len == 0 { return Err("No market data loaded".to_string()); } // Main event loop - process events grouped by timestamp let mut iteration = 0; let mut last_update_time = self.config.start_time; while !self.event_queue.read().is_empty() { iteration += 1; // Get the next event's timestamp let next_event_time = self.event_queue.read().peek_next().map(|e| e.timestamp); if let Some(event_time) = next_event_time { // Advance time to the next event self.advance_time(event_time); // Get all events at this timestamp let current_time = self.time_provider.now(); let events = self.event_queue.write().pop_until(current_time); // Process all events at this timestamp for event in events { self.process_event(event).await?; } // Only update portfolio value if time has actually advanced // This ensures we have prices for all symbols at this timestamp if current_time > last_update_time { self.update_portfolio_value(); last_update_time = current_time; } } else { // No more events break; } } // Close all open positions at market prices self.close_all_positions().await?; // Include closing costs in the final mark, replacing the same-time sample. self.update_portfolio_value(); // Generate results Ok(self.generate_results()) } async fn load_market_data(&mut self) -> Result<(), String> { let mut data_source = self.market_data_source.write(); data_source.seek_to_time(self.config.start_time)?; // Load all data into event queue while let Some(update) = data_source.get_next_update().await { if update.timestamp > self.config.end_time { break; } let event = BacktestEvent::market_data(update.timestamp, update); self.event_queue.write().push(event); } Ok(()) } async fn process_event(&mut self, event: BacktestEvent) -> Result<(), String> { match event.event_type { EventType::MarketData(data) => { self.process_market_data(data).await?; } EventType::OrderSubmitted(order) => { self.process_order_submission(order).await?; } EventType::OrderFilled(_fill) => { // Fills are already processed when orders are executed // This event is just for recording // Note: We now record fills in process_fill with symbol info } EventType::OrderCancelled(order_id) => { self.process_order_cancellation(&order_id)?; } EventType::TimeUpdate(time) => { self.advance_time(time); } } Ok(()) } async fn process_market_data(&mut self, data: MarketUpdate) -> Result<(), String> { // Update price tracking - single source of truth let price = match &data.data { MarketDataType::Bar(bar) => { // Log OHLCV data for each tick eprintln!( "[TICK] {} @ {} - O:{:.2} H:{:.2} L:{:.2} C:{:.2} V:{:.0}", data.symbol, data.timestamp.format("%Y-%m-%d %H:%M:%S"), bar.open, bar.high, bar.low, bar.close, bar.volume ); // Store the complete bar data for accurate position closing self.latest_bars.insert(data.symbol.clone(), bar.clone()); // Store in OHLC history self.ohlc_history .entry(data.symbol.clone()) .or_insert_with(Vec::new) .push((data.timestamp, bar.clone())); // Debug: Log OHLC history size periodically let history_len = self .ohlc_history .get(&data.symbol) .map(|v| v.len()) .unwrap_or(0); if history_len == 1 || history_len % 100 == 0 { eprintln!( "[DEBUG] OHLC history for {}: {} bars stored", data.symbol, history_len ); } bar.close } MarketDataType::Quote(quote) => { // Use mid price for quotes (quote.bid + quote.ask) / 2.0 } MarketDataType::Trade(trade) => trade.price, }; // Store price with timestamp - this is our source of truth self.last_prices .insert(data.symbol.clone(), (data.timestamp, price)); self.risk_engine.mark_price(&data.symbol, price); self.risk_engine.roll_day(data.timestamp.date_naive()); // Convert to simpler MarketData for strategies let market_data = self.convert_to_market_data(&data); // Send to strategies let mut all_signals = Vec::new(); { let mut strategies = self.strategies.write(); for strategy in strategies.iter_mut() { let signals = strategy.on_market_data(&market_data); all_signals.extend(signals); } } // Process signals for signal in all_signals { self.process_signal(signal).await?; } // Check pending orders for fills self.check_pending_orders(&data).await?; // Don't update portfolio value here - wait until all events at this timestamp are processed Ok(()) } fn convert_to_market_data(&self, update: &MarketUpdate) -> MarketUpdate { // MarketData is a type alias for MarketUpdate update.clone() } async fn process_signal(&mut self, signal: Signal) -> Result<(), String> { // Only process strong signals if signal.strength.abs() < 0.7 { return Ok(()); } // Convert signal to order let order = self.signal_to_order(signal)?; // Submit order self.process_order_submission(order).await } fn signal_to_order(&self, signal: Signal) -> Result { let quantity = signal.quantity.unwrap_or_else(|| { // Calculate position size based on portfolio self.calculate_position_size(&signal.symbol, signal.strength) }); let side = match signal.signal_type { SignalType::Buy => Side::Buy, SignalType::Sell => Side::Sell, SignalType::Close => { // Determine side based on current position let position = self.position_tracker.get_position(&signal.symbol); if position.as_ref().map(|p| p.quantity > 0.0).unwrap_or(false) { Side::Sell } else { Side::Buy } } }; Ok(crate::Order { id: format!("order_{}", uuid::Uuid::new_v4()), symbol: signal.symbol, side, quantity, order_type: crate::OrderType::Market, time_in_force: crate::TimeInForce::Day, }) } async fn process_order_submission(&mut self, order: Order) -> Result<(), String> { // Risk checks // Get current position for the symbol let current_position = self .position_tracker .get_position(&order.symbol) .map(|p| p.quantity); let price = self .last_prices .get(&order.symbol) .map(|(_, price)| *price) .unwrap_or(f64::NAN); let price = if order.side == Side::Buy { price * (1.0 + self.config.slippage) } else { price * (1.0 - self.config.slippage) }; let risk_check = self .risk_engine .check_order(&order, current_position, price); if !risk_check.passed { return Err(format!("Risk check failed: {:?}", risk_check.violations)); } // Add to pending orders self.state.write().add_pending_order(order.clone()); // For market orders in backtesting, fill immediately if matches!(order.order_type, crate::OrderType::Market) { self.check_order_fill(&order).await?; } Ok(()) } async fn check_pending_orders(&mut self, market_data: &MarketUpdate) -> Result<(), String> { let orders_to_check: Vec = { let state = self.state.read(); state .pending_orders .values() .filter(|o| o.symbol == market_data.symbol) .cloned() .collect() }; for order in orders_to_check { self.check_order_fill(&order).await?; } Ok(()) } async fn check_order_fill(&mut self, order: &Order) -> Result<(), String> { let current_time = self.time_provider.now(); // Get current market price let (price_time, base_price) = self .last_prices .get(&order.symbol) .copied() .ok_or_else(|| format!("No price available for symbol: {}", order.symbol))?; // Apply slippage let fill_price = match order.side { crate::Side::Buy => base_price * (1.0 + self.config.slippage), crate::Side::Sell => base_price * (1.0 - self.config.slippage), }; // Get the OHLC data that was used let ohlc_info = if let Some(bar) = self.latest_bars.get(&order.symbol) { format!( "O:{:.2} H:{:.2} L:{:.2} C:{:.2}", bar.open, bar.high, bar.low, bar.close ) } else { "No OHLC data".to_string() }; // Log trade execution eprintln!( "[TRADE] {} {} {} @ {:.2} | OHLC: {} | Time: {}", match order.side { crate::Side::Buy => "BUY", crate::Side::Sell => "SELL", }, order.quantity, order.symbol, fill_price, ohlc_info, current_time.format("%Y-%m-%d %H:%M:%S") ); // Create fill let fill = crate::Fill { timestamp: current_time, price: fill_price, quantity: order.quantity, commission: order.quantity * fill_price * self.config.commission, }; // Process the fill self.process_fill(&order, fill).await } async fn process_fill( &mut self, order: &crate::Order, fill: crate::Fill, ) -> Result<(), String> { // Remove from pending orders self.state.write().remove_pending_order(&order.id); // Get position before the fill let position_before = self .position_tracker .get_position(&order.symbol) .map(|p| p.quantity) .unwrap_or(0.0); let previous_pnl = self .position_tracker .get_position(&order.symbol) .map(|p| p.realized_pnl) .unwrap_or(0.0); // Update positions let update = self .position_tracker .process_fill(&order.symbol, &fill, order.side); self.risk_engine.update_position( &order.symbol, update.resulting_position.quantity, fill.price, ); self.risk_engine .update_daily_pnl(update.resulting_position.realized_pnl - previous_pnl); // Calculate P&L if position was reduced/closed let pnl = if update.resulting_position.realized_pnl != 0.0 { Some(update.resulting_position.realized_pnl - previous_pnl) } else { None }; // Get position after this fill let position_after = update.resulting_position.quantity; // Record the fill with position and P&L information self.state.write().record_fill( order.symbol.clone(), order.side, fill.clone(), position_after, position_before, pnl, ); // Track trades self.trade_tracker .process_fill(&order.symbol, order.side, &fill); // Update cash let cash_change = match order.side { crate::Side::Buy => -(fill.quantity * fill.price + fill.commission), crate::Side::Sell => fill.quantity * fill.price - fill.commission, }; self.state.write().cash += cash_change; // Notify strategies { let mut strategies = self.strategies.write(); for strategy in strategies.iter_mut() { let side_str = match order.side { crate::Side::Buy => "buy", crate::Side::Sell => "sell", }; strategy.on_fill(&order.symbol, fill.quantity, fill.price, side_str); } } // Update metrics self.total_trades += 1; if update.resulting_position.realized_pnl > 0.0 { self.profitable_trades += 1; } self.total_pnl = self .position_tracker .get_all_positions() .iter() .map(|p| p.realized_pnl) .sum(); Ok(()) } fn process_order_cancellation(&mut self, order_id: &str) -> Result<(), String> { self.state.write().remove_pending_order(order_id); Ok(()) } fn advance_time(&mut self, time: DateTime) { if let Some(simulated_time) = self .time_provider .as_any() .downcast_ref::() { simulated_time.advance_to(time); } self.state.write().current_time = time; // Forward-fill prices: Update timestamps for all symbols to current time // This ensures we don't use stale prices when not all symbols have data at every timestamp let symbols: Vec = self.last_prices.keys().cloned().collect(); for symbol in symbols { if let Some((old_time, price)) = self.last_prices.get(&symbol).copied() { if old_time < time { // Update the timestamp to current time, keeping the same price (forward-fill) self.last_prices.insert(symbol.clone(), (time, price)); } } } } fn update_portfolio_value(&mut self) { let positions = self.position_tracker.get_all_positions(); let cash = self.state.read().cash; let mut portfolio_value = cash; for position in &positions { // Use last known price for the symbol let price = self .last_prices .get(&position.symbol) .map(|(_, p)| *p) .unwrap_or(position.average_price); // Calculate market value correctly for long and short positions let market_value = if position.quantity > 0.0 { // Long position: value = quantity * current_price position.quantity * price } else { // Short position: // We have a liability to buy back shares at current market price // This is a negative value that reduces portfolio value // Value = quantity * price (quantity is already negative) position.quantity * price }; portfolio_value += market_value; } self.state.write().update_portfolio_value(portfolio_value); } fn calculate_position_size(&self, symbol: &str, signal_strength: f64) -> f64 { let state = self.state.read(); let portfolio_value = state.portfolio_value; let cash = state.cash; // Use available cash, not total portfolio value for position sizing let allocation = 0.2; // 20% of available cash per position let position_value = cash.min(portfolio_value * allocation) * signal_strength.abs(); let price = self .last_prices .get(symbol) .map(|(_, p)| *p) .unwrap_or(100.0); (position_value / price).floor() } fn get_next_event_time(&self) -> Option> { // Get the timestamp of the next event in the queue self.event_queue .read() .peek_next() .map(|event| event.timestamp) } async fn close_all_positions(&mut self) -> Result<(), String> { // Ensure we have the most recent prices for symbols with positions let current_time = self.time_provider.now(); let positions = self.position_tracker.get_all_positions(); // First, ensure all positions have up-to-date prices for position in &positions { if position.quantity.abs() > 0.001 { // Check if we have bar data for this symbol if let Some(latest_bar) = self.latest_bars.get(&position.symbol) { // Use the close price from the latest bar self.last_prices .insert(position.symbol.clone(), (current_time, latest_bar.close)); } else if let Some((price_time, price)) = self.last_prices.get(&position.symbol).copied() { if price_time < current_time { // Update timestamp to current time for proper fill processing self.last_prices .insert(position.symbol.clone(), (current_time, price)); } } } } for position in positions { if position.quantity.abs() > 0.001 { // Create market order to close position let order = crate::Order { id: format!("close_{}", uuid::Uuid::new_v4()), symbol: position.symbol.clone(), side: if position.quantity > 0.0 { Side::Sell } else { Side::Buy }, quantity: position.quantity.abs(), order_type: crate::OrderType::Market, time_in_force: crate::TimeInForce::Day, }; // Process the closing order self.check_order_fill(&order).await?; } } Ok(()) } fn generate_results(&self) -> BacktestResult { let state = self.state.read(); let start_time = self.config.start_time; // Get final positions let final_positions = self .position_tracker .get_all_positions() .into_iter() .map(|p| (p.symbol.clone(), p)) .collect(); // Get completed trades from tracker let completed_trades = self.trade_tracker.get_completed_trades(); // Convert last_prices to simple HashMap for results let simple_last_prices: HashMap = self .last_prices .iter() .map(|(symbol, (_, price))| (symbol.clone(), *price)) .collect(); // Debug: Log OHLC history size before generating results eprintln!("[DEBUG] Generating results with OHLC history:"); for (symbol, bars) in &self.ohlc_history { eprintln!(" {}: {} bars", symbol, bars.len()); } // Use simple results builder with proper trade data BacktestResult::from_engine_data_with_trades( self.config.clone(), state.equity_curve.clone(), state.completed_trades.clone(), completed_trades, final_positions, start_time, &simple_last_prices, &self.ohlc_history, ) } } // Add uuid dependency use uuid::Uuid; #[cfg(test)] mod terminal_valuation_tests { use super::*; use crate::core::{ execution_handlers::{BacktestFillSimulator, SimulatedExecution}, market_data_sources::HistoricalDataSource, time_providers::SimulatedTime, }; use chrono::{Duration, TimeZone}; struct FixedEntries { quantities: HashMap, } impl Strategy for FixedEntries { fn on_market_data(&mut self, data: &MarketUpdate) -> Vec { self.quantities .remove(&data.symbol) .map(|quantity| Signal { symbol: data.symbol.clone(), signal_type: SignalType::Buy, strength: 1.0, quantity: Some(quantity), reason: None, metadata: None, }) .into_iter() .collect() } fn on_fill(&mut self, _: &str, _: f64, _: f64, _: &str) {} fn get_name(&self) -> &str { "fixed-terminal-entries" } fn get_parameters(&self) -> serde_json::Value { serde_json::json!({}) } } fn fixture( commission: f64, entries: &[(&str, f64)], prices: &[(&str, f64, f64)], single_start_time: bool, ) -> BacktestEngine { let start_time = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap(); let end_time = if single_start_time { start_time } else { start_time + Duration::days(2) }; let config = BacktestConfig { name: "Terminal valuation".to_string(), strategy: None, symbols: prices .iter() .map(|(symbol, _, _)| symbol.to_string()) .collect(), start_time, end_time, initial_capital: 10.0, commission, slippage: 0.0, data_frequency: "1d".to_string(), }; let mut updates = Vec::new(); for &(symbol, entry, terminal) in prices { let observations = if single_start_time { vec![(start_time, entry)] } else { vec![ (start_time + Duration::days(1), entry), (end_time, terminal), ] }; for (timestamp, price) in observations { updates.push(MarketUpdate { symbol: symbol.to_string(), timestamp, data: MarketDataType::Bar(crate::Bar { open: price, high: price, low: price, close: price, volume: 1000.0, vwap: Some(price), }), }); } } updates.sort_by_key(|update| update.timestamp); let source = HistoricalDataSource::new(); source.load_data(updates); let mut engine = BacktestEngine::new( config, TradingMode::Backtest { start_time, end_time, speed_multiplier: 1.0, }, Box::new(SimulatedTime::new(start_time)), Box::new(source), Box::new(SimulatedExecution::new(Box::new( BacktestFillSimulator::new(), ))), ); engine.add_strategy(Box::new(FixedEntries { quantities: entries .iter() .map(|&(symbol, quantity)| (symbol.to_string(), quantity)) .collect(), })); engine } fn assert_amount(actual: f64, expected: f64) { assert!( (actual - expected).abs() < 1e-10, "expected {expected}, got {actual}" ); } fn assert_terminal( engine: &BacktestEngine, result: &BacktestResult, cash: f64, equity: f64, fees: f64, fills: usize, ) { let state = engine.state.read(); assert_amount(state.cash, cash); assert_amount(state.portfolio_value, equity); assert_amount(state.equity_curve.last().unwrap().1, equity); assert_amount(result.equity.last().unwrap().value, equity); assert_amount(result.metrics.final_value, equity); assert_amount(result.metrics.total_pnl, equity - 10.0); assert_amount(result.trades.iter().map(|fill| fill.commission).sum(), fees); assert_eq!(state.completed_trades.len(), fills); assert_eq!(result.trades.len(), fills); assert_eq!( state .equity_curve .iter() .filter(|(time, _)| *time == state.current_time) .count(), 1, ); assert!(state .equity_curve .windows(2) .all(|points| points[0].0 < points[1].0)); assert!(result .equity .windows(2) .all(|points| points[0].date < points[1].date)); assert_eq!( result.equity.last().unwrap().date, state.current_time.to_rfc3339() ); } fn assert_flat(engine: &BacktestEngine, result: &BacktestResult) { assert!(engine .position_tracker .get_all_positions() .iter() .all(|p| p.quantity == 0.0)); assert!(result.positions.iter().all(|p| p.quantity == 0.0)); } #[tokio::test] async fn terminal_fee_equity() { let mut engine = fixture(0.01, &[("A", 1.0)], &[("A", 4.0, 4.0)], false); let result = engine.run().await.unwrap(); // 10 - (4 + 0.04) + (4 - 0.04) = 9.92. assert_terminal(&engine, &result, 9.92, 9.92, 0.08, 2); assert_flat(&engine, &result); assert_eq!(result.metrics.total_trades, 1); assert_amount(result.metrics.max_drawdown, 0.008); assert_amount(result.trades[0].quantity, 1.0); assert_amount(result.trades[1].quantity, 1.0); assert_amount(result.trades[0].commission, 0.04); assert_amount(result.trades[1].commission, 0.04); } #[tokio::test] async fn terminal_no_positions() { let mut engine = fixture(0.01, &[], &[("A", 4.0, 4.0)], false); let result = engine.run().await.unwrap(); assert_terminal(&engine, &result, 10.0, 10.0, 0.0, 0); assert_flat(&engine, &result); assert_eq!(result.metrics.total_trades, 0); } #[tokio::test] async fn terminal_zero_fee() { let mut engine = fixture(0.0, &[("A", 1.0)], &[("A", 4.0, 5.0)], false); let result = engine.run().await.unwrap(); assert_terminal(&engine, &result, 11.0, 11.0, 0.0, 2); assert_flat(&engine, &result); } #[tokio::test] async fn terminal_multiple_symbols() { let mut engine = fixture( 0.01, &[("A", 1.0), ("B", 1.0)], &[("A", 4.0, 5.0), ("B", 2.0, 3.0)], false, ); let result = engine.run().await.unwrap(); // Gross gain 2, entry fees 0.06, closing fees 0.08. assert_terminal(&engine, &result, 11.86, 11.86, 0.14, 4); assert_flat(&engine, &result); assert_eq!(result.metrics.total_trades, 2); for (symbol, exit_price) in [("A", 5.0), ("B", 3.0)] { let closing: Vec<_> = result .trades .iter() .filter(|fill| fill.symbol == symbol && fill.side == "sell") .collect(); assert_eq!(closing.len(), 1); assert_amount(closing[0].price, exit_price); } } #[tokio::test] async fn terminal_timestamp_unique() { // At the initial timestamp, the event loop has not written another mark. let mut engine = fixture(0.01, &[("A", 1.0)], &[("A", 4.0, 4.0)], true); let result = engine.run().await.unwrap(); assert_terminal(&engine, &result, 9.92, 9.92, 0.08, 2); assert_eq!(result.equity.len(), 1); assert_flat(&engine, &result); } #[tokio::test] async fn terminal_preserves_marked_micro_position() { let mut engine = fixture(0.01, &[("A", 0.001)], &[("A", 4.0, 8.0)], false); let result = engine.run().await.unwrap(); // Existing liquidation policy retains quantities <= 0.001. assert_terminal(&engine, &result, 9.99596, 10.00396, 0.00004, 1); let position = engine.position_tracker.get_position("A").unwrap(); assert_amount(position.quantity, 0.001); assert_amount( result.metrics.final_value, engine.state.read().cash + 0.001 * 8.0, ); } }