diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index a83cb7b..c0c40f0 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -781,6 +781,7 @@ export class PositionManager { let exitReason: 'TP1' | 'TP2' | 'SL' | 'SOFT_SL' | 'HARD_SL' = 'SL' // Include any previously realized profit (e.g., from TP1 partial close) + // CRITICAL: Get from original database value, not mutated in-memory value const previouslyRealized = trade.realizedPnL let runnerRealized = 0 @@ -795,8 +796,9 @@ export class PositionManager { } const totalRealizedPnL = previouslyRealized + runnerRealized - trade.realizedPnL = totalRealizedPnL - console.log(` Realized P&L snapshot → Previous: $${previouslyRealized.toFixed(2)} | Runner: $${runnerRealized.toFixed(2)} (Δ${runnerProfitPercent.toFixed(2)}%) on $${sizeForPnL.toFixed(2)} | Total: $${totalRealizedPnL.toFixed(2)}`) + // DON'T mutate trade.realizedPnL here - causes compounding on re-detection! + // trade.realizedPnL = totalRealizedPnL ← REMOVED: Causes duplicate P&L bug + console.log(` Realized P&L calculation → Previous: $${previouslyRealized.toFixed(2)} | Runner: $${runnerRealized.toFixed(2)} (Δ${runnerProfitPercent.toFixed(2)}%) on $${sizeForPnL.toFixed(2)} | Total: $${totalRealizedPnL.toFixed(2)}`) // Determine exit reason from trade state and P&L if (trade.tp2Hit) {