small fixes for backtest
This commit is contained in:
parent
6df32dc18b
commit
6cf3179092
16 changed files with 2180 additions and 16 deletions
84
apps/stock/orchestrator/examples/test-backtest-trades.ts
Normal file
84
apps/stock/orchestrator/examples/test-backtest-trades.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { BacktestEngine } from '../src/backtest/BacktestEngine';
|
||||
import { StrategyManager } from '../src/strategies/StrategyManager';
|
||||
import { StorageService } from '../src/services/StorageService';
|
||||
import { ModeManager } from '../src/core/ModeManager';
|
||||
import { MarketDataService } from '../src/services/MarketDataService';
|
||||
import { ExecutionService } from '../src/services/ExecutionService';
|
||||
import { IServiceContainer } from '@stock-bot/di';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
|
||||
const logger = getLogger('BacktestTest');
|
||||
|
||||
async function runBacktestWithDetailedLogging() {
|
||||
// Create service container
|
||||
const container: IServiceContainer = {
|
||||
logger,
|
||||
custom: {}
|
||||
};
|
||||
|
||||
// Initialize services
|
||||
const storageService = new StorageService();
|
||||
const marketDataService = new MarketDataService(container);
|
||||
const executionService = new ExecutionService(container);
|
||||
const modeManager = new ModeManager(container);
|
||||
const strategyManager = new StrategyManager(container);
|
||||
|
||||
// Set services in container
|
||||
container.custom = {
|
||||
MarketDataService: marketDataService,
|
||||
ExecutionService: executionService,
|
||||
ModeManager: modeManager,
|
||||
StorageService: storageService
|
||||
};
|
||||
|
||||
// Set backtest mode
|
||||
await modeManager.setMode('backtest', {
|
||||
startTime: new Date('2020-01-01').getTime(),
|
||||
endTime: new Date('2025-01-01').getTime(),
|
||||
speedMultiplier: 1
|
||||
});
|
||||
|
||||
// Create backtest engine
|
||||
const backtestEngine = new BacktestEngine(container, storageService, strategyManager);
|
||||
|
||||
// Configure backtest
|
||||
const config = {
|
||||
name: 'SMA Crossover Test',
|
||||
strategy: 'sma-crossover',
|
||||
symbols: ['AAPL', 'GOOGL', 'MSFT'],
|
||||
startDate: '2020-01-01',
|
||||
endDate: '2025-01-01',
|
||||
initialCapital: 100000,
|
||||
commission: 0.001,
|
||||
slippage: 0.0001,
|
||||
dataFrequency: '1d'
|
||||
};
|
||||
|
||||
logger.info('Starting backtest with configuration:', config);
|
||||
|
||||
try {
|
||||
const result = await backtestEngine.runBacktest(config);
|
||||
|
||||
logger.info('=== BACKTEST RESULTS ===');
|
||||
logger.info(`Total Trades: ${result.metrics.totalTrades}`);
|
||||
logger.info(`Win Rate: ${result.metrics.winRate.toFixed(2)}%`);
|
||||
logger.info(`Total Return: ${result.metrics.totalReturn.toFixed(2)}%`);
|
||||
logger.info(`Sharpe Ratio: ${result.metrics.sharpeRatio.toFixed(2)}`);
|
||||
logger.info(`Max Drawdown: ${result.metrics.maxDrawdown.toFixed(2)}%`);
|
||||
|
||||
logger.info('\n=== TRADE HISTORY ===');
|
||||
result.trades.forEach((trade, i) => {
|
||||
logger.info(`Trade ${i + 1}:`);
|
||||
logger.info(` Symbol: ${trade.symbol}`);
|
||||
logger.info(` Entry: ${trade.entryDate} @ $${trade.entryPrice.toFixed(2)}`);
|
||||
logger.info(` Exit: ${trade.exitDate} @ $${trade.exitPrice.toFixed(2)}`);
|
||||
logger.info(` P&L: $${trade.pnl.toFixed(2)} (${trade.pnlPercent.toFixed(2)}%)`);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
logger.error('Backtest failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Run the test
|
||||
runBacktestWithDetailedLogging().catch(console.error);
|
||||
Loading…
Add table
Add a link
Reference in a new issue