finished initial backtest / engine
This commit is contained in:
parent
55b4ca78c9
commit
c106a719e8
18 changed files with 1571 additions and 180 deletions
70
apps/stock/orchestrator/test-backtest-simple.ts
Normal file
70
apps/stock/orchestrator/test-backtest-simple.ts
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
/**
|
||||
* Simple backtest test without full container
|
||||
*/
|
||||
|
||||
import { BacktestEngine } from './src/backtest/BacktestEngine';
|
||||
import { StrategyManager } from './src/strategies/StrategyManager';
|
||||
import { StorageService } from './src/services/StorageService';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
|
||||
async function runSimpleBacktest() {
|
||||
console.log('Running simple backtest test...\n');
|
||||
|
||||
// Create minimal container
|
||||
const logger = getLogger('test');
|
||||
const container = {
|
||||
logger,
|
||||
custom: {}
|
||||
};
|
||||
|
||||
// Create services
|
||||
const storageService = new StorageService(container as any);
|
||||
const strategyManager = new StrategyManager(container as any);
|
||||
|
||||
// Initialize strategy
|
||||
await strategyManager.initializeStrategies([{
|
||||
id: 'test-sma',
|
||||
name: 'sma-crossover',
|
||||
enabled: true,
|
||||
symbols: ['AAPL'],
|
||||
allocation: 1.0
|
||||
}]);
|
||||
|
||||
// Create backtest engine
|
||||
const backtestEngine = new BacktestEngine(container as any, storageService, strategyManager);
|
||||
|
||||
const config = {
|
||||
mode: 'backtest',
|
||||
name: 'Simple SMA Test',
|
||||
strategy: 'sma-crossover',
|
||||
symbols: ['AAPL'],
|
||||
startDate: '2023-01-01T00:00:00Z',
|
||||
endDate: '2023-03-01T00:00:00Z', // Just 2 months
|
||||
initialCapital: 100000,
|
||||
dataFrequency: '1d',
|
||||
commission: 0.001,
|
||||
slippage: 0.0001
|
||||
};
|
||||
|
||||
try {
|
||||
const result = await backtestEngine.runBacktest(config);
|
||||
|
||||
console.log('\nBacktest Results:');
|
||||
console.log(`Total Return: ${result.metrics.totalReturn.toFixed(2)}%`);
|
||||
console.log(`Total Trades: ${result.metrics.totalTrades}`);
|
||||
console.log(`Trades in history: ${result.trades.length}`);
|
||||
console.log(`Win Rate: ${result.metrics.winRate.toFixed(2)}%`);
|
||||
|
||||
console.log('\nTrade Details:');
|
||||
result.trades.forEach((trade, i) => {
|
||||
console.log(`Trade ${i + 1}: ${trade.side} ${trade.quantity} @ $${trade.entryPrice.toFixed(2)} -> $${trade.exitPrice.toFixed(2)} (P&L: $${trade.pnl.toFixed(2)})`);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Backtest failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
runSimpleBacktest().catch(console.error);
|
||||
Loading…
Add table
Add a link
Reference in a new issue