fixed backtest i think
This commit is contained in:
parent
16ac28a565
commit
083dca500c
7 changed files with 663 additions and 56 deletions
|
|
@ -28,13 +28,16 @@ pub struct BacktestEngine {
|
|||
risk_engine: Arc<RiskEngine>,
|
||||
orderbook_manager: Arc<OrderBookManager>,
|
||||
time_provider: Arc<Box<dyn TimeProvider>>,
|
||||
market_data_source: Arc<RwLock<Box<dyn MarketDataSource>>>,
|
||||
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
|
||||
last_prices: HashMap<String, f64>,
|
||||
}
|
||||
|
||||
impl BacktestEngine {
|
||||
|
|
@ -63,6 +66,7 @@ impl BacktestEngine {
|
|||
total_trades: 0,
|
||||
profitable_trades: 0,
|
||||
total_pnl: 0.0,
|
||||
last_prices: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -159,17 +163,19 @@ impl BacktestEngine {
|
|||
}
|
||||
|
||||
async fn process_market_data(&mut self, data: MarketUpdate) -> Result<(), String> {
|
||||
// Update orderbook if it's quote data
|
||||
// Update price tracking
|
||||
match &data.data {
|
||||
MarketDataType::Bar(bar) => {
|
||||
self.last_prices.insert(data.symbol.clone(), bar.close);
|
||||
}
|
||||
MarketDataType::Quote(quote) => {
|
||||
// For now, skip orderbook updates
|
||||
// self.orderbook_manager.update_quote(&data.symbol, quote.bid, quote.ask);
|
||||
// Use mid price for quotes
|
||||
let mid_price = (quote.bid + quote.ask) / 2.0;
|
||||
self.last_prices.insert(data.symbol.clone(), mid_price);
|
||||
}
|
||||
MarketDataType::Trade(trade) => {
|
||||
// For now, skip orderbook updates
|
||||
// self.orderbook_manager.update_last_trade(&data.symbol, trade.price, trade.size);
|
||||
self.last_prices.insert(data.symbol.clone(), trade.price);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Convert to simpler MarketData for strategies
|
||||
|
|
@ -285,9 +291,9 @@ impl BacktestEngine {
|
|||
|
||||
async fn check_order_fill(&mut self, order: &Order) -> Result<(), String> {
|
||||
// Get current market price
|
||||
// For now, use a simple fill model with last known price
|
||||
// In a real backtest, this would use orderbook data
|
||||
let base_price = 100.0; // TODO: Get from market data
|
||||
let 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 {
|
||||
|
|
@ -366,8 +372,9 @@ impl BacktestEngine {
|
|||
let mut portfolio_value = self.state.read().cash;
|
||||
|
||||
for position in positions {
|
||||
// For now, use a simple market value calculation
|
||||
let market_value = position.quantity * 100.0; // TODO: Get actual price
|
||||
// Use last known price for the symbol
|
||||
let price = self.last_prices.get(&position.symbol).copied().unwrap_or(position.average_price);
|
||||
let market_value = position.quantity * price;
|
||||
portfolio_value += market_value;
|
||||
}
|
||||
|
||||
|
|
@ -378,7 +385,7 @@ impl BacktestEngine {
|
|||
let portfolio_value = self.state.read().portfolio_value;
|
||||
let allocation = 0.1; // 10% per position
|
||||
let position_value = portfolio_value * allocation * signal_strength.abs();
|
||||
let price = 100.0; // TODO: Get actual price from market data
|
||||
let price = self.last_prices.get(symbol).copied().unwrap_or(100.0);
|
||||
|
||||
(position_value / price).floor()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue