docs: Add Common Pitfall #62 - Missing quality threshold validation

- Bug: Execute endpoint calculated quality but never validated it
- Three trades executed at quality 30/50/50 (threshold: 90/95)
- All three stopped out, confirming low quality = losing trades
- Root cause: TradingView sent incomplete data (metrics=0, old v5) + missing validation after timeframe check
- Fix: Added validation block lines 193-213 in execute/route.ts
- Returns HTTP 400 if quality < minQualityScore
- Deployed: Nov 27, 2025 23:16 UTC (commit cefa3e6)
- Lesson: Calculate ≠ Validate - minQualityScore must be enforced at ALL execution pathways

This documents the CRITICAL FIX from commit cefa3e6.
Per Nov 27 mandatory documentation rules, work is INCOMPLETE without copilot-instructions.md updates.
This commit is contained in:
mindesbunister
2025-11-27 23:28:26 +01:00
parent cefa3e646d
commit 3cd292d90d

View File

@@ -4329,6 +4329,76 @@ trade.realizedPnL += actualRealizedPnL // NOT: result.realizedPnL from SDK
- **Deployed:** Nov 23, container rebuilt (71.8s), all services running
- **Lesson:** When async processing modifies collections during iteration, always guard against stale references. Array snapshots don't protect against this - need explicit membership checks. ALL monitoring code paths need duplicate prevention, not just error scenarios.
62. **Execute endpoint bypassing quality threshold validation (CRITICAL - Fixed Nov 27, 2025):**
- **Symptom:** Bot executed trades at quality 30, 50, 50 when minimum threshold is 90 (LONG) / 95 (SHORT)
- **Root Cause:** Execute endpoint calculated quality score but never validated it after timeframe='5' confirmation
- **Real incidents (Nov 27, 2025):**
* Trade cmihwkjmb0088m407lqd8mmbb: Quality 30, entry $142.63, 20:45:23, exit SOFT_SL
* Trade cmih6ghn20002ql07zxfvna1l: Quality 50, entry $142.31, 08:34:24, exit SL
* Trade cmih5vrpu0001ql076mj3nm63: Quality 50, entry $142.92, 08:18:17, exit SL
* **All three stopped out** - confirms low quality = losing trades
- **TradingView sent incomplete data:**
```
Risk check for: {
timeframe: '5',
atr: 0,
adx: 0,
rsi: 0,
volumeRatio: 0,
pricePosition: 0,
indicatorVersion: 'v5' // Old indicator, not current v9
}
```
* All metrics = 0 → Quality calculated as 30
* Old indicator v5 still firing (should be v9)
- **Bug sequence:**
1. Execute endpoint calculates quality score (line 130)
2. Gets minQualityScore = getMinQualityScoreForDirection() (line 137)
3. Handles data collection for non-5min timeframes with quality check (line 142-189)
4. Confirms "✅ 5min signal confirmed - proceeding with trade execution" (line 191)
5. **SKIPS quality validation** - proceeds directly to position sizing (line 195+)
6. Opens position with quality 30 when threshold requires 90
- **Impact:** Three trades executed way below threshold, all exited at stop loss, financial losses from preventable low-quality signals
- **Fix (Nov 27, 2025 - Lines 193-213 in execute/route.ts):**
```typescript
// CRITICAL FIX (Nov 27, 2025): Verify quality score meets minimum threshold
// Bug: Quality 30 trade executed because no quality check after timeframe validation
// Three trades (quality 30/50/50) all stopped out - low quality = losing trades
if (qualityResult.score < minQualityScore) {
console.log(`❌ QUALITY TOO LOW: ${qualityResult.score} < ${minQualityScore} threshold`)
console.log(` Direction: ${direction}, Threshold: ${minQualityScore}`)
console.log(` Quality breakdown: ${JSON.stringify(qualityResult.breakdown)}`)
return NextResponse.json({
success: false,
error: 'Quality score too low',
message: `Signal quality ${qualityResult.score} below ${minQualityScore} minimum for ${direction}. ` +
`Score breakdown: ADX penalty ${qualityResult.breakdown.adxPenalty}, ` +
`ATR penalty ${qualityResult.breakdown.atrPenalty}, ` +
`RSI penalty ${qualityResult.breakdown.rsiPenalty}`
}, { status: 400 })
}
console.log(`✅ Quality check passed: ${qualityResult.score} >= ${minQualityScore}`)
console.log(` Direction: ${direction}, proceeding with trade execution`)
```
- **Behavior now:**
* After timeframe='5' confirmation, validates quality score
* If quality < minQualityScore: Returns HTTP 400 with detailed error
* If quality >= minQualityScore: Logs success and continues to execution
* Prevents ANY signal below 90/95 threshold from executing
- **Files changed:** `/home/icke/traderv4/app/api/trading/execute/route.ts` (lines 193-213)
- **Git commit:** cefa3e6 "critical: MANDATORY quality score check in execute endpoint"
- **Deployed:** Nov 27, 2025 23:16 UTC (container restarted with fix)
- **TradingView cleanup needed:**
* User should verify 5-minute chart using v9 indicator (not old v5)
* Disable/delete any old v5 alerts sending incomplete data
* Verify webhook sends complete metrics: atr, adx, rsi, volumeRatio, pricePosition
- **Verification:** Next 5-minute signal will test fix
* Quality < 90: Should see "❌ QUALITY TOO LOW" log + HTTP 400 error
* Quality >= 90: Should see "✅ Quality check passed" log + execution proceeds
- **Lesson:** Calculating a value (minQualityScore) doesn't mean it's enforced. EVERY execution pathway must validate quality threshold explicitly. Data collection timeframes had quality check, but production execution path didn't - need validation at ALL entry points.
61. **P&L compounding STILL happening despite all guards (CRITICAL - UNDER INVESTIGATION Nov 24, 2025):**
- **Symptom:** Trade cmici8j640001ry074d7leugt showed $974.05 P&L in database when actual was $72.41 (13.4× inflation)
- **Evidence:** 14 duplicate Telegram notifications, each with compounding P&L ($71.19 → $68.84 → $137.69 → ... → $974.05)