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
This commit is contained in:
mindesbunister
2025-12-15 17:16:24 +01:00
parent cb597ca020
commit 6894bc1f68

View File

@@ -1914,10 +1914,15 @@ export class PositionManager {
return 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 // Update trade state
if (percentToClose >= 100) { if (percentToClose >= 100) {
// Full close - remove from monitoring // Full close - remove from monitoring
trade.realizedPnL += result.realizedPnL || 0
// Save to database (only for valid exit reasons) // Save to database (only for valid exit reasons)
if (reason !== 'error') { if (reason !== 'error') {