buy/sell signals training - wip

This commit is contained in:
Dobromir Popov
2025-03-29 04:28:12 +02:00
parent 8b3db10a85
commit 1b9f471076
2 changed files with 172 additions and 39 deletions

View File

@ -395,10 +395,19 @@ class DataInterface:
win_rate is the ratio of winning trades
trades is a list of trade dictionaries
"""
if len(predictions) != len(actual_prices) - 1:
logger.error("Predictions and prices length mismatch")
# Ensure we have enough prices for the predictions
if len(actual_prices) <= 1:
logger.error("Not enough price data for PnL calculation")
return 0.0, 0.0, []
# Adjust predictions length to match available price data
n_prices = len(actual_prices) - 1 # We need current and next price for each prediction
if len(predictions) > n_prices:
predictions = predictions[:n_prices]
elif len(predictions) < n_prices:
n_prices = len(predictions)
actual_prices = actual_prices[:n_prices + 1] # +1 to include the next price
pnl = 0.0
trades = 0
wins = 0
@ -422,7 +431,7 @@ class DataInterface:
'type': 'buy',
'price': current_price,
'pnl': trade_pnl,
'timestamp': self.dataframes[self.timeframes[0]]['timestamp'].iloc[i]
'timestamp': self.dataframes[self.timeframes[0]]['timestamp'].iloc[i] if self.dataframes[self.timeframes[0]] is not None else None
})
elif pred == 0: # Sell
trade_pnl = -price_change * position_size
@ -433,7 +442,7 @@ class DataInterface:
'type': 'sell',
'price': current_price,
'pnl': trade_pnl,
'timestamp': self.dataframes[self.timeframes[0]]['timestamp'].iloc[i]
'timestamp': self.dataframes[self.timeframes[0]]['timestamp'].iloc[i] if self.dataframes[self.timeframes[0]] is not None else None
})
pnl += trade_pnl if pred in [0, 2] else 0
@ -443,30 +452,30 @@ class DataInterface:
def get_future_prices(self, prices, n_candles=3):
"""
Extract future prices for use in retrospective training.
Extract future prices for retrospective training.
Args:
prices: Array of close prices
n_candles: Number of future candles to predict
prices (np.ndarray): Array of prices
n_candles (int): Number of future candles to look at
Returns:
numpy.ndarray: Array of future prices for each sample
np.ndarray: Future prices array
"""
if prices is None or len(prices) <= n_candles:
logger.warning(f"Not enough price data for future prediction: {len(prices) if prices is not None else 0} prices")
# Return zeros if not enough data
return np.zeros((len(prices) if prices is not None else 0, 1))
if len(prices) < n_candles + 1:
return None
# For each price point i, get the price at i+n_candles
future_prices = np.zeros((len(prices), 1))
# For each price point, get the maximum price in the next n_candles
future_prices = np.zeros(len(prices))
for i in range(len(prices) - n_candles):
future_prices[i, 0] = prices[i + n_candles]
# For the last n_candles positions, we don't have future data
# We'll use the last known price as a placeholder
for i in range(len(prices) - n_candles, len(prices)):
future_prices[i, 0] = prices[-1]
# Get the next n candles
next_candles = prices[i+1:i+n_candles+1]
# Use the maximum price as the future price
future_prices[i] = np.max(next_candles)
# For the last n_candles points, use the last available price
future_prices[-n_candles:] = prices[-1]
return future_prices
def prepare_training_data(self, refresh=False, refresh_interval=300):