104 lines
No EOL
3.9 KiB
TypeScript
104 lines
No EOL
3.9 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 testInitialCapital() {
|
||
console.log('Testing Initial Capital Issue...\n');
|
||
|
||
// Create 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
|
||
};
|
||
|
||
// Test with LOW initial capital
|
||
const config = {
|
||
mode: 'backtest',
|
||
name: 'Initial Capital Test',
|
||
strategy: 'sma-crossover',
|
||
symbols: ['AAPL'],
|
||
startDate: '2023-01-01T00:00:00Z',
|
||
endDate: '2023-01-31T00:00:00Z', // Just one month
|
||
initialCapital: 1000, // LOW CAPITAL
|
||
commission: 0.001,
|
||
slippage: 0.0001,
|
||
dataFrequency: '1d',
|
||
speed: 'max'
|
||
};
|
||
|
||
// Initialize backtest mode
|
||
await modeManager.initializeMode(config);
|
||
|
||
// Create backtest engine
|
||
const backtestEngine = new BacktestEngine(container, storageService, strategyManager);
|
||
|
||
console.log('Running backtest with initial capital: $' + config.initialCapital);
|
||
|
||
try {
|
||
const result = await backtestEngine.runBacktest(config);
|
||
|
||
console.log('\n=== RESULTS ===');
|
||
console.log(`Initial Capital: $${config.initialCapital}`);
|
||
console.log(`Final Value: $${result.equity[result.equity.length - 1].value.toFixed(2)}`);
|
||
console.log(`Total Return: ${result.metrics.totalReturn.toFixed(2)}%`);
|
||
console.log(`Total Trades: ${result.metrics.totalTrades}`);
|
||
|
||
// Check if portfolio grew from 1000
|
||
const finalValue = result.equity[result.equity.length - 1].value;
|
||
const expectedRange = config.initialCapital * 0.5; // Within 50% of initial
|
||
|
||
if (Math.abs(finalValue - config.initialCapital) > expectedRange &&
|
||
Math.abs(finalValue - 100000) < expectedRange) {
|
||
console.log('\n⚠️ WARNING: Portfolio appears to be using 100000 as initial capital!');
|
||
} else {
|
||
console.log('\n✅ Portfolio appears to be using correct initial capital');
|
||
}
|
||
|
||
// Check first few trades
|
||
if (result.trades.length > 0) {
|
||
console.log('\nFirst trade:');
|
||
const t = result.trades[0];
|
||
console.log(` ${t.symbol} ${t.side} ${t.quantity} shares @ $${t.entryPrice}`);
|
||
console.log(` Trade value: $${(t.quantity * t.entryPrice).toFixed(2)}`);
|
||
|
||
if (t.quantity * t.entryPrice > config.initialCapital) {
|
||
console.log(' ⚠️ WARNING: Trade size exceeds initial capital!');
|
||
}
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('Backtest failed:', error);
|
||
}
|
||
|
||
console.log('\n=== TEST COMPLETE ===');
|
||
process.exit(0);
|
||
}
|
||
|
||
testInitialCapital().catch(error => {
|
||
console.error('Test failed:', error);
|
||
process.exit(1);
|
||
}); |