finished initial backtest / engine
This commit is contained in:
parent
55b4ca78c9
commit
c106a719e8
18 changed files with 1571 additions and 180 deletions
80
apps/stock/orchestrator/test-minimal-backtest.ts
Executable file
80
apps/stock/orchestrator/test-minimal-backtest.ts
Executable file
|
|
@ -0,0 +1,80 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
/**
|
||||
* Minimal test to debug order flow
|
||||
*/
|
||||
|
||||
import { BacktestEngine } from './src/backtest/BacktestEngine';
|
||||
import { StrategyManager } from './src/strategies/StrategyManager';
|
||||
import { StorageService } from './src/services/StorageService';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { TradingEngine } from '@stock-bot/core';
|
||||
import { ModeManager } from './src/core/ModeManager';
|
||||
import { MarketDataService } from './src/services/MarketDataService';
|
||||
import { ExecutionService } from './src/services/ExecutionService';
|
||||
|
||||
async function testMinimalBacktest() {
|
||||
console.log('=== Minimal Backtest Test ===\n');
|
||||
|
||||
const logger = getLogger('test');
|
||||
const container = {
|
||||
logger,
|
||||
custom: {}
|
||||
};
|
||||
|
||||
const storageService = new StorageService(container as any);
|
||||
const marketDataService = new MarketDataService(container as any);
|
||||
const executionService = new ExecutionService(container as any);
|
||||
const modeManager = new ModeManager(container as any, marketDataService, executionService, storageService);
|
||||
|
||||
// Add services to container
|
||||
container.custom = {
|
||||
ModeManager: modeManager,
|
||||
MarketDataService: marketDataService,
|
||||
ExecutionService: executionService
|
||||
};
|
||||
|
||||
const strategyManager = new StrategyManager(container as any);
|
||||
|
||||
// Add debug logging to strategy manager
|
||||
const origHandleMarketData = (strategyManager as any).handleMarketData;
|
||||
(strategyManager as any).handleMarketData = async function(data: any) {
|
||||
console.log(`>>> StrategyManager.handleMarketData called for ${data.data.symbol} @ ${data.data.close}`);
|
||||
console.log(` Active strategies: ${this.activeStrategies.size}`);
|
||||
console.log(` Strategy IDs: ${Array.from(this.activeStrategies).join(', ')}`);
|
||||
return origHandleMarketData.call(this, data);
|
||||
};
|
||||
|
||||
const backtestEngine = new BacktestEngine(container as any, storageService, strategyManager);
|
||||
|
||||
const config = {
|
||||
mode: 'backtest' as const,
|
||||
name: 'Minimal Test',
|
||||
strategy: 'sma-crossover',
|
||||
symbols: ['AAPL'],
|
||||
startDate: '2023-01-01T00:00:00Z',
|
||||
endDate: '2023-02-15T00:00:00Z', // 45 days to ensure we have enough data
|
||||
initialCapital: 100000,
|
||||
dataFrequency: '1d',
|
||||
commission: 0.001,
|
||||
slippage: 0.0001,
|
||||
speed: 'max' as const
|
||||
};
|
||||
|
||||
// Initialize mode manager with backtest config
|
||||
await modeManager.initializeMode(config);
|
||||
|
||||
try {
|
||||
const result = await backtestEngine.runBacktest(config);
|
||||
|
||||
console.log('\nResults:');
|
||||
console.log(`Total Return: ${result.metrics.totalReturn.toFixed(2)}%`);
|
||||
console.log(`Total Trades: ${result.metrics.totalTrades}`);
|
||||
console.log(`Trades in history: ${result.trades.length}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Backtest failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
testMinimalBacktest().catch(console.error);
|
||||
Loading…
Add table
Add a link
Reference in a new issue