CRITICAL BUG FIX: Stop loss and take profit exits were sending duplicate Telegram notifications with compounding P&L (16 duplicates, 796x inflation). Real Incident (Dec 2, 2025): - Manual SOL-PERP SHORT position stopped out - 16 duplicate Telegram notifications received - P&L compounding: $0.23 → $12.10 → $24.21 → $183.12 (796× multiplication) - All showed identical: entry $139.64, hold 4h 5-6m, exit reason SL - First notification: Ghost detected (handled correctly) - Next 15 notifications: SL exit (all duplicates with compounding P&L) Root Cause: - Multiple monitoring loops detect SL condition simultaneously - All call executeExit() before any can remove position from tracking - Race condition: check closingInProgress → both true → both proceed - Database update happens BEFORE activeTrades.delete() - Each execution sends Telegram notification - P&L values compound across notifications Solution: Applied same atomic delete pattern as ghost detection fix (commit93dd950): - Move activeTrades.delete() to START of executeExit() (before any async operations) - Check wasInMap return value (only true for first caller, false for duplicates) - Early return if already deleted (atomic deduplication guard) - Only first loop proceeds to close, save DB, send notification - Removed redundant removeTrade() call (already deleted at start) Impact: - Prevents duplicate notifications for SL, TP1, TP2, emergency stops - Ensures accurate P&L reporting (no compounding) - Database receives correct single exit record - User receives ONE notification per exit (as intended) Code Changes: - Line ~1520: Added atomic delete guard for full closes (percentToClose >= 100) - Line ~1651: Removed redundant removeTrade() call - Both changes prevent race condition at function entry Scope: - ✅ Stop loss exits: Fixed - ✅ Take profit 2 exits: Fixed - ✅ Emergency stops: Fixed - ✅ Trailing stops: Fixed - ℹ️ Take profit 1: Not affected (partial close keeps position in monitoring) Related: - Ghost detection fix: commit93dd950(Dec 2, 2025) - same pattern, different function - Manual trade enhancement: commit23277b7(Dec 2, 2025) - unrelated feature - P&L compounding series: Common Pitfalls #48-49, #59-61, #67 in docs
82 KiB
82 KiB