Files
gogo2/TREND_TARGET_IMPROVEMENT.md
2025-12-08 17:35:16 +02:00

5.1 KiB

Trend Target Calculation - Forward-Looking Approach

Problem

Trend targets were calculated from historical price movement (last 10 candles), not from future price movement (what will actually happen).

This meant the model was learning:

  • "The price WAS going up" → predict "up trend"
  • ✓ Should learn: "The price WILL go up" → predict "up trend"

Solution

Changed trend target calculation to use FUTURE price from the next candle:

Before (Historical):

# Used last 10 candles to calculate trend
recent_closes = price_data[0, -10:, 3]  
price_start = recent_closes[0]  # 10 candles ago
price_end = recent_closes[-1]   # Current candle
trend_angle = atan2(price_end - price_start, ...)  # Past trend

Problem: Model learns to predict what already happened!

After (Forward-Looking):

# Use current price and NEXT candle price
current_price = price_data[0, -1, 3]  # Current close
future_candle = _extract_next_candle(...)  # Next candle (ground truth)
future_price = future_candle[3]  # Future close

# Calculate trend from NOW to FUTURE
price_delta = future_price - current_price
trend_angle = atan2(price_delta, ...)  # FUTURE trend!

Benefit: Model learns to predict what WILL happen!

How It Works

1. Primary Method - Future Candle Data

# Try 1s timeframe first (most responsive)
if future_candle_1s available:
    future_price = future_candle_1s[3]  # Close
    
# Fallback to 1m timeframe
elif future_candle_1m available:
    future_price = future_candle_1m[3]
    
# Calculate trend from current to future
trend_angle = atan2(future_price - current_price, ...)
trend_steepness = abs(future_price - current_price) / current_price
trend_direction = +1 (up) or -1 (down)

2. Fallback Method - Recent Historical Trend

# Only if future data not available
if no future data:
    # Use last 5 candles as fallback
    recent_closes = price_data[0, -5:, 3]
    trend_angle = atan2(recent_closes[-1] - recent_closes[0], ...)

Impact

Training Behavior:

  • Before: "Model, learn that when price went up, trend was up" (obvious!)
  • After: "Model, learn to predict when price WILL go up" (useful!)

Trend Line Display:

The yellow trend line now shows:

  • Prediction: Model's forecast of future trend
  • Target: What actually happened (for comparison)

This lets you see if the model is learning correctly!

Technical Details

Trend Vector Components:

  • angle (radians): Direction and steepness combined

    • Positive = upward trend
    • Negative = downward trend
    • Magnitude = steepness
  • steepness (0 to 1): How steep the trend is

    • Calculated as: abs(price_delta / current_price) * 100
    • Capped at 1.0
  • direction (+1, 0, -1): Simple direction indicator

    • +1.0 = up
    • -1.0 = down
    • 0.0 = sideways

Loss Calculation:

# NN/models/advanced_transformer_trading.py
trend_pred = [model_angle, model_steepness, model_direction]
trend_target = [actual_angle, actual_steepness, actual_direction]
trend_loss = MSE(trend_pred, trend_target)

total_loss = action_loss + 0.1*price_loss + 0.05*trend_loss + 0.15*candle_loss

Trend loss weight = 5% of total loss.

Example

Current State:

  • Current price: $2750
  • Last 5 candles: $2740, $2745, $2748, $2749, $2750 (trending up)

Old Method (Historical):

trend_target:
  angle = atan2(2750 - 2740, ...) = 0.15 rad ≈ 8.6°
  direction = +1.0 (up)

→ Model learns "price was going up"

New Method (Forward-Looking):

Next candle (ground truth): Close = $2755

trend_target:
  angle = atan2(2755 - 2750, ...) = 0.25 rad ≈ 14.3°  
  direction = +1.0 (up)

→ Model learns "price WILL go up by $5"

Why This Matters

  1. Predictive vs Reactive:

    • Old: Reactive (learns what happened)
    • New: Predictive (learns what will happen)
  2. Trading Value:

    • Old: "Market was bullish" (too late!)
    • New: "Market will be bullish" (actionable!)
  3. Model Accuracy:

    • Old: Can achieve high accuracy by just looking at past
    • New: Must actually predict future to be accurate

Testing

Before Training:

  • Trend predictions will be random/wrong
  • Yellow line will point in wrong direction

After 5-10 Epochs:

  • Trend predictions should match actual future movement
  • Yellow line should point correctly more often
  • Trend accuracy should reach 40-60%

Check Console Logs:

Trend loss: 0.0234 (pred=[0.12, 0.45, 1.0], target=[0.15, 0.52, 1.0])
trend_accuracy: 78.3% (angle: 85.2%, steepness: 71.4%)

Files Modified

  • /ANNOTATE/core/real_training_adapter.py (lines 1618-1682)
    • Changed from historical (last 10 candles) to forward-looking (next candle)
    • Added fallback to historical if future data not available

This complements the earlier fixes:

  1. Removed synthetic fixed-angle trend data (±45°)
  2. Now uses real future price movement
  3. Trend line projection adjusted for better visualization

Status

IMPLEMENTED - Trend targets now forward-looking PENDING - Needs testing with fresh training 📊 EXPECTED - Better trend predictions after training