2023-03-20 15:06:33 +00:00
|
|
|
import logging
|
2023-05-29 11:33:29 +00:00
|
|
|
from time import time
|
|
|
|
from typing import Any, Tuple
|
2023-03-20 15:06:33 +00:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import numpy.typing as npt
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
|
|
|
from freqtrade.freqai.base_models.BasePyTorchModel import BasePyTorchModel
|
|
|
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-03-22 15:50:00 +00:00
|
|
|
class BasePyTorchRegressor(BasePyTorchModel):
|
2023-03-20 15:06:33 +00:00
|
|
|
"""
|
|
|
|
A PyTorch implementation of a regressor.
|
|
|
|
User must implement fit method
|
|
|
|
"""
|
2023-06-07 16:26:49 +00:00
|
|
|
|
2023-03-20 15:06:33 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
def predict(
|
|
|
|
self, unfiltered_df: DataFrame, dk: FreqaiDataKitchen, **kwargs
|
|
|
|
) -> Tuple[DataFrame, npt.NDArray[np.int_]]:
|
|
|
|
"""
|
|
|
|
Filter the prediction features data and predict with it.
|
|
|
|
:param unfiltered_df: Full dataframe for the current backtest period.
|
|
|
|
: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)
|
|
|
|
"""
|
|
|
|
|
|
|
|
dk.find_features(unfiltered_df)
|
|
|
|
filtered_df, _ = dk.filter_features(
|
|
|
|
unfiltered_df, dk.training_features_list, training_filter=False
|
|
|
|
)
|
|
|
|
dk.data_dictionary["prediction_features"] = filtered_df
|
|
|
|
|
2023-05-29 11:33:29 +00:00
|
|
|
dk.data_dictionary["prediction_features"], outliers, _ = dk.feature_pipeline.transform(
|
|
|
|
dk.data_dictionary["prediction_features"], outlier_check=True)
|
|
|
|
|
2023-04-03 12:19:10 +00:00
|
|
|
x = self.data_convertor.convert_x(
|
|
|
|
dk.data_dictionary["prediction_features"],
|
|
|
|
device=self.device
|
|
|
|
)
|
2023-05-01 13:18:03 +00:00
|
|
|
self.model.model.eval()
|
2023-03-20 15:06:33 +00:00
|
|
|
y = self.model.model(x)
|
2023-05-05 13:43:48 +00:00
|
|
|
pred_df = DataFrame(y.detach().tolist(), columns=[dk.label_list[0]])
|
2023-05-29 11:33:29 +00:00
|
|
|
pred_df, _, _ = dk.label_pipeline.inverse_transform(pred_df)
|
|
|
|
|
2023-06-17 11:21:31 +00:00
|
|
|
if dk.feature_pipeline["di"]:
|
2023-05-29 11:33:29 +00:00
|
|
|
dk.DI_values = dk.feature_pipeline["di"].di_values
|
|
|
|
else:
|
2023-06-25 14:34:44 +00:00
|
|
|
dk.DI_values = np.zeros(outliers.shape[0])
|
2023-06-25 13:43:02 +00:00
|
|
|
dk.do_predict = outliers
|
2023-03-20 17:28:30 +00:00
|
|
|
return (pred_df, dk.do_predict)
|
2023-05-29 11:33:29 +00:00
|
|
|
|
|
|
|
def train(
|
|
|
|
self, unfiltered_df: DataFrame, pair: str, dk: FreqaiDataKitchen, **kwargs
|
|
|
|
) -> Any:
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
:param unfiltered_df: Full dataframe for the current training period
|
|
|
|
:return:
|
|
|
|
:model: Trained model which can be used to inference (self.predict)
|
|
|
|
"""
|
|
|
|
|
|
|
|
logger.info(f"-------------------- Starting training {pair} --------------------")
|
|
|
|
|
|
|
|
start_time = time()
|
|
|
|
|
|
|
|
features_filtered, labels_filtered = dk.filter_features(
|
|
|
|
unfiltered_df,
|
|
|
|
dk.training_features_list,
|
|
|
|
dk.label_list,
|
|
|
|
training_filter=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# split data into train/test data.
|
2023-06-07 16:26:49 +00:00
|
|
|
dd = dk.make_train_test_datasets(features_filtered, labels_filtered)
|
|
|
|
if not self.freqai_info.get("fit_live_predictions_candles", 0) or not self.live:
|
2023-05-29 11:33:29 +00:00
|
|
|
dk.fit_labels()
|
2023-06-08 10:33:08 +00:00
|
|
|
dk.feature_pipeline = self.define_data_pipeline(threads=dk.thread_count)
|
|
|
|
dk.label_pipeline = self.define_label_pipeline(threads=dk.thread_count)
|
2023-05-29 11:33:29 +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"])
|
2023-05-29 11:33:29 +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-06-17 13:39:33 +00:00
|
|
|
dd["train_labels"], _, _ = dk.label_pipeline.fit_transform(dd["train_labels"])
|
2023-05-29 11:33:29 +00:00
|
|
|
|
2023-06-17 13:39:33 +00:00
|
|
|
if self.freqai_info.get('data_split_parameters', {}).get('test_size', 0.1) != 0:
|
|
|
|
(dd["test_features"],
|
|
|
|
dd["test_labels"],
|
|
|
|
dd["test_weights"]) = dk.feature_pipeline.transform(dd["test_features"],
|
|
|
|
dd["test_labels"],
|
|
|
|
dd["test_weights"])
|
|
|
|
dd["test_labels"], _, _ = dk.label_pipeline.transform(dd["test_labels"])
|
2023-05-29 11:33:29 +00:00
|
|
|
|
|
|
|
logger.info(
|
|
|
|
f"Training model on {len(dk.data_dictionary['train_features'].columns)} features"
|
|
|
|
)
|
2023-06-07 16:26:49 +00:00
|
|
|
logger.info(f"Training model on {len(dd['train_features'])} data points")
|
2023-05-29 11:33:29 +00:00
|
|
|
|
2023-06-07 16:26:49 +00:00
|
|
|
model = self.fit(dd, dk)
|
2023-05-29 11:33:29 +00:00
|
|
|
end_time = time()
|
|
|
|
|
|
|
|
logger.info(f"-------------------- Done training {pair} "
|
|
|
|
f"({end_time - start_time:.2f} secs) --------------------")
|
|
|
|
|
|
|
|
return model
|