425 lines
11 KiB
Markdown
425 lines
11 KiB
Markdown
# 🔧 D1 Data Loading Fix
|
|||
|
|
|
||
|
|
**Status:** ✅ FIXED
|
||
|
|
**Date:** 2026-01-21
|
||
|
|
**Issue:** "Keine Daten für D1" error during automated trading checks
|
||
|
|
**Solution:** Robust MT5 data loading with retry logic
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 🐛 Problem Identified
|
||
|
|
|
||
|
|
**Symptom:**
|
||
|
|
```
|
||
|
|
✅ Position-Check OK: 0/1
|
||
|
|
🔍 Analyzing XAUUSD with V1.6 ADAPTIVE COMPLETE parameters...
|
||
|
|
⏳ Retry 1/3 for D1...
|
||
|
|
⏳ Retry 2/3 for D1...
|
||
|
|
❌ Failed to load D1 after 3 retries
|
||
|
|
⚠️ Keine Daten für D1
|
||
|
|
❌ Signal-Analyse fehlgeschlagen
|
||
|
|
```
|
||
|
|
|
||
|
|
**Root Cause:**
|
||
|
|
- `get_rates()` function called MT5 API without checking connection state
|
||
|
|
- No retry logic at data source level
|
||
|
|
- MT5 connection can be unstable during scheduler runs
|
||
|
|
- D1 timeframe requires more time to load than lower timeframes
|
||
|
|
|
||
|
|
**Impact:**
|
||
|
|
- Enhanced Signal Scoring activated but cannot run
|
||
|
|
- No signal analysis possible → No trades
|
||
|
|
- Bot essentially non-functional
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## ✅ Solution Implemented
|
||
|
|
|
||
|
|
### Updated `get_rates()` Function (Cell 17)
|
||
|
|
|
||
|
|
**New Features:**
|
||
|
|
|
||
|
|
1. **MT5 Connection Check**
|
||
|
|
```python
|
||
|
|
# Check if MT5 is initialized
|
||
|
|
if not mt.initialize():
|
||
|
|
print(f"⚠️ MT5 not initialized, attempting to reconnect...")
|
||
|
|
time.sleep(1)
|
||
|
|
continue
|
||
|
|
```
|
||
|
|
|
||
|
|
2. **Symbol Visibility Check**
|
||
|
|
```python
|
||
|
|
# Check symbol is selected
|
||
|
|
symbol_info = mt.symbol_info(symbol)
|
||
|
|
if not symbol_info.visible:
|
||
|
|
if not mt.symbol_select(symbol, True):
|
||
|
|
print(f"⚠️ Failed to select symbol {symbol}")
|
||
|
|
return None
|
||
|
|
```
|
||
|
|
|
||
|
|
3. **3-Attempt Retry Logic**
|
||
|
|
```python
|
||
|
|
for attempt in range(max_retries):
|
||
|
|
rates = mt.copy_rates_from_pos(symbol, timeframes_dict[timeframe], 0, count)
|
||
|
|
|
||
|
|
if rates is None or len(rates) == 0:
|
||
|
|
if attempt < max_retries - 1:
|
||
|
|
print(f" ⏳ No data for {timeframe.upper()}, retry {attempt + 1}/{max_retries}...")
|
||
|
|
time.sleep(2) # Longer wait for D1
|
||
|
|
continue
|
||
|
|
```
|
||
|
|
|
||
|
|
4. **Better Error Messages**
|
||
|
|
```python
|
||
|
|
except Exception as e:
|
||
|
|
print(f" ❌ Error loading {timeframe.upper()} after {max_retries} retries: {e}")
|
||
|
|
return None
|
||
|
|
```
|
||
|
|
|
||
|
|
**Key Improvements:**
|
||
|
|
- ✅ Checks MT5 initialization before each attempt
|
||
|
|
- ✅ Ensures symbol is visible and selected
|
||
|
|
- ✅ 2-second wait between retries (longer for D1)
|
||
|
|
- ✅ 3 retry attempts with detailed error logging
|
||
|
|
- ✅ Retries at data source level (more efficient)
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 🚀 How to Apply Fix
|
||
|
|
|
||
|
|
### Step 1: Restart Kernel
|
||
|
|
|
||
|
|
```
|
||
|
|
Jupyter: Kernel → Restart & Clear Output
|
||
|
|
```
|
||
|
|
|
||
|
|
**CRITICAL:** Must restart to load updated `get_rates()` function!
|
||
|
|
|
||
|
|
### Step 2: Run All Cells
|
||
|
|
|
||
|
|
```
|
||
|
|
Jupyter: Cell → Run All
|
||
|
|
```
|
||
|
|
|
||
|
|
Wait for all cells to complete (2-3 minutes).
|
||
|
|
|
||
|
|
### Step 3: Verify Fix
|
||
|
|
|
||
|
|
**Check Cell 17 Output:**
|
||
|
|
```
|
||
|
|
✅ Helper functions defined (with robust MT5 retry logic)
|
||
|
|
```
|
||
|
|
|
||
|
|
**Wait for next trading check** (happens every 1 minute).
|
||
|
|
|
||
|
|
**Expected Output:**
|
||
|
|
```
|
||
|
|
✅ Position-Check OK: 0/1
|
||
|
|
|
||
|
|
🔍 Analyzing XAUUSD with V1.6 ADAPTIVE COMPLETE parameters...
|
||
|
|
|
||
|
|
📊 V1.6 ADAPTIVE COMPLETE Trend-Analyse für XAUUSD
|
||
|
|
⚡ Adaptive Interval: 1 min | Session: LONDON
|
||
|
|
🎯 Market Regime: TRENDING (Strength: 75%)
|
||
|
|
🎚️ Adaptive Threshold: 60% (RELAXED)
|
||
|
|
|
||
|
|
+------+----------+----------+---------+-----------+----------+
|
||
|
|
| TF | Trend | Strength | ATR | Slope | Price |
|
||
|
|
+------+----------+----------+---------+-----------+----------+
|
||
|
|
| D1 | uptrend | 1.45 | 12.3456 | 0.002345 | 2864.50 |
|
||
|
|
| H4 | uptrend | 1.32 | 8.7654 | 0.001234 | 2864.50 |
|
||
|
|
| H1 | uptrend | 1.28 | 5.4321 | 0.000987 | 2864.50 |
|
||
|
|
| M30 | uptrend | 1.15 | 3.2109 | 0.000654 | 2864.50 |
|
||
|
|
| M15 | uptrend | 1.05 | 2.1098 | 0.000432 | 2864.50 |
|
||
|
|
| M5 | uptrend | 0.98 | 1.5432 | 0.000321 | 2864.50 |
|
||
|
|
+------+----------+----------+---------+-----------+----------+
|
||
|
|
|
||
|
|
➡️ Standard-Trend: uptrend (Strength: 1.38)
|
||
|
|
➡️ Fast-Trend: uptrend (Required: 2/4)
|
||
|
|
➡️ Top-Down-Trend: uptrend
|
||
|
|
➡️ Confidence: 85.0% (Threshold: 60.0%)
|
||
|
|
➡️ Risk-Adjusted Strength: 125.3 (Min: 80)
|
||
|
|
➡️ Signal Quality: GOOD
|
||
|
|
|
||
|
|
🚀 V1.6 Adaptive Complete: Full Features + Adaptive Rhythm
|
||
|
|
|
||
|
|
🎯 Calculating Enhanced Signal Score...
|
||
|
|
|
||
|
|
✅ Enhanced Signal Scoring:
|
||
|
|
Trend Score: 85.0/100
|
||
|
|
Volume Score: 90.0/100
|
||
|
|
Momentum Score: 75.0/100
|
||
|
|
S/R Score: 82.0/100
|
||
|
|
Fibonacci Score: 88.0/100
|
||
|
|
─────────────────────────────────────
|
||
|
|
📊 Base Confidence: 85.0%
|
||
|
|
🎯 Enhanced Score: 84.3%
|
||
|
|
📈 Signal Quality: EXCELLENT
|
||
|
|
|
||
|
|
💡 Analysis: Strong trend (85%), High volume, Good momentum
|
||
|
|
|
||
|
|
🎯 Signal qualified! 84.3% >= 60.0%
|
||
|
|
|
||
|
|
✅ Trade executed with enhanced confidence: 84.3%
|
||
|
|
```
|
||
|
|
|
||
|
|
**If you see D1 data loading successfully → FIX WORKED!** ✅
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 🧪 Test D1 Loading Manually
|
||
|
|
|
||
|
|
**Run this in a new cell to test:**
|
||
|
|
|
||
|
|
```python
|
||
|
|
# Test D1 data loading
|
||
|
|
print("🧪 Testing D1 data loading...")
|
||
|
|
print("=" * 70)
|
||
|
|
|
||
|
|
import time
|
||
|
|
|
||
|
|
for i in range(3):
|
||
|
|
print(f"\n📊 Attempt {i+1}/3:")
|
||
|
|
|
||
|
|
df = get_rates("d1", 150, "XAUUSD")
|
||
|
|
|
||
|
|
if df is not None:
|
||
|
|
print(f" ✅ D1 data loaded: {len(df)} bars")
|
||
|
|
print(f" Latest close: {df['close'].iloc[-1]:.2f}")
|
||
|
|
print(f" ATR: {df['atr'].iloc[-1]:.4f}")
|
||
|
|
break
|
||
|
|
else:
|
||
|
|
print(f" ❌ D1 data loading failed")
|
||
|
|
if i < 2:
|
||
|
|
print(f" ⏳ Waiting 2 seconds before retry...")
|
||
|
|
time.sleep(2)
|
||
|
|
|
||
|
|
print("\n" + "=" * 70)
|
||
|
|
```
|
||
|
|
|
||
|
|
**Expected Output:**
|
||
|
|
```
|
||
|
|
🧪 Testing D1 data loading...
|
||
|
|
======================================================================
|
||
|
|
|
||
|
|
📊 Attempt 1/3:
|
||
|
|
✅ D1 data loaded: 150 bars
|
||
|
|
Latest close: 2864.50
|
||
|
|
ATR: 12.3456
|
||
|
|
|
||
|
|
======================================================================
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## ⚠️ If Still Failing
|
||
|
|
|
||
|
|
### Problem: D1 still returns None after 3 retries
|
||
|
|
|
||
|
|
**Possible Causes:**
|
||
|
|
|
||
|
|
1. **MT5 Not Running**
|
||
|
|
- Open MetaTrader 5
|
||
|
|
- Ensure logged in to trading account
|
||
|
|
- Check market watch shows XAUUSD
|
||
|
|
|
||
|
|
2. **Symbol Not Available**
|
||
|
|
- Right-click in Market Watch
|
||
|
|
- Select "Show All"
|
||
|
|
- Find XAUUSD and enable
|
||
|
|
|
||
|
|
3. **No Historical Data**
|
||
|
|
- In MT5: View → Symbols
|
||
|
|
- Find XAUUSD
|
||
|
|
- Click "Properties"
|
||
|
|
- Check "Show in Market Watch"
|
||
|
|
- Go to "Charts" tab
|
||
|
|
- Request historical data
|
||
|
|
|
||
|
|
4. **MT5 Connection Issue**
|
||
|
|
```python
|
||
|
|
# Test MT5 connection
|
||
|
|
import MetaTrader5 as mt
|
||
|
|
|
||
|
|
if not mt.initialize():
|
||
|
|
print("❌ MT5 initialization failed")
|
||
|
|
else:
|
||
|
|
print("✅ MT5 connected")
|
||
|
|
|
||
|
|
symbol_info = mt.symbol_info("XAUUSD")
|
||
|
|
if symbol_info is None:
|
||
|
|
print("❌ XAUUSD not found")
|
||
|
|
else:
|
||
|
|
print(f"✅ XAUUSD found: {symbol_info.bid}/{symbol_info.ask}")
|
||
|
|
|
||
|
|
# Try to get 10 D1 bars
|
||
|
|
rates = mt.copy_rates_from_pos("XAUUSD", mt.TIMEFRAME_D1, 0, 10)
|
||
|
|
if rates is None:
|
||
|
|
print("❌ Cannot load D1 data")
|
||
|
|
print(f" Error: {mt.last_error()}")
|
||
|
|
else:
|
||
|
|
print(f"✅ D1 data: {len(rates)} bars")
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 📊 What Changed
|
||
|
|
|
||
|
|
### Before (Cell 17):
|
||
|
|
```python
|
||
|
|
def get_rates(timeframe="h4", count=200, symbol="XAUUSD"):
|
||
|
|
timeframes_dict = {...}
|
||
|
|
try:
|
||
|
|
rates = mt.copy_rates_from_pos(symbol, timeframes_dict[timeframe], 0, count)
|
||
|
|
if rates is None:
|
||
|
|
return None
|
||
|
|
# ... process data
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Error getting rates: {e}")
|
||
|
|
return None
|
||
|
|
```
|
||
|
|
|
||
|
|
**Problems:**
|
||
|
|
- ❌ No MT5 connection check
|
||
|
|
- ❌ No retry logic
|
||
|
|
- ❌ Single attempt only
|
||
|
|
- ❌ No symbol visibility check
|
||
|
|
|
||
|
|
### After (Cell 17):
|
||
|
|
```python
|
||
|
|
def get_rates(timeframe="h4", count=200, symbol="XAUUSD", max_retries=3):
|
||
|
|
timeframes_dict = {...}
|
||
|
|
|
||
|
|
for attempt in range(max_retries):
|
||
|
|
try:
|
||
|
|
# Check MT5 initialized
|
||
|
|
if not mt.initialize():
|
||
|
|
print(f"⚠️ MT5 not initialized, attempting to reconnect...")
|
||
|
|
time.sleep(1)
|
||
|
|
continue
|
||
|
|
|
||
|
|
# Check symbol visible
|
||
|
|
symbol_info = mt.symbol_info(symbol)
|
||
|
|
if not symbol_info.visible:
|
||
|
|
mt.symbol_select(symbol, True)
|
||
|
|
|
||
|
|
# Get rates with retry
|
||
|
|
rates = mt.copy_rates_from_pos(...)
|
||
|
|
|
||
|
|
if rates is None or len(rates) == 0:
|
||
|
|
if attempt < max_retries - 1:
|
||
|
|
print(f" ⏳ No data for {timeframe.upper()}, retry {attempt + 1}/{max_retries}...")
|
||
|
|
time.sleep(2) # Longer wait
|
||
|
|
continue
|
||
|
|
|
||
|
|
# ... process data
|
||
|
|
return df
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f" ❌ Error: {e}")
|
||
|
|
time.sleep(2)
|
||
|
|
|
||
|
|
return None
|
||
|
|
```
|
||
|
|
|
||
|
|
**Improvements:**
|
||
|
|
- ✅ Checks MT5 initialization
|
||
|
|
- ✅ Ensures symbol is visible
|
||
|
|
- ✅ 3 retry attempts
|
||
|
|
- ✅ 2-second wait between retries
|
||
|
|
- ✅ Detailed error messages
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 🎯 Expected Results
|
||
|
|
|
||
|
|
After this fix:
|
||
|
|
|
||
|
|
1. **D1 Data Loads Successfully**
|
||
|
|
- Signal analysis completes
|
||
|
|
- Enhanced Scoring can run
|
||
|
|
- Trading resumes
|
||
|
|
|
||
|
|
2. **Better Reliability**
|
||
|
|
- Handles temporary MT5 connection issues
|
||
|
|
- Recovers from symbol visibility problems
|
||
|
|
- More robust during high-load periods
|
||
|
|
|
||
|
|
3. **Enhanced Logging**
|
||
|
|
- See exactly which retry attempt succeeded
|
||
|
|
- Understand when/why data loading fails
|
||
|
|
- Better debugging information
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 📚 Technical Details
|
||
|
|
|
||
|
|
### Why D1 Specifically Failed
|
||
|
|
|
||
|
|
**Hypothesis:**
|
||
|
|
- D1 data requires more processing time from MT5
|
||
|
|
- Lower timeframes (M5, M15, etc.) load faster
|
||
|
|
- During scheduler runs, D1 request times out
|
||
|
|
- Connection state not verified before request
|
||
|
|
|
||
|
|
**Solution:**
|
||
|
|
- Add 2-second wait between retries (vs 1 second)
|
||
|
|
- Check MT5 initialization state before each attempt
|
||
|
|
- Ensure symbol is selected in Market Watch
|
||
|
|
- Retry 3 times before giving up
|
||
|
|
|
||
|
|
### Retry Logic Flow
|
||
|
|
|
||
|
|
```
|
||
|
|
Attempt 1:
|
||
|
|
Check MT5 initialized → Yes
|
||
|
|
Check symbol visible → Yes
|
||
|
|
Request D1 data → None (timeout)
|
||
|
|
Wait 2 seconds...
|
||
|
|
|
||
|
|
Attempt 2:
|
||
|
|
Check MT5 initialized → Yes
|
||
|
|
Check symbol visible → Yes
|
||
|
|
Request D1 data → None (still loading)
|
||
|
|
Wait 2 seconds...
|
||
|
|
|
||
|
|
Attempt 3:
|
||
|
|
Check MT5 initialized → Yes
|
||
|
|
Check symbol visible → Yes
|
||
|
|
Request D1 data → Success! 150 bars
|
||
|
|
Return DataFrame ✅
|
||
|
|
```
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## ✅ Success Checklist
|
||
|
|
|
||
|
|
After restarting kernel and running all cells:
|
||
|
|
|
||
|
|
- [ ] Cell 17 shows "with robust MT5 retry logic"
|
||
|
|
- [ ] No "Keine Daten für D1" errors in logs
|
||
|
|
- [ ] Signal analysis completes successfully
|
||
|
|
- [ ] Enhanced Signal Scoring shows component breakdown
|
||
|
|
- [ ] Trading checks show all 6 timeframes (D1, H4, H1, M30, M15, M5)
|
||
|
|
- [ ] Bot executes trades (if signals qualify)
|
||
|
|
|
||
|
|
If all ✅ → **D1 DATA LOADING FIXED!** 🎉
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## 🆘 Still Need Help?
|
||
|
|
|
||
|
|
If D1 data still fails after these fixes:
|
||
|
|
|
||
|
|
1. **Share MT5 connection test output** (see "If Still Failing" section)
|
||
|
|
2. **Check MT5 terminal logs** (View → Logs)
|
||
|
|
3. **Verify XAUUSD symbol properties** in MT5
|
||
|
|
4. **Test manual D1 loading** in new notebook cell
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
**🎯 Generated with [Claude Code](https://claude.com/claude-code)**
|
||
|
|
|
||
|
|
**Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>**
|