2023-03-20 15:06:33 +00:00
|
|
|
import logging
|
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
)
|
|
|
|
filtered_df = dk.normalize_data_from_metadata(filtered_df)
|
|
|
|
dk.data_dictionary["prediction_features"] = filtered_df
|
|
|
|
|
|
|
|
self.data_cleaning_predict(dk)
|
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-04-28 19:48:16 +00:00
|
|
|
pred_df = dk.denormalize_labels_from_metadata(pred_df)
|
2023-03-20 17:28:30 +00:00
|
|
|
return (pred_df, dk.do_predict)
|