diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 64cfcdd..f068399 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -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) - **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):** - - **Symptom:** Drift UI shows 6 trades executed in last 6 hours, database shows only 1 trade, bot logs show NO API requests - - **Root Cause:** Bot container restarted at 06:51 UTC, but trades executed BEFORE restart (00:20-05:56) weren't fully saved +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 3 trades, P&L values inflated 5-14× + - **Root Cause:** P&L compounding bug in external closure detection - `trade.realizedPnL` reading from in-memory object with stale/accumulated values - **Evidence:** - * n8n executions: 08:15, 06:45, 05:35, 01:45, 01:20 (all succeeded) - * Drift trades: 6 trades showing P&L from $18.42 to -$99.93 (total ~-$52 loss) - * Database: Only 3 v8 trades, with one showing $581 profit (10× inflated) - * Bot logs: ZERO API requests after 06:51 restart - - **Two Separate Issues:** - 1. **Historical trades (before restart):** Database P&L values are inflated due to P&L compounding bug (Common Pitfall #49) - - One SHORT trade shows $581 profit in DB - - Actual: $138.88 → $137.74 = 0.82% move × $7232 position = **$59 expected** - - Inflated by ~10× due to P&L accumulation during external closure detection - 2. **Recent trades (after restart):** Bot NOT receiving signals from n8n at all - - n8n shows successful executions at 08:15, 06:45 - - Bot logs show ZERO incoming requests - - API is accessible (returns 401 on auth test) - - **Conclusion:** n8n is calling a DIFFERENT endpoint or the workflow changed - - **Impact:** - * Cannot trust database P&L values for v8 trades - * System appears to be "working" in n8n but bot isn't actually trading - * Analytics dashboard showing completely wrong performance metrics - - **Temporary Status:** - * Real v8 performance: ~$52 LOSS (from Drift UI) - * Database shows: $420 profit (WRONG - includes inflated $581 trade) - * Need to fix: P&L compounding bug + n8n→bot connection - - **Next Steps:** - 1. Verify n8n workflow URLs point to correct bot endpoint - 2. Fix P&L compounding in external closure detection (Common Pitfall #49 fix didn't work) - 3. Correct historical database records with actual Drift transaction data - 4. Add API request logging to catch silent failures - - **Lesson:** Always cross-reference database records with exchange UI after system changes. Container restarts can expose silent failures in the execution pipeline. + * Database showed: $581, $273, -$434 P&L for 3 trades (total $420 profit) + * Actual P&L: $59.35, $19.79, -$87.88 (total -$8.74 loss) + * Inflation: 9.8×, 13.8×, 5× respectively + * Missing trades: ~3 trades from pre-06:51 container restart not in database + - **Bug Mechanism:** + 1. External closure detected, calculates: `totalRealizedPnL = previouslyRealized + runnerRealized` + 2. `previouslyRealized` reads from in-memory `trade.realizedPnL` (could be stale) + 3. Updates database with compounded value + 4. If detected again (race condition/rate limits), reads ALREADY ACCUMULATED value + 5. Adds more P&L, compounds 2×, 5×, 10× + - **Fix Applied (Nov 19, 2025):** + * Removed `previouslyRealized` from external closure P&L calculation + * Now calculates ONLY current closure's P&L: `totalRealizedPnL = runnerRealized` + * Database corrected via SQL UPDATE: recalculated P&L from entry/exit prices + * Formula: `positionSizeUSD × ((exitPrice - entryPrice) / entryPrice)` for longs + * Code: lib/trading/position-manager.ts lines 785-803 + - **Verification (08:40 CET):** + * New v8 trade executed successfully + * n8n workflow functioning (22.49s execution) + * Database save working correctly + * Analytics now shows accurate -$8.74 baseline + - **Current Status:** + * System fully operational + * Analytics baseline: 3 trades, -$8.74 P&L, 66.7% WR + * Historical gap: ~3 pre-restart trades not in DB (explains -$52 Drift vs -$8.74 DB) + * All future trades will track accurately + - **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. 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