From 8335699f276fb0ddbd0366a599d605b3fee33d1a Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Fri, 14 Nov 2025 08:27:51 +0100 Subject: [PATCH] docs: document flip-flop price data bug and fix Updated documentation to reflect critical bug found and fixed: SIGNAL_QUALITY_OPTIMIZATION_ROADMAP.md: - Added bug fix commit (795026a) to Phase 1.5 - Documented price source (Pyth price monitor) - Added validation and logging details - Included Known Issues section with real incident details - Updated monitoring examples with detailed price logging .github/copilot-instructions.md: - Added Common Pitfall #31: Flip-flop price context bug - Documented root cause: currentPrice undefined in check-risk - Real incident: Nov 14 06:05, -$1.56 loss from false positive - Two-part fix with code examples (price fetch + validation) - Lesson: Always validate financial calculation inputs - Monitoring guidance: Watch for flip-flop price check logs This ensures future AI agents and developers understand: 1. Why Pyth price fetch is needed in check-risk 2. Why validation before calculation is critical 3. The real financial impact of missing validation --- .github/copilot-instructions.md | 38 ++++++++++++++++++++++++++ SIGNAL_QUALITY_OPTIMIZATION_ROADMAP.md | 28 +++++++++++++------ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 5f0814d..1b43bb2 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -1138,6 +1138,44 @@ trade.realizedPnL += actualRealizedPnL // NOT: result.realizedPnL from SDK - **Impact:** Protects user from unlimited risk during unavailable hours. Phantom trades are rare edge cases (oracle issues, exchange rejections). - **Database tracking:** `status='phantom'`, `exitReason='manual'`, enables analysis of phantom frequency and patterns +31. **Flip-flop price context using wrong data (CRITICAL - Fixed Nov 14, 2025):** + - **Symptom:** Flip-flop detection showing "100% price move" when actual movement was 0.2%, allowing trades that should be blocked + - **Root Cause:** `currentPrice` parameter not available in check-risk endpoint (trade hasn't opened yet), so calculation used undefined/zero + - **Real incident:** Nov 14, 06:05 CET - SHORT allowed with 0.2% flip-flop, lost -$1.56 in 5 minutes + - **Bug sequence:** + 1. LONG opened at $143.86 (06:00) + 2. SHORT signal 4min later at $143.58 (0.2% move) + 3. Flip-flop check: `(undefined - 143.86) / 143.86 * 100` = garbage β†’ showed "100%" + 4. System thought it was reversal β†’ allowed trade + 5. Should have been blocked as tight-range chop + - **Fix:** Two-part fix in commits 77a9437 and 795026a: + ```typescript + // In app/api/trading/check-risk/route.ts: + // Get current price from Pyth BEFORE quality scoring + const priceMonitor = getPythPriceMonitor() + const latestPrice = priceMonitor.getCachedPrice(body.symbol) + const currentPrice = latestPrice?.price || body.currentPrice + + // In lib/trading/signal-quality.ts: + // Validate price data exists before calculation + if (!params.currentPrice || params.currentPrice === 0) { + // No current price available - apply penalty (conservative) + console.warn(`⚠️ Flip-flop check: No currentPrice available, applying penalty`) + frequencyPenalties.flipFlop = -25 + score -= 25 + } else { + const priceChangePercent = Math.abs( + (params.currentPrice - recentSignals.oppositeDirectionPrice) / + recentSignals.oppositeDirectionPrice * 100 + ) + console.log(`πŸ” Flip-flop price check: $${recentSignals.oppositeDirectionPrice.toFixed(2)} β†’ $${params.currentPrice.toFixed(2)} = ${priceChangePercent.toFixed(2)}%`) + // Apply penalty only if < 2% move + } + ``` + - **Impact:** Without this fix, flip-flop detection is useless - blocks reversals, allows chop + - **Lesson:** Always validate input data for financial calculations, especially when data might not exist yet + - **Monitoring:** Watch logs for "πŸ” Flip-flop price check: $X β†’ $Y = Z%" to verify correct calculations + ## File Conventions - **API routes:** `app/api/[feature]/[action]/route.ts` (Next.js 15 App Router) diff --git a/SIGNAL_QUALITY_OPTIMIZATION_ROADMAP.md b/SIGNAL_QUALITY_OPTIMIZATION_ROADMAP.md index f8ee0f8..1bdf0a4 100644 --- a/SIGNAL_QUALITY_OPTIMIZATION_ROADMAP.md +++ b/SIGNAL_QUALITY_OPTIMIZATION_ROADMAP.md @@ -80,9 +80,10 @@ ORDER BY MIN(signalQualityScore) DESC; ## Phase 1.5: Signal Frequency Penalties βœ… DEPLOYED Nov 14, 2025 -**Status:** Production deployment complete -**Commit:** 111e3ed -**Deployment Time:** 07:28 CET, November 14, 2025 +**Status:** Production deployment complete (with critical bug fix) +**Initial Commit:** 111e3ed (07:28 CET) +**Bug Fix Commit:** 795026a (09:22 CET) +**Container:** trading-bot-v4 ### What Was Implemented Real-time database analysis that detects overtrading and flip-flop patterns before trade execution. @@ -98,6 +99,8 @@ Real-time database analysis that detects overtrading and flip-flop patterns befo - Example chop: $154.50 SHORT β†’ $154.30 LONG (0.13% move) = blocked ⚠️ - Example reversal: $170 SHORT β†’ $153 LONG (10% move) = allowed βœ… - Blocks rapid longβ†’shortβ†’long whipsaws in tight ranges + - **BUG FIX (795026a):** Now uses Pyth price data for accurate calculations + - Previous issue: showed "100% move" for 0.2% actual movement (allowed false flip-flop) 3. **Alternating pattern (last 3 trades):** -30 points - Detects choppy market conditions @@ -105,9 +108,11 @@ Real-time database analysis that detects overtrading and flip-flop patterns befo ### Technical Details - **Function:** `getRecentSignals()` in `lib/database/trades.ts` +- **Price source:** Pyth price monitor via `getPythPriceMonitor()` in check-risk - **Architecture:** `scoreSignalQuality()` now async - **Endpoints updated:** check-risk, execute, reentry-check - **Performance:** Indexed queries, <10ms overhead +- **Validation:** Logs "πŸ” Flip-flop price check: $X β†’ $Y = Z%" for debugging ### Expected Impact - Eliminate tight-range flip-flops (Nov 14 chart: $141-145 SOL) @@ -116,17 +121,24 @@ Real-time database analysis that detects overtrading and flip-flop patterns befo - Better capital preservation in chop ### Monitoring -Watch for penalty and allowance messages in logs: +Watch for detailed penalty and allowance messages in logs: ``` +πŸ” Flip-flop price check: $143.86 β†’ $143.58 = 0.20% ⚠️ Overtrading zone: 3 signals in 30min (-20 pts) -⚠️ Flip-flop in tight range: 12min ago, only 0.13% move (-25 pts) -βœ… Direction change after 10.2% move (12min ago) - reversal allowed +⚠️ Flip-flop in tight range: 4min ago, only 0.20% move ($143.86 β†’ $143.58) (-25 pts) +βœ… Direction change after 10.0% move ($170.00 β†’ $153.00, 12min ago) - reversal allowed ⚠️ Chop pattern: last 3 trades alternating (long β†’ short β†’ long) (-30 pts) ``` +### Known Issues Fixed +- **Nov 14, 06:05 CET:** Initial deployment allowed 0.2% flip-flop due to incorrect price data + - Trade: SHORT at $143.58 (should have been blocked) + - Result: -$1.56 loss in 5 minutes + - Fix deployed: 09:22 CET (795026a) - now uses Pyth price monitor + ### Validation Plan -1. Monitor next 5-10 signals -2. Verify penalties trigger correctly +1. Monitor next 5-10 signals for accurate price calculations +2. Verify penalties trigger with correct percentages 3. Analyze if blocked signals would have lost 4. If effective, proceed to Phase 6 (range compression)