From 6894bc1f681b587928466a6f4e5f54a5bc8cc5bc Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Mon, 15 Dec 2025 17:16:24 +0100 Subject: [PATCH] critical: Fix P&L accumulation for two-stage closes (Bug #85) - Bug: TP1 profit never accumulated, only TP2 profit recorded - Impact: Database understated all two-stage trades by TP1 amount - Example: Actual 7.69 (5.87 TP1 + 1.82 TP2) showed as 5.83 - Fix: Accumulate realizedPnL for ALL closes (partial and full) - Root Cause: percentToClose >= 100 check excluded TP1 (60%) from P&L update - File: lib/trading/position-manager.ts line 1916-1920 - Discovered: Dec 15, 2025 - User caught calculation error --- lib/trading/position-manager.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index 965e409..1debb08 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -1914,10 +1914,15 @@ export class PositionManager { return } + // CRITICAL FIX (Dec 15, 2025): Accumulate P&L for BOTH partial and full closes + // Bug was: TP1 (60% close) never updated trade.realizedPnL, only TP2 (100% close) did + // Result: Database only showed TP2 runner profit, missing TP1 profit + trade.realizedPnL += result.realizedPnL || 0 + logger.log(`💰 P&L accumulated: +$${(result.realizedPnL || 0).toFixed(2)} | Total: $${trade.realizedPnL.toFixed(2)}`) + // Update trade state if (percentToClose >= 100) { // Full close - remove from monitoring - trade.realizedPnL += result.realizedPnL || 0 // Save to database (only for valid exit reasons) if (reason !== 'error') {