87 lines
No EOL
3.4 KiB
TypeScript
87 lines
No EOL
3.4 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 testSimple() {
|
||
// Create service container
|
||
const container: IServiceContainer = {
|
||
logger: {
|
||
info: (msg: string, ...args: any[]) => {},
|
||
error: (msg: string, ...args: any[]) => {},
|
||
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
|
||
};
|
||
|
||
// Test with 1000 initial capital
|
||
const config = {
|
||
mode: 'backtest',
|
||
name: 'Test',
|
||
strategy: 'sma-crossover',
|
||
symbols: ['AAPL'],
|
||
startDate: '2023-01-01T00:00:00Z',
|
||
endDate: '2023-01-31T00:00:00Z',
|
||
initialCapital: 1000,
|
||
commission: 0.001,
|
||
slippage: 0.0001,
|
||
dataFrequency: '1d',
|
||
speed: 'max'
|
||
};
|
||
|
||
await modeManager.initializeMode(config);
|
||
const backtestEngine = new BacktestEngine(container, storageService, strategyManager);
|
||
const result = await backtestEngine.runBacktest(config);
|
||
|
||
// Get P&L from Rust
|
||
const tradingEngine = strategyManager.getTradingEngine();
|
||
const [realized, unrealized] = tradingEngine.getTotalPnl();
|
||
|
||
console.log('\n=== RESULTS ===');
|
||
console.log(`Config Initial Capital: $${config.initialCapital}`);
|
||
console.log(`First Equity Value: $${result.equity[0].value.toFixed(2)}`);
|
||
console.log(`Final Equity Value: $${result.equity[result.equity.length - 1].value.toFixed(2)}`);
|
||
console.log(`\nRust Core:`);
|
||
console.log(` Realized P&L: $${realized.toFixed(2)}`);
|
||
console.log(` Unrealized P&L: $${unrealized.toFixed(2)}`);
|
||
console.log(` Total P&L: $${(realized + unrealized).toFixed(2)}`);
|
||
console.log(`\nCalculated Final Value:`);
|
||
console.log(` $${config.initialCapital} + $${(realized + unrealized).toFixed(2)} = $${(config.initialCapital + realized + unrealized).toFixed(2)}`);
|
||
console.log(`\nActual vs Expected:`);
|
||
const actualFinal = result.equity[result.equity.length - 1].value;
|
||
const expectedFinal = config.initialCapital + realized + unrealized;
|
||
console.log(` Actual: $${actualFinal.toFixed(2)}`);
|
||
console.log(` Expected: $${expectedFinal.toFixed(2)}`);
|
||
console.log(` Difference: $${(actualFinal - expectedFinal).toFixed(2)}`);
|
||
|
||
// Check if it's using 100k base
|
||
if (Math.abs(actualFinal - (100000 + realized + unrealized)) < 1) {
|
||
console.log('\n⚠️ BUG CONFIRMED: Using 100000 as base instead of ' + config.initialCapital);
|
||
}
|
||
|
||
process.exit(0);
|
||
}
|
||
|
||
testSimple().catch(error => {
|
||
console.error('Test failed:', error);
|
||
process.exit(1);
|
||
}); |