backtest work
This commit is contained in:
parent
143e2e1678
commit
55b4ca78c9
6 changed files with 427 additions and 129 deletions
99
apps/stock/orchestrator/test-backtest.ts
Normal file
99
apps/stock/orchestrator/test-backtest.ts
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
import { createContainer } from './src/simple-container';
|
||||
import { BacktestEngine } from './src/backtest/BacktestEngine';
|
||||
import { StrategyManager } from './src/strategies/StrategyManager';
|
||||
import { SimpleMovingAverageCrossover } from './src/strategies/examples/SimpleMovingAverageCrossover';
|
||||
|
||||
async function runBacktest() {
|
||||
console.log('Starting backtest test...');
|
||||
|
||||
// Create container with minimal config
|
||||
const config = {
|
||||
port: 2004,
|
||||
mode: 'paper',
|
||||
enableWebSocket: false,
|
||||
database: {
|
||||
mongodb: { enabled: false },
|
||||
postgres: { enabled: false },
|
||||
questdb: { enabled: false }
|
||||
},
|
||||
redis: {
|
||||
url: 'redis://localhost:6379'
|
||||
},
|
||||
backtesting: {
|
||||
maxConcurrent: 1,
|
||||
defaultSpeed: 'max',
|
||||
dataResolutions: ['1d']
|
||||
},
|
||||
strategies: {
|
||||
maxActive: 10,
|
||||
defaultTimeout: 30000
|
||||
}
|
||||
};
|
||||
const container = await createContainer(config);
|
||||
|
||||
// Initialize strategy manager
|
||||
const strategyManager = new StrategyManager(container.executionService, container.modeManager);
|
||||
|
||||
// Create and add strategy
|
||||
const strategyConfig = {
|
||||
id: 'sma-test',
|
||||
name: 'SMA Test',
|
||||
type: 'sma-crossover',
|
||||
symbols: ['AA'],
|
||||
active: true,
|
||||
allocation: 1.0,
|
||||
riskLimit: 0.02,
|
||||
maxPositions: 10
|
||||
};
|
||||
|
||||
const strategy = new SimpleMovingAverageCrossover(strategyConfig, container.modeManager, container.executionService);
|
||||
await strategyManager.addStrategy(strategy);
|
||||
|
||||
// Create backtest engine
|
||||
const backtestEngine = new BacktestEngine(container, strategyManager);
|
||||
|
||||
// Run backtest
|
||||
const backtestConfig = {
|
||||
symbols: ['AA'],
|
||||
strategy: 'sma-crossover',
|
||||
startDate: '2024-01-01',
|
||||
endDate: '2024-12-31',
|
||||
initialCapital: 100000,
|
||||
commission: 0.001,
|
||||
slippage: 0.0005,
|
||||
dataFrequency: '1d'
|
||||
};
|
||||
|
||||
try {
|
||||
console.log('Running backtest...');
|
||||
const result = await backtestEngine.runBacktest(backtestConfig);
|
||||
|
||||
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 ===');
|
||||
result.trades.forEach((trade, i) => {
|
||||
console.log(`Trade ${i + 1}: ${trade.side} ${trade.quantity} ${trade.symbol} @ ${trade.entryPrice.toFixed(2)}`);
|
||||
if (trade.exitDate) {
|
||||
console.log(` Exit: ${trade.exitPrice.toFixed(2)}, P&L: ${trade.pnl.toFixed(2)} (${trade.pnlPercent.toFixed(2)}%)`);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`\nTotal trades in result: ${result.trades.length}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Backtest failed:', error);
|
||||
} finally {
|
||||
// Cleanup
|
||||
await container.shutdownManager.shutdown();
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
runBacktest().catch(console.error);
|
||||
Loading…
Add table
Add a link
Reference in a new issue