use experimental features; runtime fix

This commit is contained in:
Dobromir Popov
2025-11-22 23:17:10 +02:00
parent f38a924b0f
commit 3eb74381a8
6 changed files with 263 additions and 38 deletions

View File

@@ -1615,41 +1615,68 @@ class RealTrainingAdapter:
# FIXED: Ensure shape is [1, 1] not [1] to match BCELoss requirements
trade_success = torch.tensor([[1.0 if profit_loss_pct > 0 else 0.0]], dtype=torch.float32) # [1, 1]
# REAL TREND CALCULATION from actual price data (NO MORE SYNTHETIC DATA!)
# Use last 10 candles to calculate actual trend angle, steepness, direction
# REAL TREND CALCULATION from historical + FUTURE price movement
# Calculate trend target from current price to future predicted candles
# This tells the model what the ACTUAL trend will be, not what it was
# Get price data from the batch to calculate actual trend
import math
# Get current price (last close from historical data)
price_data = price_data_1m if price_data_1m is not None else (
price_data_1s if price_data_1s is not None else price_data_1h)
if price_data is not None and price_data.shape[1] >= 10:
# price_data shape: [batch=1, seq_len=200, features=5] -> OHLCV
recent_closes = price_data[0, -10:, 3] # Last 10 close prices [10]
# Calculate actual price change and time delta
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
current_price = None
if price_data is not None and price_data.shape[1] > 0:
current_price = price_data[0, -1, 3].item() # Last close price
# Try to get future price from next candle predictions
# This represents the ACTUAL trend that will happen (ground truth)
future_price = None
timeframe_for_trend = None
# Check all available timeframes for next candle data
if timeframes and '1s' in timeframes and '1s' in norm_params_dict:
future_candle = self._extract_next_candle(timeframes['1s'], norm_params_dict['1s'])
if future_candle is not None:
future_price = future_candle[0, 3].item() # Close price from first row
timeframe_for_trend = '1s'
if future_price is None and timeframes and '1m' in timeframes and '1m' in norm_params_dict:
future_candle = self._extract_next_candle(timeframes['1m'], norm_params_dict['1m'])
if future_candle is not None:
future_price = future_candle[0, 3].item() # Close price from first row
timeframe_for_trend = '1m'
# Calculate trend from current to future (what will actually happen)
if current_price and future_price and current_price > 0:
price_delta = future_price - current_price
time_delta = 1.0 # 1 candle ahead
# Calculate real angle using atan2
import math
trend_angle = math.atan2(price_delta, time_delta * price_start / 100.0) # Normalize by price scale
trend_angle = math.atan2(price_delta, time_delta * current_price / 100.0)
# Calculate real steepness (magnitude of change)
if price_start > 0:
price_change_pct = abs(price_delta / price_start)
trend_steepness = min(price_change_pct * 100.0, 1.0) # Scale and cap at 1.0
else:
trend_steepness = 0.0
price_change_pct = abs(price_delta / current_price)
trend_steepness = min(price_change_pct * 100.0, 1.0) # Scale and cap at 1.0
# Calculate real direction
trend_direction = 1.0 if price_delta > 0 else (-1.0 if price_delta < 0 else 0.0)
else:
# Fallback if no price data available (should rarely happen)
trend_angle = 0.0
trend_steepness = 0.0
trend_direction = 0.0
# Fallback: use recent historical trend if future data not available
if price_data is not None and price_data.shape[1] >= 5:
recent_closes = price_data[0, -5:, 3] # Last 5 closes
price_start = recent_closes[0].item()
price_end = recent_closes[-1].item()
price_delta = price_end - price_start
if price_start > 0:
trend_angle = math.atan2(price_delta, 4.0 * price_start / 100.0)
trend_steepness = min(abs(price_delta / price_start) * 100.0, 1.0)
trend_direction = 1.0 if price_delta > 0 else (-1.0 if price_delta < 0 else 0.0)
else:
trend_angle, trend_steepness, trend_direction = 0.0, 0.0, 0.0
else:
trend_angle, trend_steepness, trend_direction = 0.0, 0.0, 0.0
# Create trend target tensor [batch, 3]: [angle, steepness, direction]
trend_target = torch.tensor([[trend_angle, trend_steepness, trend_direction]], dtype=torch.float32) # [1, 3]