Files
gogo2/TREND_PREDICTION_FIX.md
2025-11-22 19:25:27 +02:00

151 lines
4.6 KiB
Markdown

# Trend Prediction Fix - Real Data vs Synthetic
## Problem Discovered
The model's trend predictions were "way off" because training was using **SYNTHETIC/FAKE trend data**.
### Before (Synthetic Data):
```python
# ANNOTATE/core/real_training_adapter.py (lines 1622-1633)
if direction == 'LONG':
trend_angle = 0.785 # Fixed 45 degrees!
trend_direction = 1.0
elif direction == 'SHORT':
trend_angle = -0.785 # Fixed -45 degrees!
trend_direction = -1.0
else:
trend_angle = 0.0
trend_direction = 0.0
```
**Problem**: Model was trained on fixed angles (always ±45°) instead of learning actual price trends!
## Root Cause
This violated the user's strict rule:
> "NEVER USE SYNTHETIC/MOCK DATA. ONLY USE REAL DATA. REMOVE ALL MOCK DATA implementations"
The model was learning:
- ✗ "LONG trades = 45° angle"
- ✗ "SHORT trades = -45° angle"
- ✗ "HOLD = 0° angle"
Instead of learning:
- ✓ "This uptrend is steep (60°)"
- ✓ "This downtrend is shallow (-20°)"
- ✓ "This sideways move is flat (5°)"
## Solution
**Calculate REAL trend from actual price data** (lines 1618-1652):
```python
# Get last 10 candles
recent_closes = price_data[0, -10:, 3] # Last 10 close prices
# Calculate actual price change
price_start = recent_closes[0].item()
price_end = recent_closes[-1].item()
price_delta = price_end - price_start
time_delta = 9.0 # 10 candles = 9 intervals
# Calculate REAL angle using atan2
trend_angle = math.atan2(price_delta, time_delta * price_start / 100.0)
# Calculate REAL steepness (magnitude)
price_change_pct = abs(price_delta / price_start)
trend_steepness = min(price_change_pct * 100.0, 1.0)
# Calculate REAL direction
trend_direction = 1.0 if price_delta > 0 else -1.0
```
## Impact
### Before Fix:
- Trend predictions always showed ±45° angles
- Model couldn't distinguish steep vs shallow trends
- Yellow trend line was always wrong
- Trend accuracy: **probably 0%**
### After Fix:
- Trend predictions will match actual price movement
- Model learns real market dynamics
- Yellow trend line will be accurate
- Trend accuracy should improve significantly
## UI Improvements
Also fixed the yellow trend line zoom issue:
- **Before**: Projected 5 minutes ahead → threw off chart zoom
- **After**: Projects based on timeframe (1s: 30s, 1m: 2min, 1h: 30min)
## Testing Instructions
1. **Delete old checkpoints** (they have synthetic trend training):
```bash
rm -rf models/checkpoints/transformer/*
```
2. **Restart training**:
- Load Transformer model (fresh start)
- Start "Live Inference + Per-Candle Training"
3. **Monitor trend predictions**:
- Watch the **yellow trend line** on charts
- It should now match actual price movement direction
- Check console logs for `Trend loss` and `trend_accuracy`
4. **Expected improvements**:
- Trend line should point in correct direction
- Angle should match actual steepness
- After 5-10 epochs, trend accuracy should be 40-60%
## Files Modified
1. `/mnt/shared/DEV/repos/d-popov.com/gogo2/ANNOTATE/core/real_training_adapter.py`
- Lines 1618-1652: Calculate real trend from actual price data
2. `/mnt/shared/DEV/repos/d-popov.com/gogo2/ANNOTATE/web/static/js/chart_manager.js`
- Lines 2749-2756: Adjusted trend line projection to avoid zoom issues
- Re-enabled trend line visualization (was temporarily disabled)
## Technical Details
### Trend Vector Components:
- **angle**: Calculated using `atan2(price_delta, time_delta)` - ranges from -π to +π
- **steepness**: Normalized price change percentage (0 to 1)
- **direction**: Sign of price movement (+1 up, -1 down, 0 flat)
### Training Loss:
```python
# NN/models/advanced_transformer_trading.py (line 1355)
total_loss = action_loss + 0.1 * price_loss + 0.05 * trend_loss + 0.15 * candle_loss
```
Trend loss weight is **0.05** (5% of total loss).
### Accuracy Calculation:
```python
# Lines 1484-1501
angle_accuracy = (1.0 - clamp(angle_error_deg / 180.0, 0, 1)).mean()
steepness_accuracy = (1.0 - clamp(steepness_error, 0, 1)).mean()
trend_accuracy = (angle_accuracy + steepness_accuracy) / 2
```
## Why This Matters
Trend prediction is critical for:
1. **Trade timing**: Knowing if trend is accelerating or decelerating
2. **Risk management**: Steep trends are riskier
3. **Position sizing**: Adjust size based on trend strength
4. **Signal confidence**: Strong trends = higher confidence
With synthetic data, the model was **blind to actual trend dynamics**!
## Status
**FIXED** - Now using real trend data
**PENDING** - User needs to test with fresh training run
📊 **EXPECTED** - Trend accuracy should improve from 0% to 40-60% after training