chore: fix bug associated with leaving FreqAI offline for more than 1 candle.

This commit is contained in:
robcaulk 2023-09-12 12:19:12 +02:00
parent a52cf42218
commit 628963c207
3 changed files with 43 additions and 11 deletions

View File

@ -263,23 +263,55 @@ class FreqaiDataDrawer:
self.pair_dict[metadata["pair"]] = self.empty_pair_dict.copy() self.pair_dict[metadata["pair"]] = self.empty_pair_dict.copy()
return return
def set_initial_return_values(self, pair: str, pred_df: DataFrame) -> None: def set_initial_return_values(self, pair: str,
pred_df: DataFrame,
dataframe: DataFrame
) -> None:
""" """
Set the initial return values to the historical predictions dataframe. This avoids needing Set the initial return values to the historical predictions dataframe. This avoids needing
to repredict on historical candles, and also stores historical predictions despite to repredict on historical candles, and also stores historical predictions despite
retrainings (so stored predictions are true predictions, not just inferencing on trained retrainings (so stored predictions are true predictions, not just inferencing on trained
data) data).
We also aim to keep the date from historical predictions so that the FreqUI displays
zeros during any downtime (between FreqAI reloads).
""" """
hist_df = self.historic_predictions new_pred = pred_df.copy()
len_diff = len(hist_df[pair].index) - len(pred_df.index) # set new_pred values to nans (we want to signal to user that there was nothing
if len_diff < 0: # historically made during downtime. The newest pred will get appeneded later in
df_concat = pd.concat([pred_df.iloc[:abs(len_diff)], hist_df[pair]], # append_model_predictions)
ignore_index=True, keys=hist_df[pair].keys()) new_pred.iloc[:, :] = np.nan
new_pred["date"] = dataframe["date"]
hist_preds = self.historic_predictions[pair].copy()
# rename date_pred column to date so that we can merge on date
hist_preds = hist_preds.rename(columns={"date_pred": "date"})
# find the closest common date between new_pred and historic predictions
# and cut off the new_pred dataframe at that date
common_dates = pd.merge(new_pred, hist_preds, on="date", how="inner")
if len(common_dates.index) > 0:
new_pred = new_pred.iloc[len(common_dates):]
else: else:
df_concat = hist_df[pair].tail(len(pred_df.index)).reset_index(drop=True) logger.error("No common dates found between new predictions and historic predictions. "
"You likely left your FreqAI instance offline for more than "
f"{len(dataframe.index)} candles.")
df_concat = pd.concat([hist_preds, new_pred], ignore_index=True, keys=hist_preds.keys())
# remove last row because we will append that later in append_model_predictions()
df_concat = df_concat.iloc[:-1]
# any missing values will get zeroed out so users can see the exact
# downtime in FreqUI
df_concat = df_concat.fillna(0) df_concat = df_concat.fillna(0)
self.model_return_values[pair] = df_concat
# rename date column back to date_pred
df_concat = df_concat.rename(columns={"date": "date_pred"})
self.historic_predictions[pair] = df_concat
self.model_return_values[pair] = df_concat.tail(len(dataframe.index)).reset_index(drop=True)
def append_model_predictions(self, pair: str, predictions: DataFrame, def append_model_predictions(self, pair: str, predictions: DataFrame,
do_preds: NDArray[np.int_], do_preds: NDArray[np.int_],

View File

@ -453,7 +453,7 @@ class IFreqaiModel(ABC):
pred_df, do_preds = self.predict(dataframe, dk) pred_df, do_preds = self.predict(dataframe, dk)
if pair not in self.dd.historic_predictions: if pair not in self.dd.historic_predictions:
self.set_initial_historic_predictions(pred_df, dk, pair, dataframe) self.set_initial_historic_predictions(pred_df, dk, pair, dataframe)
self.dd.set_initial_return_values(pair, pred_df) self.dd.set_initial_return_values(pair, pred_df, dataframe)
dk.return_dataframe = self.dd.attach_return_values_to_return_dataframe(pair, dataframe) dk.return_dataframe = self.dd.attach_return_values_to_return_dataframe(pair, dataframe)
return return

View File

@ -31,7 +31,7 @@ class FreqaiExampleStrategy(IStrategy):
plot_config = { plot_config = {
"main_plot": {}, "main_plot": {},
"subplots": { "subplots": {
"&-s_close": {"prediction": {"color": "blue"}}, "&-s_close": {"&-s_close": {"color": "blue"}},
"do_predict": { "do_predict": {
"do_predict": {"color": "brown"}, "do_predict": {"color": "brown"},
}, },