2022-07-11 09:33:59 +00:00
|
|
|
import logging
|
2022-09-23 08:18:34 +00:00
|
|
|
from time import time
|
2022-07-25 09:46:59 +00:00
|
|
|
from typing import Any, Tuple
|
2022-07-11 09:33:59 +00:00
|
|
|
|
2022-07-29 06:23:44 +00:00
|
|
|
import numpy as np
|
2022-07-25 09:46:59 +00:00
|
|
|
import numpy.typing as npt
|
2022-07-11 09:33:59 +00:00
|
|
|
from pandas import DataFrame
|
2022-07-29 06:23:44 +00:00
|
|
|
|
2022-07-11 09:33:59 +00:00
|
|
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
|
|
|
from freqtrade.freqai.freqai_interface import IFreqaiModel
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class BaseRegressionModel(IFreqaiModel):
|
|
|
|
"""
|
2022-07-12 16:09:17 +00:00
|
|
|
Base class for regression type models (e.g. Catboost, LightGBM, XGboost etc.).
|
|
|
|
User *must* inherit from this class and set fit() and predict(). See example scripts
|
|
|
|
such as prediction_models/CatboostPredictionModel.py for guidance.
|
2022-07-11 09:33:59 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def train(
|
2022-09-09 17:17:15 +00:00
|
|
|
self, unfiltered_df: DataFrame, pair: str, dk: FreqaiDataKitchen, **kwargs
|
2022-07-25 09:46:59 +00:00
|
|
|
) -> Any:
|
2022-07-11 09:33:59 +00:00
|
|
|
"""
|
|
|
|
Filter the training data and train a model to it. Train makes heavy use of the datakitchen
|
|
|
|
for storing, saving, loading, and analyzing the data.
|
2022-09-09 17:17:15 +00:00
|
|
|
:param unfiltered_df: Full dataframe for the current training period
|
2022-07-24 14:54:39 +00:00
|
|
|
:param metadata: pair metadata from strategy.
|
|
|
|
:return:
|
2022-07-11 09:33:59 +00:00
|
|
|
:model: Trained model which can be used to inference (self.predict)
|
|
|
|
"""
|
|
|
|
|
2022-09-23 08:18:34 +00:00
|
|
|
logger.info(f"-------------------- Starting training {pair} --------------------")
|
|
|
|
|
|
|
|
start_time = time()
|
2022-07-11 09:33:59 +00:00
|
|
|
|
|
|
|
# filter the features requested by user in the configuration file and elegantly handle NaNs
|
|
|
|
features_filtered, labels_filtered = dk.filter_features(
|
2022-09-09 17:17:15 +00:00
|
|
|
unfiltered_df,
|
2022-07-11 09:33:59 +00:00
|
|
|
dk.training_features_list,
|
|
|
|
dk.label_list,
|
|
|
|
training_filter=True,
|
|
|
|
)
|
|
|
|
|
2022-09-09 17:17:15 +00:00
|
|
|
start_date = unfiltered_df["date"].iloc[0].strftime("%Y-%m-%d")
|
|
|
|
end_date = unfiltered_df["date"].iloc[-1].strftime("%Y-%m-%d")
|
2022-07-19 15:49:18 +00:00
|
|
|
logger.info(f"-------------------- Training on data from {start_date} to "
|
2022-09-23 08:18:34 +00:00
|
|
|
f"{end_date} --------------------")
|
2022-07-11 09:33:59 +00:00
|
|
|
# split data into train/test data.
|
2023-06-07 16:26:49 +00:00
|
|
|
dd = dk.make_train_test_datasets(features_filtered, labels_filtered)
|
2022-10-20 15:15:41 +00:00
|
|
|
if not self.freqai_info.get("fit_live_predictions_candles", 0) or not self.live:
|
2022-07-29 15:27:35 +00:00
|
|
|
dk.fit_labels()
|
2023-06-07 16:26:49 +00:00
|
|
|
dk.feature_pipeline = self.define_data_pipeline()
|
|
|
|
dk.label_pipeline = self.define_label_pipeline()
|
2022-07-11 09:33:59 +00:00
|
|
|
|
2023-06-07 16:26:49 +00:00
|
|
|
(dd["train_features"],
|
|
|
|
dd["train_labels"],
|
|
|
|
dd["train_weights"]) = dk.feature_pipeline.fit_transform(dd["train_features"],
|
|
|
|
dd["train_labels"],
|
|
|
|
dd["train_weights"])
|
2023-05-26 16:40:14 +00:00
|
|
|
|
2023-06-07 16:26:49 +00:00
|
|
|
(dd["test_features"],
|
|
|
|
dd["test_labels"],
|
|
|
|
dd["test_weights"]) = dk.feature_pipeline.transform(dd["test_features"],
|
|
|
|
dd["test_labels"],
|
|
|
|
dd["test_weights"])
|
2023-05-26 16:40:14 +00:00
|
|
|
|
2023-06-07 16:26:49 +00:00
|
|
|
dd["train_labels"], _, _ = dk.label_pipeline.fit_transform(dd["train_labels"])
|
|
|
|
dd["test_labels"], _, _ = dk.label_pipeline.transform(dd["test_labels"])
|
2022-07-11 09:33:59 +00:00
|
|
|
|
|
|
|
logger.info(
|
2022-09-23 08:18:34 +00:00
|
|
|
f"Training model on {len(dk.data_dictionary['train_features'].columns)} features"
|
2022-07-11 09:33:59 +00:00
|
|
|
)
|
2023-06-07 16:26:49 +00:00
|
|
|
logger.info(f"Training model on {len(dd['train_features'])} data points")
|
2022-07-11 09:33:59 +00:00
|
|
|
|
2023-06-07 16:26:49 +00:00
|
|
|
model = self.fit(dd, dk)
|
2022-07-11 09:33:59 +00:00
|
|
|
|
2022-09-23 08:18:34 +00:00
|
|
|
end_time = time()
|
|
|
|
|
|
|
|
logger.info(f"-------------------- Done training {pair} "
|
|
|
|
f"({end_time - start_time:.2f} secs) --------------------")
|
2022-07-11 09:33:59 +00:00
|
|
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
def predict(
|
2022-09-09 17:17:15 +00:00
|
|
|
self, unfiltered_df: DataFrame, dk: FreqaiDataKitchen, **kwargs
|
2022-07-29 06:12:50 +00:00
|
|
|
) -> Tuple[DataFrame, npt.NDArray[np.int_]]:
|
2022-07-11 09:33:59 +00:00
|
|
|
"""
|
|
|
|
Filter the prediction features data and predict with it.
|
2022-10-10 12:15:30 +00:00
|
|
|
:param unfiltered_df: Full dataframe for the current backtest period.
|
2022-07-11 09:33:59 +00:00
|
|
|
:return:
|
|
|
|
:pred_df: dataframe containing the predictions
|
|
|
|
:do_predict: np.array of 1s and 0s to indicate places where freqai needed to remove
|
|
|
|
data (NaNs) or felt uncertain about data (PCA and DI index)
|
|
|
|
"""
|
|
|
|
|
2022-09-09 17:17:15 +00:00
|
|
|
dk.find_features(unfiltered_df)
|
2023-05-29 11:33:29 +00:00
|
|
|
dk.data_dictionary["prediction_features"], _ = dk.filter_features(
|
2022-09-09 17:17:15 +00:00
|
|
|
unfiltered_df, dk.training_features_list, training_filter=False
|
2022-07-11 09:33:59 +00:00
|
|
|
)
|
|
|
|
|
2023-05-29 11:33:29 +00:00
|
|
|
dk.data_dictionary["prediction_features"], outliers, _ = dk.feature_pipeline.transform(
|
2023-05-26 16:40:14 +00:00
|
|
|
dk.data_dictionary["prediction_features"], outlier_check=True)
|
2022-07-11 09:33:59 +00:00
|
|
|
|
|
|
|
predictions = self.model.predict(dk.data_dictionary["prediction_features"])
|
2022-12-11 11:15:19 +00:00
|
|
|
if self.CONV_WIDTH == 1:
|
|
|
|
predictions = np.reshape(predictions, (-1, len(dk.label_list)))
|
|
|
|
|
2022-07-11 09:33:59 +00:00
|
|
|
pred_df = DataFrame(predictions, columns=dk.label_list)
|
|
|
|
|
2023-05-26 16:40:14 +00:00
|
|
|
pred_df, _, _ = dk.label_pipeline.inverse_transform(pred_df)
|
2023-05-29 11:33:29 +00:00
|
|
|
if self.freqai_info.get("DI_threshold", 0) > 0:
|
|
|
|
dk.DI_values = dk.feature_pipeline["di"].di_values
|
|
|
|
else:
|
|
|
|
dk.DI_values = np.zeros(len(outliers.index))
|
2023-05-26 16:40:14 +00:00
|
|
|
dk.do_predict = outliers.to_numpy()
|
2022-07-11 09:33:59 +00:00
|
|
|
|
|
|
|
return (pred_df, dk.do_predict)
|