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
This commit is contained in:
38
.github/copilot-instructions.md
vendored
38
.github/copilot-instructions.md
vendored
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user