work on engine
This commit is contained in:
parent
a1e5a21847
commit
cbe8f0282c
12 changed files with 694 additions and 16 deletions
|
|
@ -40,6 +40,9 @@ pub struct BacktestEngine {
|
|||
// Maps symbol -> (timestamp, price)
|
||||
last_prices: HashMap<String, (DateTime<Utc>, f64)>,
|
||||
|
||||
// Store latest bar data for each symbol to ensure accurate closing prices
|
||||
latest_bars: HashMap<String, crate::Bar>,
|
||||
|
||||
// Trade tracking
|
||||
trade_tracker: TradeTracker,
|
||||
}
|
||||
|
|
@ -71,6 +74,7 @@ impl BacktestEngine {
|
|||
profitable_trades: 0,
|
||||
total_pnl: 0.0,
|
||||
last_prices: HashMap::new(),
|
||||
latest_bars: HashMap::new(),
|
||||
trade_tracker: TradeTracker::new(),
|
||||
}
|
||||
}
|
||||
|
|
@ -250,6 +254,10 @@ impl BacktestEngine {
|
|||
let old_price = old_entry.map(|(_, p)| *p);
|
||||
eprintln!("📊 PRICE UPDATE: {} @ {} - close: ${:.2} (was: ${:?})",
|
||||
data.symbol, data.timestamp.format("%Y-%m-%d"), bar.close, old_price);
|
||||
|
||||
// Store the complete bar data for accurate position closing
|
||||
self.latest_bars.insert(data.symbol.clone(), bar.clone());
|
||||
|
||||
bar.close
|
||||
}
|
||||
MarketDataType::Quote(quote) => {
|
||||
|
|
@ -543,6 +551,27 @@ impl BacktestEngine {
|
|||
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<String> = 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));
|
||||
// Log only if significant time gap (more than 1 day)
|
||||
let time_gap = time.signed_duration_since(old_time);
|
||||
if time_gap.num_days() > 1 {
|
||||
eprintln!("⏩ Forward-filled {} price ${:.2} from {} to {} (gap: {} days)",
|
||||
symbol, price,
|
||||
old_time.format("%Y-%m-%d"),
|
||||
time.format("%Y-%m-%d"),
|
||||
time_gap.num_days());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_portfolio_value(&mut self) {
|
||||
|
|
@ -649,7 +678,36 @@ impl BacktestEngine {
|
|||
eprintln!(" {}: ${:.2} (from {})", symbol, price, time.format("%Y-%m-%d %H:%M:%S"));
|
||||
}
|
||||
|
||||
// CRITICAL FIX: 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));
|
||||
eprintln!("Updated {} to latest bar close price: ${:.2}", position.symbol, latest_bar.close);
|
||||
} else if let Some((price_time, price)) = self.last_prices.get(&position.symbol).copied() {
|
||||
if price_time < current_time {
|
||||
eprintln!("⚠️ WARNING: Stale price for {} - last update was {} (current time: {})",
|
||||
position.symbol,
|
||||
price_time.format("%Y-%m-%d %H:%M:%S"),
|
||||
current_time.format("%Y-%m-%d %H:%M:%S")
|
||||
);
|
||||
// Update timestamp to current time for proper fill processing
|
||||
self.last_prices.insert(position.symbol.clone(), (current_time, price));
|
||||
}
|
||||
} else {
|
||||
eprintln!("❌ ERROR: No price data available for symbol {}", position.symbol);
|
||||
// TODO: In a production system, we should ensure all symbols have continuous price updates
|
||||
// or implement a mechanism to fetch the latest price on-demand
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for position in positions {
|
||||
if position.quantity.abs() > 0.001 {
|
||||
let last_price = self.last_prices.get(&position.symbol).map(|(_, p)| *p);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue