import { getLogger } from '@stock-bot/logger'; import { BacktestEngine } from '../src/backtest/BacktestEngine'; import { createExampleRuntime } from './example-runtime'; const _logger = getLogger('BacktestTest'); async function runSimpleBacktest() { // Create service container const { container, storageService, marketDataService: _marketDataService, modeManager, strategyManager, } = await createExampleRuntime(); // Initialize backtest mode with full config await modeManager.initializeMode({ mode: 'backtest', startDate: '2023-01-01T00:00:00Z', endDate: '2024-01-01T00:00:00Z', speed: 'max', symbols: ['AAPL.US'], initialCapital: 100000, dataFrequency: '1d', strategy: 'sma-crossover', }); // Create backtest engine const backtestEngine = new BacktestEngine(container, storageService, strategyManager); // Configure backtest - shorter period for faster testing const config = { mode: 'backtest' as const, // Add mode field name: 'SMA Crossover Test', strategy: 'sma-crossover', symbols: ['AAPL.US'], // Just one symbol for simplicity startDate: '2023-01-01T00:00:00Z', // ISO datetime format endDate: '2024-01-01T00:00:00Z', // ISO datetime format initialCapital: 100000, commission: 0.001, slippage: 0.0001, dataFrequency: '1d' as const, speed: 'max' as const, }; console.log('Starting backtest with configuration:', config); try { const result = await backtestEngine.runBacktest(config); console.log('\n=== BACKTEST RESULTS ==='); console.log(`Total Trades: ${result.metrics.totalTrades}`); console.log(`Win Rate: ${result.metrics.winRate.toFixed(2)}%`); console.log(`Total Return: ${result.metrics.totalReturn.toFixed(2)}%`); console.log(`Sharpe Ratio: ${result.metrics.sharpeRatio.toFixed(2)}`); console.log(`Max Drawdown: ${result.metrics.maxDrawdown.toFixed(2)}%`); console.log('\n=== TRADE HISTORY ==='); if (result.trades.length === 0) { console.log('No trades were executed!'); } else { result.trades.forEach((trade, i) => { console.log(`\nTrade ${i + 1}:`); console.log(` Symbol: ${trade.symbol}`); console.log(` Entry: ${trade.entryDate} @ $${trade.entryPrice.toFixed(2)}`); console.log(` Exit: ${trade.exitDate} @ $${trade.exitPrice.toFixed(2)}`); console.log(` P&L: $${trade.pnl.toFixed(2)} (${trade.pnlPercent.toFixed(2)}%)`); }); } // Show some equity curve data console.log('\n=== EQUITY CURVE (first and last 5 points) ==='); const equityCurve = result.equity; if (equityCurve.length > 0) { equityCurve.slice(0, 5).forEach(point => { console.log(`${point.date}: $${point.value.toFixed(2)}`); }); if (equityCurve.length > 10) { console.log('...'); equityCurve.slice(-5).forEach(point => { console.log(`${point.date}: $${point.value.toFixed(2)}`); }); } } } catch (error) { console.error('Backtest failed:', error); } } // Run the test runSimpleBacktest().catch(console.error);