stock-bot/apps/stock/orchestrator/examples/show-order-flow.ts

75 lines
2.4 KiB
TypeScript

import { BacktestEngine } from '../src/backtest/BacktestEngine';
import { createExampleRuntime } from './example-runtime';
async function showOrderFlow() {
// Create service container
const {
container,
storageService,
marketDataService: _marketDataService,
modeManager,
strategyManager,
} = await createExampleRuntime();
// Initialize backtest mode
await modeManager.initializeMode({
mode: 'backtest',
startDate: '2023-07-01T00:00:00Z',
endDate: '2023-08-01T00:00:00Z', // Just 1 month
speed: 'max',
symbols: ['AAPL.US'],
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: 'Order Flow Demo',
strategy: 'sma-crossover',
symbols: ['AAPL.US'],
startDate: '2023-07-01T00:00:00Z',
endDate: '2023-08-01T00:00:00Z',
initialCapital: 100000,
commission: 0.001,
slippage: 0.0001,
dataFrequency: '1d' as const,
speed: 'max' as const,
};
console.log('\n=== ORDER FLOW DEMONSTRATION ===\n');
console.log('This will show the actual BUY and SELL orders being generated and executed:\n');
try {
const result = await backtestEngine.runBacktest(config);
console.log('\n=== COMPLETED TRADES (Round-trips) ===');
console.log('Note: Each trade below represents a BUY order (entry) + SELL order (exit):\n');
result.trades.forEach((trade, i) => {
console.log(`Trade ${i + 1}:`);
console.log(
` Opened with: BUY order on ${trade.entryDate} @ $${trade.entryPrice.toFixed(2)}`
);
console.log(
` Closed with: SELL order on ${trade.exitDate} @ $${trade.exitPrice.toFixed(2)}`
);
console.log(` Trade type: ${trade.side === 'buy' ? 'LONG' : 'SHORT'} position`);
console.log(` P&L: $${trade.pnl.toFixed(2)} (${trade.pnlPercent.toFixed(2)}%)\n`);
});
console.log('Summary:');
console.log(`- Total completed trades: ${result.metrics.totalTrades}`);
console.log(`- Each trade = 1 BUY order + 1 SELL order`);
console.log(`- Total orders executed: ${result.metrics.totalTrades * 2}`);
} catch (error) {
console.error('Failed:', error);
}
}
// Run the demo
showOrderFlow().catch(console.error);