work on new engine
This commit is contained in:
parent
44476da13f
commit
a1e5a21847
126 changed files with 3425 additions and 6695 deletions
715
apps/stock/engine/src/backtest/engine.rs
Normal file
715
apps/stock/engine/src/backtest/engine.rs
Normal file
|
|
@ -0,0 +1,715 @@
|
|||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use parking_lot::RwLock;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use crate::{
|
||||
TradingMode, MarketDataSource, ExecutionHandler, TimeProvider,
|
||||
MarketUpdate, MarketDataType, Order, Fill, Side,
|
||||
positions::PositionTracker,
|
||||
risk::RiskEngine,
|
||||
orderbook::OrderBookManager,
|
||||
};
|
||||
|
||||
use super::{
|
||||
BacktestConfig, BacktestState, EventQueue, BacktestEvent, EventType,
|
||||
Strategy, Signal, SignalType, BacktestResult, TradeTracker,
|
||||
};
|
||||
|
||||
pub struct BacktestEngine {
|
||||
config: BacktestConfig,
|
||||
state: Arc<RwLock<BacktestState>>,
|
||||
event_queue: Arc<RwLock<EventQueue>>,
|
||||
strategies: Arc<RwLock<Vec<Box<dyn Strategy>>>>,
|
||||
|
||||
// Core components
|
||||
position_tracker: Arc<PositionTracker>,
|
||||
risk_engine: Arc<RiskEngine>,
|
||||
orderbook_manager: Arc<OrderBookManager>,
|
||||
time_provider: Arc<Box<dyn TimeProvider>>,
|
||||
pub market_data_source: Arc<RwLock<Box<dyn MarketDataSource>>>,
|
||||
execution_handler: Arc<RwLock<Box<dyn ExecutionHandler>>>,
|
||||
|
||||
// Metrics
|
||||
total_trades: usize,
|
||||
profitable_trades: usize,
|
||||
total_pnl: f64,
|
||||
|
||||
// Price tracking - single source of truth
|
||||
// Maps symbol -> (timestamp, price)
|
||||
last_prices: HashMap<String, (DateTime<Utc>, f64)>,
|
||||
|
||||
// Trade tracking
|
||||
trade_tracker: TradeTracker,
|
||||
}
|
||||
|
||||
impl BacktestEngine {
|
||||
pub fn new(
|
||||
config: BacktestConfig,
|
||||
mode: TradingMode,
|
||||
time_provider: Box<dyn TimeProvider>,
|
||||
market_data_source: Box<dyn MarketDataSource>,
|
||||
execution_handler: Box<dyn ExecutionHandler>,
|
||||
) -> 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(),
|
||||
trade_tracker: TradeTracker::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_strategy(&mut self, strategy: Box<dyn Strategy>) {
|
||||
self.strategies.write().push(strategy);
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> Result<BacktestResult, String> {
|
||||
eprintln!("=== BacktestEngine::run() START ===");
|
||||
eprintln!("Config: start={}, end={}, symbols={:?}",
|
||||
self.config.start_time, self.config.end_time, self.config.symbols);
|
||||
eprintln!("Number of strategies loaded: {}", self.strategies.read().len());
|
||||
|
||||
// Initialize start time
|
||||
if let Some(simulated_time) = self.time_provider.as_any()
|
||||
.downcast_ref::<crate::core::time_providers::SimulatedTime>()
|
||||
{
|
||||
simulated_time.advance_to(self.config.start_time);
|
||||
eprintln!("Time initialized to: {}", self.config.start_time);
|
||||
}
|
||||
|
||||
// Load market data
|
||||
eprintln!("Loading market data from data source...");
|
||||
self.load_market_data().await?;
|
||||
|
||||
let queue_len = self.event_queue.read().len();
|
||||
eprintln!("Event queue length after loading: {}", queue_len);
|
||||
|
||||
if queue_len == 0 {
|
||||
eprintln!("WARNING: No events loaded! Check data source.");
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
if iteration <= 5 || iteration % 100 == 0 {
|
||||
eprintln!("Processing iteration {} at time {} with {} events",
|
||||
iteration, current_time, events.len());
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("Backtest complete. Total trades: {}", self.total_trades);
|
||||
|
||||
// Close all open positions at market prices
|
||||
self.close_all_positions().await?;
|
||||
|
||||
// Generate results
|
||||
Ok(self.generate_results())
|
||||
}
|
||||
|
||||
async fn load_market_data(&mut self) -> Result<(), String> {
|
||||
eprintln!("=== load_market_data START ===");
|
||||
let mut data_source = self.market_data_source.write();
|
||||
|
||||
// Check if it's a HistoricalDataSource
|
||||
if let Some(historical) = data_source.as_any()
|
||||
.downcast_ref::<crate::core::market_data_sources::HistoricalDataSource>() {
|
||||
eprintln!("Data source is HistoricalDataSource");
|
||||
eprintln!("Historical data points available: {}", historical.data_len());
|
||||
} else {
|
||||
eprintln!("WARNING: Data source is NOT HistoricalDataSource!");
|
||||
}
|
||||
|
||||
eprintln!("Seeking to start time: {}", self.config.start_time);
|
||||
data_source.seek_to_time(self.config.start_time)?;
|
||||
|
||||
let mut count = 0;
|
||||
let mut first_few = 0;
|
||||
|
||||
// Load all data into event queue
|
||||
while let Some(update) = data_source.get_next_update().await {
|
||||
if update.timestamp > self.config.end_time {
|
||||
eprintln!("Reached end time at {} data points", count);
|
||||
break;
|
||||
}
|
||||
|
||||
count += 1;
|
||||
|
||||
// Log first few data points
|
||||
if first_few < 3 {
|
||||
eprintln!("Data point {}: symbol={}, time={}, type={:?}",
|
||||
count, update.symbol, update.timestamp,
|
||||
match &update.data {
|
||||
MarketDataType::Bar(b) => format!("Bar(close={})", b.close),
|
||||
MarketDataType::Quote(q) => format!("Quote(bid={}, ask={})", q.bid, q.ask),
|
||||
MarketDataType::Trade(t) => format!("Trade(price={})", t.price),
|
||||
}
|
||||
);
|
||||
first_few += 1;
|
||||
}
|
||||
|
||||
if count % 100 == 0 {
|
||||
eprintln!("Loaded {} data points so far...", count);
|
||||
}
|
||||
|
||||
let event = BacktestEvent::market_data(update.timestamp, update);
|
||||
self.event_queue.write().push(event);
|
||||
}
|
||||
|
||||
eprintln!("=== load_market_data COMPLETE ===");
|
||||
eprintln!("Total data points loaded: {}", count);
|
||||
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> {
|
||||
static mut MARKET_DATA_COUNT: usize = 0;
|
||||
unsafe {
|
||||
MARKET_DATA_COUNT += 1;
|
||||
if MARKET_DATA_COUNT <= 3 || MARKET_DATA_COUNT % 100 == 0 {
|
||||
eprintln!("process_market_data #{}: symbol={}, time={}",
|
||||
MARKET_DATA_COUNT, data.symbol, data.timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
// Update price tracking - single source of truth
|
||||
let price = match &data.data {
|
||||
MarketDataType::Bar(bar) => {
|
||||
let old_entry = self.last_prices.get(&data.symbol);
|
||||
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);
|
||||
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));
|
||||
|
||||
// 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 (i, strategy) in strategies.iter_mut().enumerate() {
|
||||
let signals = strategy.on_market_data(&market_data);
|
||||
if !signals.is_empty() {
|
||||
eprintln!("Strategy {} generated {} signals!", i, signals.len());
|
||||
}
|
||||
all_signals.extend(signals);
|
||||
}
|
||||
}
|
||||
|
||||
// Process signals
|
||||
for signal in all_signals {
|
||||
eprintln!("Processing signal: {:?}", signal);
|
||||
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> {
|
||||
let current_time = self.time_provider.now();
|
||||
eprintln!("📡 SIGNAL at {}: {:?} {} (strength: {}, reason: {:?})",
|
||||
current_time.format("%Y-%m-%d"),
|
||||
signal.signal_type,
|
||||
signal.symbol,
|
||||
signal.strength,
|
||||
signal.reason);
|
||||
|
||||
// Only process strong signals
|
||||
if signal.strength.abs() < 0.7 {
|
||||
eprintln!(" Signal ignored (strength < 0.7)");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Check current price before creating order
|
||||
if let Some((price_time, price)) = self.last_prices.get(&signal.symbol) {
|
||||
eprintln!(" Current price for {}: ${:.2} (from {})",
|
||||
signal.symbol, price, price_time.format("%Y-%m-%d"));
|
||||
}
|
||||
|
||||
// Convert signal to order
|
||||
let order = self.signal_to_order(signal)?;
|
||||
|
||||
eprintln!(" Creating {:?} order for {} shares", order.side, order.quantity);
|
||||
|
||||
// Submit order
|
||||
self.process_order_submission(order).await
|
||||
}
|
||||
|
||||
fn signal_to_order(&self, signal: Signal) -> Result<Order, String> {
|
||||
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 risk_check = self.risk_engine.check_order(&order, current_position);
|
||||
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<Order> = {
|
||||
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 - only use if it's from the current time
|
||||
let (price_time, base_price) = self.last_prices.get(&order.symbol)
|
||||
.copied()
|
||||
.ok_or_else(|| format!("No price available for symbol: {}", order.symbol))?;
|
||||
|
||||
// CRITICAL: Verify the price is from the current time
|
||||
if price_time != current_time {
|
||||
eprintln!("⚠️ WARNING: Price timestamp mismatch! Current: {}, Price from: {}",
|
||||
current_time.format("%Y-%m-%d %H:%M:%S"),
|
||||
price_time.format("%Y-%m-%d %H:%M:%S"));
|
||||
// In a real system, we would reject this fill or fetch current price
|
||||
// For now, log the issue
|
||||
}
|
||||
|
||||
eprintln!("🔍 CHECK_ORDER_FILL: {:?} {} @ time {} - price: ${:.2} (from {})",
|
||||
order.side, order.symbol, current_time.format("%Y-%m-%d"),
|
||||
base_price, price_time.format("%Y-%m-%d"));
|
||||
|
||||
// DEBUG: Check what's in last_prices for this symbol
|
||||
eprintln!(" DEBUG: All prices for {}: {:?}",
|
||||
order.symbol,
|
||||
self.last_prices.get(&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),
|
||||
};
|
||||
|
||||
eprintln!(" Fill price after slippage ({}): ${:.2}", self.config.slippage, fill_price);
|
||||
|
||||
// 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);
|
||||
|
||||
// Update positions
|
||||
let update = self.position_tracker.process_fill(
|
||||
&order.symbol,
|
||||
&fill,
|
||||
order.side,
|
||||
);
|
||||
|
||||
// Calculate P&L if position was reduced/closed
|
||||
let pnl = if update.resulting_position.realized_pnl != 0.0 {
|
||||
Some(update.resulting_position.realized_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);
|
||||
}
|
||||
}
|
||||
|
||||
let fill_date = fill.timestamp.format("%Y-%m-%d").to_string();
|
||||
let is_feb_mar_2024 = fill_date >= "2024-02-28".to_string() && fill_date <= "2024-03-05".to_string();
|
||||
|
||||
if is_feb_mar_2024 {
|
||||
eprintln!("
|
||||
🔴 CRITICAL FILL on {}: {} {} @ {} (side: {:?})",
|
||||
fill_date, fill.quantity, order.symbol, fill.price, order.side);
|
||||
eprintln!("Cash before: ${:.2}, Cash after: ${:.2}, Cash change: ${:.2}",
|
||||
self.state.read().cash - cash_change, self.state.read().cash, cash_change);
|
||||
}
|
||||
|
||||
eprintln!("Fill processed: {} {} @ {} (side: {:?})",
|
||||
fill.quantity, order.symbol, fill.price, order.side);
|
||||
eprintln!("Current position after fill: {}",
|
||||
self.position_tracker.get_position(&order.symbol)
|
||||
.map(|p| p.quantity)
|
||||
.unwrap_or(0.0));
|
||||
|
||||
// Update metrics
|
||||
self.total_trades += 1;
|
||||
if update.resulting_position.realized_pnl > 0.0 {
|
||||
self.profitable_trades += 1;
|
||||
}
|
||||
self.total_pnl = update.resulting_position.realized_pnl;
|
||||
|
||||
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<Utc>) {
|
||||
if let Some(simulated_time) = self.time_provider.as_any()
|
||||
.downcast_ref::<crate::core::time_providers::SimulatedTime>()
|
||||
{
|
||||
simulated_time.advance_to(time);
|
||||
}
|
||||
self.state.write().current_time = time;
|
||||
}
|
||||
|
||||
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;
|
||||
let current_time = self.time_provider.now();
|
||||
|
||||
// Debug logging for first few updates
|
||||
static mut UPDATE_COUNT: usize = 0;
|
||||
unsafe {
|
||||
UPDATE_COUNT += 1;
|
||||
if UPDATE_COUNT <= 5 || UPDATE_COUNT % 100 == 0 ||
|
||||
// Log around Feb 28 - Mar 5, 2024
|
||||
(current_time.format("%Y-%m-%d").to_string() >= "2024-02-28".to_string() &&
|
||||
current_time.format("%Y-%m-%d").to_string() <= "2024-03-05".to_string()) {
|
||||
eprintln!("=== Portfolio Update #{} at {} ===", UPDATE_COUNT, current_time);
|
||||
eprintln!("Cash: ${:.2}", 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;
|
||||
|
||||
unsafe {
|
||||
if UPDATE_COUNT <= 5 || UPDATE_COUNT % 100 == 0 ||
|
||||
// Log around Feb 28 - Mar 5, 2024
|
||||
(current_time.format("%Y-%m-%d").to_string() >= "2024-02-28".to_string() &&
|
||||
current_time.format("%Y-%m-%d").to_string() <= "2024-03-05".to_string()) {
|
||||
let pnl = if position.quantity > 0.0 {
|
||||
(price - position.average_price) * position.quantity
|
||||
} else {
|
||||
(position.average_price - price) * position.quantity.abs()
|
||||
};
|
||||
let position_type = if position.quantity > 0.0 { "LONG" } else { "SHORT" };
|
||||
eprintln!(" {} {} position: {} shares @ avg ${:.2}, current ${:.2} = ${:.2} (P&L: ${:.2})",
|
||||
position_type, position.symbol, position.quantity, position.average_price, price, market_value, pnl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
if UPDATE_COUNT <= 5 || UPDATE_COUNT % 100 == 0 ||
|
||||
// Log around Feb 28 - Mar 5, 2024
|
||||
(current_time.format("%Y-%m-%d").to_string() >= "2024-02-28".to_string() &&
|
||||
current_time.format("%Y-%m-%d").to_string() <= "2024-03-05".to_string()) {
|
||||
eprintln!("Total Portfolio Value: ${:.2}", portfolio_value);
|
||||
eprintln!("===================================");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
let shares = (position_value / price).floor();
|
||||
|
||||
eprintln!("Position sizing for {}: portfolio=${:.2}, cash=${:.2}, price=${:.2}, shares={}",
|
||||
symbol, portfolio_value, cash, price, shares);
|
||||
|
||||
shares
|
||||
}
|
||||
|
||||
fn get_next_event_time(&self) -> Option<DateTime<Utc>> {
|
||||
// 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> {
|
||||
eprintln!("=== Closing all open positions at end of backtest ===");
|
||||
eprintln!("Current time: {}", self.time_provider.now());
|
||||
eprintln!("Last prices:");
|
||||
for (symbol, (time, price)) in &self.last_prices {
|
||||
eprintln!(" {}: ${:.2} (from {})", symbol, price, time.format("%Y-%m-%d %H:%M:%S"));
|
||||
}
|
||||
|
||||
let positions = self.position_tracker.get_all_positions();
|
||||
for position in positions {
|
||||
if position.quantity.abs() > 0.001 {
|
||||
let last_price = self.last_prices.get(&position.symbol).map(|(_, p)| *p);
|
||||
eprintln!("Closing position: {} {} shares of {} at last price: {:?}",
|
||||
if position.quantity > 0.0 { "Selling" } else { "Buying" },
|
||||
position.quantity.abs(),
|
||||
position.symbol,
|
||||
last_price
|
||||
);
|
||||
|
||||
// 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?;
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("All positions closed. Final cash: {}", self.state.read().cash);
|
||||
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<String, f64> = self.last_prices
|
||||
.iter()
|
||||
.map(|(symbol, (_, price))| (symbol.clone(), *price))
|
||||
.collect();
|
||||
|
||||
// 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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Add uuid dependency
|
||||
use uuid::Uuid;
|
||||
Loading…
Add table
Add a link
Reference in a new issue