99 lines
No EOL
3.7 KiB
TypeScript
99 lines
No EOL
3.7 KiB
TypeScript
import { BacktestEngine } from '../src/backtest/BacktestEngine';
|
|
import { StrategyManager } from '../src/strategies/StrategyManager';
|
|
import { StorageService } from '../src/services/StorageService';
|
|
import { ModeManager } from '../src/core/ModeManager';
|
|
import { MarketDataService } from '../src/services/MarketDataService';
|
|
import { ExecutionService } from '../src/services/ExecutionService';
|
|
import { IServiceContainer } from '@stock-bot/di';
|
|
|
|
async function showOrderFlow() {
|
|
// Create service container
|
|
const container: IServiceContainer = {
|
|
logger: {
|
|
info: (msg: string, ...args: any[]) => {
|
|
// Only log order-related messages
|
|
if (msg.includes('order') || msg.includes('Order') || msg.includes('Filling') || msg.includes('cross detected')) {
|
|
console.log('[INFO]', msg, ...args);
|
|
}
|
|
},
|
|
error: (msg: string, ...args: any[]) => console.error('[ERROR]', msg, ...args),
|
|
warn: (msg: string, ...args: any[]) => {},
|
|
debug: (msg: string, ...args: any[]) => {},
|
|
} as any,
|
|
custom: {}
|
|
};
|
|
|
|
// Initialize services
|
|
const storageService = new StorageService();
|
|
const marketDataService = new MarketDataService(container);
|
|
const executionService = new ExecutionService(container);
|
|
const modeManager = new ModeManager(container, marketDataService, executionService, storageService);
|
|
const strategyManager = new StrategyManager(container);
|
|
|
|
// Set services in container
|
|
container.custom = {
|
|
MarketDataService: marketDataService,
|
|
ExecutionService: executionService,
|
|
ModeManager: modeManager,
|
|
StorageService: storageService
|
|
};
|
|
|
|
// 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'],
|
|
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'],
|
|
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); |