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
|
|
@ -99,6 +99,8 @@ export class BacktestEngine extends EventEmitter {
|
|||
private microstructures: Map<string, MarketMicrostructure> = new Map();
|
||||
private container: IServiceContainer;
|
||||
private initialCapital: number = 100000;
|
||||
private commission: number = 0.001; // Default 0.1%
|
||||
private slippage: number = 0.0001; // Default 0.01%
|
||||
private pendingOrders: Map<string, any> = new Map();
|
||||
private ordersListenerSetup = false;
|
||||
|
||||
|
|
@ -116,7 +118,8 @@ export class BacktestEngine extends EventEmitter {
|
|||
includeDarkPools: true,
|
||||
latencyMs: 1
|
||||
});
|
||||
this.performanceAnalyzer = new PerformanceAnalyzer();
|
||||
// Don't create performance analyzer here - wait for runBacktest
|
||||
// this.performanceAnalyzer = new PerformanceAnalyzer(this.initialCapital);
|
||||
|
||||
// Set up order listener immediately
|
||||
this.setupOrderListener();
|
||||
|
|
@ -132,6 +135,11 @@ export class BacktestEngine extends EventEmitter {
|
|||
this.reset();
|
||||
this.isRunning = true;
|
||||
this.initialCapital = validatedConfig.initialCapital;
|
||||
this.commission = validatedConfig.commission || 0.001;
|
||||
this.slippage = validatedConfig.slippage || 0.0001;
|
||||
|
||||
// Recreate performance analyzer with correct initial capital
|
||||
this.performanceAnalyzer = new PerformanceAnalyzer(this.initialCapital);
|
||||
|
||||
// Initialize equity curve with starting capital
|
||||
this.equityCurve.push({
|
||||
|
|
@ -145,6 +153,8 @@ export class BacktestEngine extends EventEmitter {
|
|||
this.initialCapital
|
||||
);
|
||||
|
||||
this.container.logger.info(`[BacktestEngine] Initial capital set to ${this.initialCapital}, equity curve initialized with ${this.equityCurve.length} points`);
|
||||
|
||||
// Generate backtest ID
|
||||
const backtestId = `backtest_${Date.now()}`;
|
||||
|
||||
|
|
@ -668,12 +678,16 @@ export class BacktestEngine extends EventEmitter {
|
|||
private async updateEquityCurve(): Promise<void> {
|
||||
const totalEquity = await this.getPortfolioValue();
|
||||
|
||||
this.container.logger.debug(`[updateEquityCurve] currentTime=${this.currentTime}, totalEquity=${totalEquity}, initialCapital=${this.initialCapital}`);
|
||||
|
||||
// Don't add duplicate points at the same timestamp
|
||||
const lastPoint = this.equityCurve[this.equityCurve.length - 1];
|
||||
if (lastPoint && lastPoint.timestamp === this.currentTime) {
|
||||
// Update the value instead of adding a duplicate
|
||||
this.container.logger.debug(`[updateEquityCurve] Updating existing point at ${this.currentTime}: ${lastPoint.value} -> ${totalEquity}`);
|
||||
lastPoint.value = totalEquity;
|
||||
} else {
|
||||
this.container.logger.debug(`[updateEquityCurve] Adding new point at ${this.currentTime}: ${totalEquity}`);
|
||||
this.equityCurve.push({
|
||||
timestamp: this.currentTime,
|
||||
value: totalEquity
|
||||
|
|
@ -684,6 +698,7 @@ export class BacktestEngine extends EventEmitter {
|
|||
private async getPortfolioValue(): Promise<number> {
|
||||
const tradingEngine = this.strategyManager.getTradingEngine();
|
||||
if (!tradingEngine) {
|
||||
this.container.logger.debug(`[getPortfolioValue] No trading engine, returning initialCapital: ${this.initialCapital}`);
|
||||
return this.initialCapital;
|
||||
}
|
||||
|
||||
|
|
@ -692,9 +707,12 @@ export class BacktestEngine extends EventEmitter {
|
|||
const [realized, unrealized] = tradingEngine.getTotalPnl();
|
||||
const totalValue = this.initialCapital + realized + unrealized;
|
||||
|
||||
this.container.logger.debug(`[getPortfolioValue] P&L: realized=${realized}, unrealized=${unrealized}, initialCapital=${this.initialCapital}, totalValue=${totalValue}`);
|
||||
|
||||
// Ensure we never return 0 or negative values that would cause spikes
|
||||
// This handles the case where getTotalPnl might not be initialized yet
|
||||
if (totalValue <= 0 || isNaN(totalValue)) {
|
||||
this.container.logger.debug(`[getPortfolioValue] Invalid totalValue, returning initialCapital: ${this.initialCapital}`);
|
||||
return this.initialCapital;
|
||||
}
|
||||
|
||||
|
|
@ -941,13 +959,6 @@ export class BacktestEngine extends EventEmitter {
|
|||
return profile.map(v => v / sum);
|
||||
}
|
||||
|
||||
private getPortfolioValue(): number {
|
||||
const tradingEngine = this.strategyManager.getTradingEngine();
|
||||
if (!tradingEngine) {return 100000;} // Default initial capital
|
||||
|
||||
const [realized, unrealized] = tradingEngine.getTotalPnl();
|
||||
return 100000 + realized + unrealized;
|
||||
}
|
||||
|
||||
async stopBacktest(): Promise<void> {
|
||||
this.isRunning = false;
|
||||
|
|
@ -1160,8 +1171,8 @@ export class BacktestEngine extends EventEmitter {
|
|||
if (!orderEvent) continue;
|
||||
|
||||
const fillPrice = orderEvent.order.side === 'buy' ?
|
||||
currentPrice * (1 + (this.marketSimulator.config.slippage || 0.0001)) :
|
||||
currentPrice * (1 - (this.marketSimulator.config.slippage || 0.0001));
|
||||
currentPrice * (1 + this.slippage) :
|
||||
currentPrice * (1 - this.slippage);
|
||||
|
||||
const fill = {
|
||||
orderId,
|
||||
|
|
@ -1170,7 +1181,7 @@ export class BacktestEngine extends EventEmitter {
|
|||
quantity: orderEvent.order.quantity,
|
||||
price: fillPrice,
|
||||
timestamp: this.currentTime,
|
||||
commission: orderEvent.order.quantity * fillPrice * 0.001, // 0.1% commission
|
||||
commission: orderEvent.order.quantity * fillPrice * this.commission,
|
||||
strategyId: orderEvent.strategyId
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue