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:
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user