stock-bot/apps/stock/orchestrator/test-equity-spike-simple.ts
2025-07-03 18:49:48 -04:00

105 lines
No EOL
3.8 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 testEquitySpike() {
console.log('Testing equity curve spike fix...\n');
// Create minimal service container
const container: IServiceContainer = {
logger: {
info: (msg: string, ...args: any[]) => console.log('[INFO]', msg, ...args),
error: (msg: string, ...args: any[]) => console.error('[ERROR]', msg, ...args),
warn: (msg: string, ...args: any[]) => console.warn('[WARN]', msg, ...args),
debug: (msg: string, ...args: any[]) => console.log('[DEBUG]', msg, ...args),
} 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-01-01T00:00:00Z',
endDate: '2023-01-10T00:00:00Z', // Just 10 days
speed: 'max',
symbols: ['TEST'],
initialCapital: 100000,
dataFrequency: '1d',
strategy: 'sma-crossover'
});
// Create backtest engine
const backtestEngine = new BacktestEngine(container, storageService, strategyManager);
// Run backtest
const config = {
mode: 'backtest',
name: 'Equity Spike Test',
strategy: 'sma-crossover',
symbols: ['TEST'],
startDate: '2023-01-01T00:00:00Z',
endDate: '2023-01-10T00:00:00Z',
initialCapital: 100000,
commission: 0.001,
slippage: 0.0001,
dataFrequency: '1d',
speed: 'max'
};
console.log('Running backtest...');
const result = await backtestEngine.runBacktest(config);
// Analyze equity curve
console.log('\n=== EQUITY CURVE ANALYSIS ===');
console.log(`Initial Capital: $${config.initialCapital.toLocaleString()}`);
console.log(`Equity points: ${result.equity.length}`);
if (result.equity.length > 0) {
console.log('\nFirst 3 equity points:');
result.equity.slice(0, 3).forEach((point, i) => {
console.log(` ${i + 1}. ${point.date} => $${point.value.toFixed(2)}`);
});
const firstValue = result.equity[0].value;
const hasSpike = firstValue === 0 || firstValue < config.initialCapital * 0.9;
console.log(`\nFirst value: $${firstValue.toFixed(2)}`);
console.log(`Expected: $${config.initialCapital.toFixed(2)}`);
console.log(`Difference: $${Math.abs(firstValue - config.initialCapital).toFixed(2)}`);
if (hasSpike) {
console.log('\n❌ SPIKE DETECTED! Portfolio value starts at 0 or too low.');
} else if (Math.abs(firstValue - config.initialCapital) < 1) {
console.log('\n✅ SUCCESS! Portfolio value correctly starts at initial capital.');
} else {
console.log('\n⚠ WARNING: Small difference detected, but no major spike.');
}
}
console.log('\n=== TEST COMPLETE ===');
process.exit(0);
}
testEquitySpike().catch(error => {
console.error('Test failed:', error);
process.exit(1);
});