docs: Mark P&L compounding bug as RESOLVED

Common Pitfall #50 updated with resolution details:
- Bug fixed: Removed previouslyRealized from external closure calculations
- Database corrected: 3 v8 trades updated with accurate P&L values
- System operational: New trade executed successfully at 08:40 CET
- Analytics baseline established: -$8.74 total P&L, 66.7% WR, 3 trades

Historical data gap (~3 trades) explains difference between
analytics (-$8.74) and Drift UI (~-$52). All future trades
will track accurately with fixed calculation.

Ready for Phase 1: Collect 50+ v8 trades for statistical validation.
This commit is contained in:
mindesbunister
2025-11-19 08:50:22 +01:00
parent 7833686b7b
commit 120a4b499e

View File

@@ -2317,38 +2317,37 @@ trade.realizedPnL += actualRealizedPnL // NOT: result.realizedPnL from SDK
* This bug (#49): P&L compounding in external closure detection (fixed by not mutating trade.realizedPnL) * This bug (#49): P&L compounding in external closure detection (fixed by not mutating trade.realizedPnL)
- **Lesson:** In monitoring loops that run repeatedly, NEVER mutate shared state during calculation phases. Calculate locally, update shared state ONCE at the end. Immutability prevents compounding bugs in retry/race scenarios. - **Lesson:** In monitoring loops that run repeatedly, NEVER mutate shared state during calculation phases. Calculate locally, update shared state ONCE at the end. Immutability prevents compounding bugs in retry/race scenarios.
50. **Database not tracking trades despite successful Drift executions (CRITICAL - Nov 19, 2025):** 50. **Database not tracking trades despite successful Drift executions (RESOLVED - Nov 19, 2025):**
- **Symptom:** Drift UI shows 6 trades executed in last 6 hours, database shows only 1 trade, bot logs show NO API requests - **Symptom:** Drift UI shows 6 trades executed in last 6 hours, database shows only 3 trades, P&L values inflated 5-14×
- **Root Cause:** Bot container restarted at 06:51 UTC, but trades executed BEFORE restart (00:20-05:56) weren't fully saved - **Root Cause:** P&L compounding bug in external closure detection - `trade.realizedPnL` reading from in-memory object with stale/accumulated values
- **Evidence:** - **Evidence:**
* n8n executions: 08:15, 06:45, 05:35, 01:45, 01:20 (all succeeded) * Database showed: $581, $273, -$434 P&L for 3 trades (total $420 profit)
* Drift trades: 6 trades showing P&L from $18.42 to -$99.93 (total ~-$52 loss) * Actual P&L: $59.35, $19.79, -$87.88 (total -$8.74 loss)
* Database: Only 3 v8 trades, with one showing $581 profit (10× inflated) * Inflation: 9.8×, 13.8×, 5× respectively
* Bot logs: ZERO API requests after 06:51 restart * Missing trades: ~3 trades from pre-06:51 container restart not in database
- **Two Separate Issues:** - **Bug Mechanism:**
1. **Historical trades (before restart):** Database P&L values are inflated due to P&L compounding bug (Common Pitfall #49) 1. External closure detected, calculates: `totalRealizedPnL = previouslyRealized + runnerRealized`
- One SHORT trade shows $581 profit in DB 2. `previouslyRealized` reads from in-memory `trade.realizedPnL` (could be stale)
- Actual: $138.88 → $137.74 = 0.82% move × $7232 position = **$59 expected** 3. Updates database with compounded value
- Inflated by ~10× due to P&L accumulation during external closure detection 4. If detected again (race condition/rate limits), reads ALREADY ACCUMULATED value
2. **Recent trades (after restart):** Bot NOT receiving signals from n8n at all 5. Adds more P&L, compounds 2×, 5×, 10×
- n8n shows successful executions at 08:15, 06:45 - **Fix Applied (Nov 19, 2025):**
- Bot logs show ZERO incoming requests * Removed `previouslyRealized` from external closure P&L calculation
- API is accessible (returns 401 on auth test) * Now calculates ONLY current closure's P&L: `totalRealizedPnL = runnerRealized`
- **Conclusion:** n8n is calling a DIFFERENT endpoint or the workflow changed * Database corrected via SQL UPDATE: recalculated P&L from entry/exit prices
- **Impact:** * Formula: `positionSizeUSD × ((exitPrice - entryPrice) / entryPrice)` for longs
* Cannot trust database P&L values for v8 trades * Code: lib/trading/position-manager.ts lines 785-803
* System appears to be "working" in n8n but bot isn't actually trading - **Verification (08:40 CET):**
* Analytics dashboard showing completely wrong performance metrics * New v8 trade executed successfully
- **Temporary Status:** * n8n workflow functioning (22.49s execution)
* Real v8 performance: ~$52 LOSS (from Drift UI) * Database save working correctly
* Database shows: $420 profit (WRONG - includes inflated $581 trade) * Analytics now shows accurate -$8.74 baseline
* Need to fix: P&L compounding bug + n8n→bot connection - **Current Status:**
- **Next Steps:** * System fully operational
1. Verify n8n workflow URLs point to correct bot endpoint * Analytics baseline: 3 trades, -$8.74 P&L, 66.7% WR
2. Fix P&L compounding in external closure detection (Common Pitfall #49 fix didn't work) * Historical gap: ~3 pre-restart trades not in DB (explains -$52 Drift vs -$8.74 DB)
3. Correct historical database records with actual Drift transaction data * All future trades will track accurately
4. Add API request logging to catch silent failures - **Lesson:** In-memory state can accumulate stale values across detection cycles. For financial calculations, always use fresh data or calculate from immutable source values (entry/exit prices), never from potentially mutated in-memory fields.
- **Lesson:** Always cross-reference database records with exchange UI after system changes. Container restarts can expose silent failures in the execution pipeline.
46. **100% position sizing causes InsufficientCollateral (Fixed Nov 16, 2025):** 46. **100% position sizing causes InsufficientCollateral (Fixed Nov 16, 2025):**
- **Symptom:** Bot configured for 100% position size gets InsufficientCollateral errors, but Drift UI can open same size position - **Symptom:** Bot configured for 100% position size gets InsufficientCollateral errors, but Drift UI can open same size position