chore: add tests for set_initial_return_values

This commit is contained in:
robcaulk
2023-09-14 00:05:59 +02:00
parent 628963c207
commit 844ab4aef5
3 changed files with 126 additions and 6 deletions
+3 -3
View File
@@ -294,9 +294,9 @@ class FreqaiDataDrawer:
if len(common_dates.index) > 0: if len(common_dates.index) > 0:
new_pred = new_pred.iloc[len(common_dates):] new_pred = new_pred.iloc[len(common_dates):]
else: else:
logger.error("No common dates found between new predictions and historic predictions. " logger.warning("No common dates found between new predictions and historic "
"You likely left your FreqAI instance offline for more than " "predictions. You likely left your FreqAI instance offline "
f"{len(dataframe.index)} candles.") f"for more than {len(dataframe.index)} candles.")
df_concat = pd.concat([hist_preds, new_pred], ignore_index=True, keys=hist_preds.keys()) df_concat = pd.concat([hist_preds, new_pred], ignore_index=True, keys=hist_preds.keys())
+3 -3
View File
@@ -645,11 +645,11 @@ class IFreqaiModel(ABC):
If the user reuses an identifier on a subsequent instance, If the user reuses an identifier on a subsequent instance,
this function will not be called. In that case, "real" predictions this function will not be called. In that case, "real" predictions
will be appended to the loaded set of historic predictions. will be appended to the loaded set of historic predictions.
:param df: DataFrame = the dataframe containing the training feature data :param pred_df: DataFrame = the dataframe containing the predictions coming
:param model: Any = A model which was `fit` using a common library such as out of a model
catboost or lightgbm
:param dk: FreqaiDataKitchen = object containing methods for data analysis :param dk: FreqaiDataKitchen = object containing methods for data analysis
:param pair: str = current pair :param pair: str = current pair
:param strat_df: DataFrame = dataframe coming from strategy
""" """
self.dd.historic_predictions[pair] = pred_df self.dd.historic_predictions[pair] = pred_df
+120
View File
@@ -1,7 +1,9 @@
import shutil import shutil
from pathlib import Path from pathlib import Path
from unittest.mock import patch
import pandas as pd
import pytest import pytest
from freqtrade.configuration import TimeRange from freqtrade.configuration import TimeRange
@@ -135,3 +137,121 @@ def test_get_timerange_from_backtesting_live_df_pred_not_found(mocker, freqai_co
match=r'Historic predictions not found.*' match=r'Historic predictions not found.*'
): ):
freqai.dd.get_timerange_from_live_historic_predictions() freqai.dd.get_timerange_from_live_historic_predictions()
class MockClass: # This represents your class that has the set_initial_return_values method.
def __init__(self):
self.historic_predictions = {}
self.model_return_values = {}
# ... set_initial_return_values function here ...
def test_set_initial_return_values(mocker, freqai_conf):
"""
Simple test of the set initial return values that ensures
we are concatening and ffilling values properly.
"""
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
exchange = get_patched_exchange(mocker, freqai_conf)
strategy.dp = DataProvider(freqai_conf, exchange)
freqai = strategy.freqai
freqai.live = False
freqai.dk = FreqaiDataKitchen(freqai_conf)
# Setup
pair = "BTC/USD"
end_x = "2023-08-31"
start_x_plus_1 = "2023-08-30"
end_x_plus_5 = "2023-09-03"
historic_data = {
'date_pred': pd.date_range(end=end_x, periods=5),
'value': range(1, 6)
}
new_data = {
'date': pd.date_range(start=start_x_plus_1, end=end_x_plus_5),
'value': range(6, 11)
}
freqai.dd.historic_predictions[pair] = pd.DataFrame(historic_data)
new_pred_df = pd.DataFrame(new_data)
dataframe = pd.DataFrame(new_data)
# Action
with patch('logging.Logger.warning') as mock_logger_warning:
freqai.dd.set_initial_return_values(pair, new_pred_df, dataframe)
# Assertions
hist_pred_df = freqai.dd.historic_predictions[pair]
model_return_df = freqai.dd.model_return_values[pair]
assert (hist_pred_df['date_pred'].iloc[-1] ==
pd.Timestamp(end_x_plus_5) - pd.Timedelta(days=1))
assert 'date' not in hist_pred_df.columns
assert 'date_pred' in hist_pred_df.columns
assert hist_pred_df.shape[0] == 7 # Total rows: 5 from historic and 2 new zeros
# compare values in model_return_df with hist_pred_df
assert (model_return_df["value"].values ==
hist_pred_df.tail(len(dataframe))["value"].values).all()
assert model_return_df.shape[0] == len(dataframe)
# Ensure logger error is not called
mock_logger_warning.assert_not_called()
def test_set_initial_return_values_warning(mocker, freqai_conf):
"""
Simple test of set_initial_return_values that hits the warning
associated with leaving a FreqAI bot offline so long that the
exchange candles have no common date with the historic predictions
"""
strategy = get_patched_freqai_strategy(mocker, freqai_conf)
exchange = get_patched_exchange(mocker, freqai_conf)
strategy.dp = DataProvider(freqai_conf, exchange)
freqai = strategy.freqai
freqai.live = False
freqai.dk = FreqaiDataKitchen(freqai_conf)
# Setup
pair = "BTC/USD"
end_x = "2023-08-31"
start_x_plus_1 = "2023-09-01"
end_x_plus_5 = "2023-09-05"
historic_data = {
'date_pred': pd.date_range(end=end_x, periods=5),
'value': range(1, 6)
}
new_data = {
'date': pd.date_range(start=start_x_plus_1, end=end_x_plus_5),
'value': range(6, 11)
}
freqai.dd.historic_predictions[pair] = pd.DataFrame(historic_data)
new_pred_df = pd.DataFrame(new_data)
dataframe = pd.DataFrame(new_data)
# Action
with patch('logging.Logger.warning') as mock_logger_warning:
freqai.dd.set_initial_return_values(pair, new_pred_df, dataframe)
# Assertions
hist_pred_df = freqai.dd.historic_predictions[pair]
model_return_df = freqai.dd.model_return_values[pair]
assert hist_pred_df['date_pred'].iloc[-1] == pd.Timestamp(end_x_plus_5) - pd.Timedelta(days=1)
assert 'date' not in hist_pred_df.columns
assert 'date_pred' in hist_pred_df.columns
assert hist_pred_df.shape[0] == 9 # Total rows: 5 from historic and 4 new zeros
# compare values in model_return_df with hist_pred_df
assert (model_return_df["value"].values == hist_pred_df.tail(
len(dataframe))["value"].values).all()
assert model_return_df.shape[0] == len(dataframe)
# Ensure logger error is not called
mock_logger_warning.assert_called()