- CRITICAL BUG: trade.realizedPnL was being mutated during each external closure detection - This caused exponential compounding: $6 → $12 → $24 → $48 → $96 - Each time monitoring loop detected closure, it added previouslyRealized + runnerRealized - But previouslyRealized was the ALREADY ACCUMULATED value from previous iteration - Result: P&L compounded 15-20x on actual value ROOT CAUSE (line 797): const totalRealizedPnL = previouslyRealized + runnerRealized trade.realizedPnL = totalRealizedPnL // ← BUG: Mutates in-memory trade object Next detection cycle: const previouslyRealized = trade.realizedPnL // ← Gets ACCUMULATED value const totalRealizedPnL = previouslyRealized + runnerRealized // ← Adds AGAIN FIX: - Don't mutate trade.realizedPnL during external closure detection - Calculate totalRealizedPnL locally, use for database update only - trade.realizedPnL stays immutable after initial DB save - Log message clarified: 'P&L calculation' not 'P&L snapshot' IMPACT: - Every external closure (TP/SL on-chain orders) affected - With rate limiting, closure detected 15-20 times before removal - Real example: $6 actual profit showed as $92.46 in database - This is WORSE than duplicate notification bug - corrupts financial data FILES CHANGED: - lib/trading/position-manager.ts: Removed trade.realizedPnL mutation (line 799) - Database manually corrected: $92.46 → $6.00 for affected trade RELATED BUGS: - Common Pitfall #48: closingInProgress flag prevents some duplicates - But doesn't help if monitoring loop runs DURING external closure detection - Need both fixes: closingInProgress + no mutation of trade.realizedPnL
64 KiB
64 KiB