84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
import { BacktestEngine } from '../src/backtest/BacktestEngine';
|
|
import { createExampleRuntime } from './example-runtime';
|
|
|
|
async function testShortTrades() {
|
|
// Create service container
|
|
const {
|
|
container,
|
|
storageService,
|
|
marketDataService: _marketDataService,
|
|
modeManager,
|
|
strategyManager,
|
|
} = await createExampleRuntime();
|
|
|
|
// Initialize backtest mode
|
|
await modeManager.initializeMode({
|
|
mode: 'backtest',
|
|
startDate: '2023-01-01T00:00:00Z',
|
|
endDate: '2024-01-01T00:00:00Z',
|
|
speed: 'max',
|
|
symbols: ['AAPL'],
|
|
initialCapital: 100000,
|
|
dataFrequency: '1d',
|
|
strategy: 'sma-crossover',
|
|
});
|
|
|
|
// Create backtest engine
|
|
const backtestEngine = new BacktestEngine(container, storageService, strategyManager);
|
|
|
|
// Configure backtest
|
|
const config = {
|
|
mode: 'backtest' as const,
|
|
name: 'Short Trade Test',
|
|
strategy: 'sma-crossover',
|
|
symbols: ['AAPL'],
|
|
startDate: '2023-01-01T00:00:00Z',
|
|
endDate: '2024-01-01T00:00:00Z',
|
|
initialCapital: 100000,
|
|
commission: 0.001,
|
|
slippage: 0.0001,
|
|
dataFrequency: '1d' as const,
|
|
speed: 'max' as const,
|
|
};
|
|
|
|
console.log('Testing short trade recording...\n');
|
|
|
|
try {
|
|
const result = await backtestEngine.runBacktest(config);
|
|
|
|
console.log('=== 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)}%`);
|
|
|
|
// Count long vs short trades
|
|
let longTrades = 0;
|
|
let shortTrades = 0;
|
|
|
|
console.log('\n=== TRADE BREAKDOWN ===');
|
|
result.trades.forEach((trade, i) => {
|
|
const tradeType = trade.side === 'buy' ? 'LONG' : 'SHORT';
|
|
if (trade.side === 'buy') {
|
|
longTrades++;
|
|
} else {
|
|
shortTrades++;
|
|
}
|
|
|
|
console.log(`Trade ${i + 1} (${tradeType}):`);
|
|
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)}%)`);
|
|
console.log('');
|
|
});
|
|
|
|
console.log('=== TRADE TYPE SUMMARY ===');
|
|
console.log(`Long trades: ${longTrades}`);
|
|
console.log(`Short trades: ${shortTrades}`);
|
|
console.log(`Total trades: ${result.metrics.totalTrades}`);
|
|
} catch (error) {
|
|
console.error('Test failed:', error);
|
|
}
|
|
}
|
|
|
|
// Run the test
|
|
testShortTrades().catch(console.error);
|