finished initial backtest / engine
This commit is contained in:
parent
55b4ca78c9
commit
c106a719e8
18 changed files with 1571 additions and 180 deletions
54
apps/stock/orchestrator/test-quick-backtest.ts
Normal file
54
apps/stock/orchestrator/test-quick-backtest.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
/**
|
||||
* Quick test of backtest with fixed order execution
|
||||
*/
|
||||
|
||||
import { createContainer } from './src/simple-container';
|
||||
import { BacktestEngine } from './src/backtest/BacktestEngine';
|
||||
|
||||
async function runQuickBacktest() {
|
||||
const container = await createContainer();
|
||||
|
||||
const backtestEngine = container.resolve('backtestEngine') as BacktestEngine;
|
||||
|
||||
const config = {
|
||||
name: 'Quick SMA Test',
|
||||
strategy: 'sma-crossover',
|
||||
symbols: ['AAPL'],
|
||||
startDate: '2020-01-01',
|
||||
endDate: '2021-01-01', // Just 1 year for quick test
|
||||
initialCapital: 100000,
|
||||
dataFrequency: '1d',
|
||||
commission: 0.001,
|
||||
slippage: 0.0001
|
||||
};
|
||||
|
||||
console.log('Running quick backtest...\n');
|
||||
|
||||
try {
|
||||
const result = await backtestEngine.runBacktest(config);
|
||||
|
||||
console.log('Backtest Results:');
|
||||
console.log(`Total Return: ${result.metrics.totalReturn.toFixed(2)}%`);
|
||||
console.log(`Total Trades: ${result.metrics.totalTrades}`);
|
||||
console.log(`Win Rate: ${result.metrics.winRate.toFixed(2)}%`);
|
||||
console.log(`Sharpe Ratio: ${result.metrics.sharpeRatio.toFixed(2)}`);
|
||||
console.log(`Max Drawdown: ${result.metrics.maxDrawdown.toFixed(2)}%`);
|
||||
console.log('\nTrade History:');
|
||||
console.log(`Trades in history: ${result.trades.length}`);
|
||||
|
||||
result.trades.slice(0, 5).forEach(trade => {
|
||||
console.log(`- ${trade.side} ${trade.quantity} @ $${trade.entryPrice} -> $${trade.exitPrice} (${trade.pnlPercent.toFixed(2)}%)`);
|
||||
});
|
||||
|
||||
if (result.trades.length > 5) {
|
||||
console.log(`... and ${result.trades.length - 5} more trades`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Backtest failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
runQuickBacktest().catch(console.error);
|
||||
Loading…
Add table
Add a link
Reference in a new issue