docs: Document 1-minute webhook fix and add setup instructions (Common Pitfall #80)

This commit is contained in:
mindesbunister
2025-12-09 18:49:32 +01:00
parent 96683497f4
commit 31f6c0f212
2 changed files with 426 additions and 0 deletions

View File

@@ -3524,6 +3524,109 @@ This section contains the **TOP 10 MOST CRITICAL** pitfalls that every AI agent
- **Deploy Status:** ✅ DEPLOYED Dec 9, 2025 17:07 CET
- **Status:** ✅ Fixed - Queue now restores pending signals on startup, production logging enabled
80. **CRITICAL: 1-Minute Market Data Webhook Action Mismatch - Fresh ATR Data Never Arriving (CRITICAL - Dec 9, 2025):**
- **Symptom:** Telegram bot timing out waiting for fresh ATR data, falling back to stale preset (0.43)
- **User Report:** "for some reason we are not getting fresh atr data from the 1 minute data feed"
- **Financial Impact:** Manual trades executing with stale volatility metrics instead of fresh real-time data
- **Real Incident (Dec 9, 2025 19:00):**
* User sent "long sol" via Telegram
* Bot response: "⏳ Waiting for next 1-minute datapoint... Will execute with fresh ATR (max 60s)"
* After 60s: "⚠️ Timeout waiting for fresh data. Using preset ATR: 0.43"
* Cache inspection: Only contained "manual" timeframe data (97 seconds old), no fresh 1-minute data
* Logs: No "Received market data webhook" entries
- **Root Cause - Webhook Action Validation Mismatch:**
* File: `app/api/trading/market-data/route.ts` lines 64-71
* Endpoint validated: `if (body.action !== 'market_data')` (exact string match)
* TradingView alert sends: `"action": "market_data_1min"` (line 54 in 1min_market_data_feed.pinescript)
* Result: Webhook returned 400 Bad Request, data never cached
* Smart entry timer polled empty cache, timed out after 60 seconds
- **THE FIX (Dec 9, 2025 - DEPLOYED):**
```typescript
// BEFORE (lines 64-71):
if (body.action !== 'market_data') {
return NextResponse.json(
{ error: 'Invalid action - expected "market_data"' },
{ status: 400 }
)
}
// AFTER:
const validActions = ['market_data', 'market_data_1min']
if (!validActions.includes(body.action)) {
return NextResponse.json(
{ error: `Invalid action - expected one of: ${validActions.join(', ')}` },
{ status: 400 }
)
}
```
- **Why This Fix:**
* Endpoint now accepts BOTH action variants
* TradingView 1-minute alerts use "market_data_1min" to distinguish from 5-minute signals
* Higher timeframe alerts (15min, 1H, 4H, Daily) use "market_data"
* Single endpoint serves both data collection systems
* Error message updated to show all valid options
- **Build Challenges:**
* Initial build used cached layers, fix not included in compiled code
* Required `docker compose build trading-bot --no-cache` to force TypeScript recompilation
* Verification: `docker exec trading-bot-v4 grep "validActions" /app/.next/server/app/api/trading/market-data/route.js`
- **Verification Complete (Dec 9, 2025 19:18 CET):**
* Manual test: `curl -X POST /api/trading/market-data -d '{"action": "market_data_1min", ...}'`
* Response: `{"success": true, "symbol": "SOL-PERP", "message": "Market data cached and stored successfully"}`
* Cache inspection: Fresh data with ATR 0.55, ADX 28.5, RSI 62, timeframe "1", age 9 seconds
* Logs: "📡 Received market data webhook: { action: 'market_data_1min', symbol: 'SOLUSDT', atr: 0.55 }"
* Logs: "✅ Market data cached for SOL-PERP"
- **Expected Behavior After Fix:**
* TradingView 1-minute alert fires → webhook accepted → data cached
* Telegram "long sol" command → waits for next datapoint → receives fresh data within 60s
* Bot shows: "✅ Fresh data received | ATR: 0.55 | ADX: 28.5 | RSI: 62.0"
* Trade executes with real-time ATR-based TP/SL targets (not stale preset)
- **Why This Matters:**
* **This is a REAL MONEY system** - stale volatility metrics = wrong position sizing
* ATR changes with market conditions (0.43 preset vs 0.55 actual = 28% difference)
* TP/SL targets calculated from ATR multipliers (2.0×, 4.0×, 3.0×)
* Wrong ATR = targets too tight (missed profits) or too wide (unnecessary risk)
* User's manual trades require fresh data for optimal execution
- **Prevention Rules:**
1. NEVER use exact string match for webhook action validation (use array inclusion)
2. ALWAYS accept multiple action variants when endpoints serve similar purposes
3. ALWAYS verify Docker build includes TypeScript changes (check compiled JS)
4. ALWAYS test webhook endpoints with curl before declaring fix working
5. Add monitoring alerts when cache shows only stale "manual" timeframe data
6. Log webhook rejections with 400 errors for debugging
- **Red Flags Indicating This Bug:**
* Telegram bot times out waiting for fresh data every time
* Cache only contains "manual" timeframe (not "1" for 1-minute data)
* No "Received market data webhook" logs in container output
* TradingView alerts configured but endpoint returns 400 errors
* Bot always falls back to preset metrics (ATR 0.43, ADX 32, RSI 58/42)
- **Files Changed:**
* app/api/trading/market-data/route.ts (Lines 64-71 - webhook validation)
- **TradingView Alert Setup:**
* Alert name: "1-Minute Market Data Feed"
* Chart: SOL-PERP 1-minute timeframe
* Condition: "1min Market Data" indicator, "Once Per Bar Close"
* Webhook URL: n8n or direct bot endpoint
* Alert message: Auto-generated JSON with `"action": "market_data_1min"`
* Expected rate: 1 alert per minute (60/hour per symbol)
- **Troubleshooting Commands:**
```bash
# Check if webhook firing
docker logs trading-bot-v4 2>&1 | grep "Received market data webhook" | tail -5
# Check cache contents
curl -s http://localhost:3001/api/trading/market-data | jq '.cache."SOL-PERP"'
# Check data age (should be < 60 seconds)
curl -s http://localhost:3001/api/trading/market-data | jq '.cache."SOL-PERP".ageSeconds'
# Monitor webhook hits in real-time
docker logs -f trading-bot-v4 2>&1 | grep "market data webhook"
```
- **Git commit:** 9668349 "fix: Accept market_data_1min action in webhook endpoint" (Dec 9, 2025)
- **Deploy Status:** ✅ DEPLOYED Dec 9, 2025 19:18 CET (--no-cache build)
- **Status:** ✅ Fixed - Endpoint accepts both action variants, fresh data flow operational
- **Documentation:** `docs/1MIN_ALERT_SETUP_INSTRUCTIONS.md` - Complete setup guide for TradingView alerts
72. **CRITICAL: MFE Data Unit Mismatch - ALWAYS Filter by Date (CRITICAL - Dec 5, 2025):**
- **Symptom:** SQL analysis shows "20%+ average MFE" but TP1 (0.6% target) never hits
- **Root Cause:** Old Trade records stored MFE/MAE in DOLLARS, new records store PERCENTAGES