critical: Fix MAE/MFE storing percentages instead of dollars + duplicate Telegram notifications

CRITICAL BUGS FIXED (Nov 19, 2025):

1. MAE/MFE Bug:
   - Was storing: account percentage (profit % × leverage)
   - Example: 1.31% move × 15x = 19.73% stored as MFE
   - Should store: actual dollar P&L (81 not 19.73%)
   - Impact: Telegram shows 'Max Gain: +19.73%' instead of '+.XX'
   - Fix: Changed from accountPnL (leverage-adjusted %) to currentPnLDollars
   - Lines 964-987: Removed accountPnL calculation, use currentPnLDollars

2. Duplicate Notification Bug:
   - handleExternalClosure() was checking if trade removed AFTER removal
   - Result: 16 duplicate Telegram notifications with compounding P&L
   - Example: 6 → 2 → 11 → ... → 81 (16 notifications for 1 close)
   - Fix: Check if trade already removed BEFORE processing
   - Lines 382-391: Move duplicate check to START of function
   - Early return prevents notification send if already processed

3. Database Compounding (NOT A BUG):
   - Nov 17 fix (Common Pitfall #49) still working correctly
   - Only 1 database record with 81 P&L
   - Issue was notification duplication, not DB duplication

IMPACT:
- MAE/MFE data now usable for TP/SL optimization
- Telegram notifications accurate (1 per close, correct P&L)
- Database analytics will show real dollar movements
- Next trade will have correct Max Gain/Drawdown display

FILES:
- lib/trading/position-manager.ts: MAE/MFE calculation + duplicate check
This commit is contained in:
mindesbunister
2025-11-19 07:45:58 +01:00
parent 5146f37acc
commit 267456f699
2 changed files with 18 additions and 15 deletions

2
.env
View File

@@ -406,7 +406,7 @@ MAX_SCALE_MULTIPLIER=2
SCALE_SIZE_PERCENT=50
MIN_ADX_INCREASE=5
MAX_PRICE_POSITION_FOR_SCALE=70
TRAILING_STOP_ATR_MULTIPLIER=1.8
TRAILING_STOP_ATR_MULTIPLIER=2.5
TRAILING_STOP_MIN_PERCENT=0.25
TRAILING_STOP_MAX_PERCENT=2.5
USE_PERCENTAGE_SIZE=false