cleanup and fixed initial capital / commision / splippage
This commit is contained in:
parent
70da2c68e5
commit
d14380d740
19 changed files with 1344 additions and 33 deletions
122
apps/stock/orchestrator/test-initial-capital-debug.ts
Normal file
122
apps/stock/orchestrator/test-initial-capital-debug.ts
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
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 testInitialCapitalDebug() {
|
||||
console.log('Debugging 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 Debug',
|
||||
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=== DEBUG INFO ===');
|
||||
console.log(`Initial Capital: $${config.initialCapital}`);
|
||||
|
||||
// Check equity curve
|
||||
console.log('\nEquity Curve:');
|
||||
result.equity.forEach((point, idx) => {
|
||||
if (idx < 3 || idx >= result.equity.length - 3) {
|
||||
console.log(` ${new Date(point.timestamp).toISOString()}: $${point.value.toFixed(2)}`);
|
||||
}
|
||||
if (idx === 3 && result.equity.length > 6) {
|
||||
console.log(' ...');
|
||||
}
|
||||
});
|
||||
|
||||
// Get trading engine to check P&L
|
||||
const tradingEngine = strategyManager.getTradingEngine();
|
||||
if (tradingEngine) {
|
||||
const [realized, unrealized] = tradingEngine.getTotalPnl();
|
||||
console.log(`\nRust Core P&L:`);
|
||||
console.log(` Realized: $${realized.toFixed(2)}`);
|
||||
console.log(` Unrealized: $${unrealized.toFixed(2)}`);
|
||||
console.log(` Total P&L: $${(realized + unrealized).toFixed(2)}`);
|
||||
console.log(` Expected Final Value: $${(config.initialCapital + realized + unrealized).toFixed(2)}`);
|
||||
}
|
||||
|
||||
// Check trades
|
||||
console.log('\nTrades:');
|
||||
result.trades.forEach((t, idx) => {
|
||||
console.log(` ${idx + 1}. ${t.symbol} ${t.side} ${t.quantity} @ $${t.entryPrice} -> $${t.exitPrice}`);
|
||||
console.log(` P&L: $${t.pnl.toFixed(2)} (${t.pnlPercent.toFixed(2)}%)`);
|
||||
});
|
||||
|
||||
// The issue calculation
|
||||
const finalValue = result.equity[result.equity.length - 1].value;
|
||||
console.log('\n=== ISSUE ANALYSIS ===');
|
||||
console.log(`Final portfolio value: $${finalValue.toFixed(2)}`);
|
||||
console.log(`Initial capital: $${config.initialCapital}`);
|
||||
console.log(`Difference: $${(finalValue - config.initialCapital).toFixed(2)}`);
|
||||
console.log(`Return %: ${((finalValue - config.initialCapital) / config.initialCapital * 100).toFixed(2)}%`);
|
||||
|
||||
// Check if it's using 100k
|
||||
const assumedInitial = 100000;
|
||||
const assumedReturn = (finalValue - assumedInitial) / assumedInitial * 100;
|
||||
console.log(`\nIf initial was $100k:`);
|
||||
console.log(` Return would be: ${assumedReturn.toFixed(2)}%`);
|
||||
console.log(` P&L would be: $${(finalValue - assumedInitial).toFixed(2)}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Backtest failed:', error);
|
||||
}
|
||||
|
||||
console.log('\n=== TEST COMPLETE ===');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
testInitialCapitalDebug().catch(error => {
|
||||
console.error('Test failed:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue