2022-08-15 08:26:44 +00:00
|
|
|
import logging
|
|
|
|
from typing import Any, Dict, Tuple
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import numpy.typing as npt
|
|
|
|
import pandas as pd
|
|
|
|
from pandas import DataFrame
|
|
|
|
from abc import abstractmethod
|
2022-08-21 17:58:36 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2022-08-15 08:26:44 +00:00
|
|
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
|
|
|
from freqtrade.freqai.freqai_interface import IFreqaiModel
|
2022-08-18 14:07:19 +00:00
|
|
|
from freqtrade.freqai.RL.Base5ActionRLEnv import Base5ActionRLEnv, Actions, Positions
|
2022-08-15 08:26:44 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2022-08-17 10:51:14 +00:00
|
|
|
import torch.multiprocessing
|
2022-08-20 14:35:29 +00:00
|
|
|
from stable_baselines3.common.callbacks import EvalCallback
|
2022-08-18 14:07:19 +00:00
|
|
|
from stable_baselines3.common.monitor import Monitor
|
2022-08-17 10:51:14 +00:00
|
|
|
import torch as th
|
2022-08-20 14:35:29 +00:00
|
|
|
from typing import Callable
|
2022-08-23 07:44:44 +00:00
|
|
|
from datetime import datetime, timezone
|
2022-08-20 14:35:29 +00:00
|
|
|
from stable_baselines3.common.utils import set_random_seed
|
|
|
|
import gym
|
2022-08-15 08:26:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-08-18 11:02:47 +00:00
|
|
|
|
2022-08-17 10:51:14 +00:00
|
|
|
torch.multiprocessing.set_sharing_strategy('file_system')
|
2022-08-15 08:26:44 +00:00
|
|
|
|
2022-08-21 17:58:36 +00:00
|
|
|
SB3_MODELS = ['PPO', 'A2C', 'DQN', 'TD3', 'SAC']
|
|
|
|
SB3_CONTRIB_MODELS = ['TRPO', 'ARS']
|
|
|
|
|
2022-08-15 08:26:44 +00:00
|
|
|
|
|
|
|
class BaseReinforcementLearningModel(IFreqaiModel):
|
|
|
|
"""
|
|
|
|
User created Reinforcement Learning Model prediction model.
|
|
|
|
"""
|
|
|
|
|
2022-08-18 11:02:47 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(config=kwargs['config'])
|
2022-08-20 14:35:29 +00:00
|
|
|
th.set_num_threads(self.freqai_info['rl_config'].get('thread_count', 4))
|
2022-08-18 11:02:47 +00:00
|
|
|
self.reward_params = self.freqai_info['rl_config']['model_reward_parameters']
|
2022-08-18 14:07:19 +00:00
|
|
|
self.train_env: Base5ActionRLEnv = None
|
2022-08-20 14:35:29 +00:00
|
|
|
self.eval_env: Base5ActionRLEnv = None
|
|
|
|
self.eval_callback: EvalCallback = None
|
2022-08-21 17:58:36 +00:00
|
|
|
self.model_type = self.freqai_info['rl_config']['model_type']
|
2022-08-24 10:54:02 +00:00
|
|
|
self.rl_config = self.freqai_info['rl_config']
|
2022-08-24 14:32:14 +00:00
|
|
|
self.continual_retraining = self.rl_config.get('continual_retraining', False)
|
2022-08-21 17:58:36 +00:00
|
|
|
if self.model_type in SB3_MODELS:
|
|
|
|
import_str = 'stable_baselines3'
|
|
|
|
elif self.model_type in SB3_CONTRIB_MODELS:
|
|
|
|
import_str = 'sb3_contrib'
|
|
|
|
else:
|
|
|
|
raise OperationalException(f'{self.model_type} not available in stable_baselines3 or '
|
|
|
|
f'sb3_contrib. please choose one of {SB3_MODELS} or '
|
|
|
|
f'{SB3_CONTRIB_MODELS}')
|
|
|
|
|
|
|
|
mod = __import__(import_str, fromlist=[
|
|
|
|
self.model_type])
|
|
|
|
self.MODELCLASS = getattr(mod, self.model_type)
|
2022-08-20 14:35:29 +00:00
|
|
|
self.policy_type = self.freqai_info['rl_config']['policy_type']
|
2022-08-18 11:02:47 +00:00
|
|
|
|
2022-08-15 08:26:44 +00:00
|
|
|
def train(
|
|
|
|
self, unfiltered_dataframe: DataFrame, pair: str, dk: FreqaiDataKitchen
|
|
|
|
) -> 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_dataframe: Full dataframe for the current training period
|
|
|
|
:param metadata: pair metadata from strategy.
|
|
|
|
:returns:
|
|
|
|
:model: Trained model which can be used to inference (self.predict)
|
|
|
|
"""
|
|
|
|
|
|
|
|
logger.info("--------------------Starting training " f"{pair} --------------------")
|
|
|
|
|
|
|
|
features_filtered, labels_filtered = dk.filter_features(
|
|
|
|
unfiltered_dataframe,
|
|
|
|
dk.training_features_list,
|
|
|
|
dk.label_list,
|
|
|
|
training_filter=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
data_dictionary: Dict[str, Any] = dk.make_train_test_datasets(
|
|
|
|
features_filtered, labels_filtered)
|
2022-08-24 10:54:02 +00:00
|
|
|
dk.fit_labels() # FIXME useless for now, but just satiating append methods
|
2022-08-15 08:26:44 +00:00
|
|
|
|
|
|
|
# normalize all data based on train_dataset only
|
2022-08-17 10:51:14 +00:00
|
|
|
prices_train, prices_test = self.build_ohlc_price_dataframes(dk.data_dictionary, pair, dk)
|
2022-08-15 08:26:44 +00:00
|
|
|
data_dictionary = dk.normalize_data(data_dictionary)
|
|
|
|
|
2022-08-24 10:54:02 +00:00
|
|
|
# data cleaning/analysis
|
2022-08-15 08:26:44 +00:00
|
|
|
self.data_cleaning_train(dk)
|
|
|
|
|
|
|
|
logger.info(
|
2022-08-24 10:54:02 +00:00
|
|
|
f'Training model on {len(dk.data_dictionary["train_features"].columns)}'
|
|
|
|
f' features and {len(data_dictionary["train_features"])} data points'
|
2022-08-15 08:26:44 +00:00
|
|
|
)
|
|
|
|
|
2022-08-20 14:35:29 +00:00
|
|
|
self.set_train_and_eval_environments(data_dictionary, prices_train, prices_test, dk)
|
2022-08-18 14:07:19 +00:00
|
|
|
|
|
|
|
model = self.fit_rl(data_dictionary, dk)
|
2022-08-15 08:26:44 +00:00
|
|
|
|
|
|
|
logger.info(f"--------------------done training {pair}--------------------")
|
|
|
|
|
|
|
|
return model
|
|
|
|
|
2022-08-24 10:54:02 +00:00
|
|
|
def set_train_and_eval_environments(self, data_dictionary: Dict[str, DataFrame],
|
|
|
|
prices_train: DataFrame, prices_test: DataFrame,
|
|
|
|
dk: FreqaiDataKitchen):
|
2022-08-18 14:07:19 +00:00
|
|
|
"""
|
2022-08-24 10:54:02 +00:00
|
|
|
User can override this if they are using a custom MyRLEnv
|
2022-08-18 14:07:19 +00:00
|
|
|
"""
|
|
|
|
train_df = data_dictionary["train_features"]
|
|
|
|
test_df = data_dictionary["test_features"]
|
2022-08-20 14:35:29 +00:00
|
|
|
eval_freq = self.freqai_info["rl_config"]["eval_cycles"] * len(test_df)
|
2022-08-18 14:07:19 +00:00
|
|
|
|
2022-08-24 16:32:40 +00:00
|
|
|
self.train_env = MyRLEnv(df=train_df, prices=prices_train, window_size=self.CONV_WIDTH,
|
|
|
|
reward_kwargs=self.reward_params, config=self.config)
|
|
|
|
self.eval_env = Monitor(MyRLEnv(df=test_df, prices=prices_test,
|
|
|
|
window_size=self.CONV_WIDTH,
|
|
|
|
reward_kwargs=self.reward_params, config=self.config))
|
|
|
|
self.eval_callback = EvalCallback(self.eval_env, deterministic=True,
|
|
|
|
render=False, eval_freq=eval_freq,
|
|
|
|
best_model_save_path=str(dk.data_path))
|
2022-08-18 14:07:19 +00:00
|
|
|
|
2022-08-15 08:26:44 +00:00
|
|
|
@abstractmethod
|
2022-08-18 14:07:19 +00:00
|
|
|
def fit_rl(self, data_dictionary: Dict[str, Any], dk: FreqaiDataKitchen):
|
2022-08-15 08:26:44 +00:00
|
|
|
"""
|
|
|
|
Agent customizations and abstract Reinforcement Learning customizations
|
|
|
|
go in here. Abstract method, so this function must be overridden by
|
|
|
|
user class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2022-08-24 10:54:02 +00:00
|
|
|
def get_state_info(self, pair: str):
|
2022-08-21 15:43:40 +00:00
|
|
|
open_trades = Trade.get_trades_proxy(is_open=True)
|
2022-08-15 08:26:44 +00:00
|
|
|
market_side = 0.5
|
2022-08-24 10:54:02 +00:00
|
|
|
current_profit: float = 0
|
2022-08-23 07:44:44 +00:00
|
|
|
trade_duration = 0
|
2022-08-15 08:26:44 +00:00
|
|
|
for trade in open_trades:
|
|
|
|
if trade.pair == pair:
|
2022-08-24 10:54:02 +00:00
|
|
|
# FIXME: mypy typing doesnt like that strategy may be "None" (it never will be)
|
2022-08-23 07:44:44 +00:00
|
|
|
current_value = self.strategy.dp._exchange.get_rate(
|
|
|
|
pair, refresh=False, side="exit", is_short=trade.is_short)
|
2022-08-15 08:26:44 +00:00
|
|
|
openrate = trade.open_rate
|
2022-08-23 07:44:44 +00:00
|
|
|
now = datetime.now(timezone.utc).timestamp()
|
2022-08-24 10:54:02 +00:00
|
|
|
trade_duration = int((now - trade.open_date.timestamp()) / self.base_tf_seconds)
|
|
|
|
if 'long' in str(trade.enter_tag):
|
2022-08-15 08:26:44 +00:00
|
|
|
market_side = 1
|
2022-08-22 16:06:33 +00:00
|
|
|
current_profit = (current_value - openrate) / openrate
|
2022-08-15 08:26:44 +00:00
|
|
|
else:
|
|
|
|
market_side = 0
|
2022-08-23 07:44:44 +00:00
|
|
|
current_profit = (openrate - current_value) / openrate
|
2022-08-15 08:26:44 +00:00
|
|
|
|
2022-08-23 07:44:44 +00:00
|
|
|
# total_profit = 0
|
|
|
|
# closed_trades = Trade.get_trades_proxy(pair=pair, is_open=False)
|
|
|
|
# for trade in closed_trades:
|
|
|
|
# total_profit += trade.close_profit
|
2022-08-15 08:26:44 +00:00
|
|
|
|
2022-08-23 07:44:44 +00:00
|
|
|
return market_side, current_profit, int(trade_duration)
|
2022-08-15 08:26:44 +00:00
|
|
|
|
|
|
|
def predict(
|
|
|
|
self, unfiltered_dataframe: DataFrame, dk: FreqaiDataKitchen, first: bool = False
|
|
|
|
) -> Tuple[DataFrame, npt.NDArray[np.int_]]:
|
|
|
|
"""
|
|
|
|
Filter the prediction features data and predict with it.
|
|
|
|
:param: unfiltered_dataframe: 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_dataframe)
|
|
|
|
filtered_dataframe, _ = dk.filter_features(
|
|
|
|
unfiltered_dataframe, dk.training_features_list, training_filter=False
|
|
|
|
)
|
|
|
|
filtered_dataframe = dk.normalize_data_from_metadata(filtered_dataframe)
|
|
|
|
dk.data_dictionary["prediction_features"] = filtered_dataframe
|
|
|
|
|
|
|
|
# optional additional data cleaning/analysis
|
|
|
|
self.data_cleaning_predict(dk, filtered_dataframe)
|
|
|
|
|
2022-08-18 11:02:47 +00:00
|
|
|
pred_df = self.rl_model_predict(
|
|
|
|
dk.data_dictionary["prediction_features"], dk, self.model)
|
2022-08-15 08:26:44 +00:00
|
|
|
pred_df.fillna(0, inplace=True)
|
|
|
|
|
|
|
|
return (pred_df, dk.do_predict)
|
|
|
|
|
|
|
|
def rl_model_predict(self, dataframe: DataFrame,
|
|
|
|
dk: FreqaiDataKitchen, model: Any) -> DataFrame:
|
|
|
|
|
2022-08-18 11:02:47 +00:00
|
|
|
output = pd.DataFrame(np.zeros(len(dataframe)), columns=dk.label_list)
|
2022-08-15 08:26:44 +00:00
|
|
|
|
|
|
|
def _predict(window):
|
2022-08-23 07:44:44 +00:00
|
|
|
market_side, current_profit, trade_duration = self.get_state_info(dk.pair)
|
2022-08-15 08:26:44 +00:00
|
|
|
observations = dataframe.iloc[window.index]
|
2022-08-18 11:02:47 +00:00
|
|
|
observations['current_profit'] = current_profit
|
|
|
|
observations['position'] = market_side
|
2022-08-23 07:44:44 +00:00
|
|
|
observations['trade_duration'] = trade_duration
|
2022-08-15 08:26:44 +00:00
|
|
|
res, _ = model.predict(observations, deterministic=True)
|
|
|
|
return res
|
|
|
|
|
|
|
|
output = output.rolling(window=self.CONV_WIDTH).apply(_predict)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2022-08-17 10:51:14 +00:00
|
|
|
def build_ohlc_price_dataframes(self, data_dictionary: dict,
|
|
|
|
pair: str, dk: FreqaiDataKitchen) -> Tuple[DataFrame,
|
|
|
|
DataFrame]:
|
|
|
|
"""
|
|
|
|
Builds the train prices and test prices for the environment.
|
|
|
|
"""
|
|
|
|
|
|
|
|
coin = pair.split('/')[0]
|
|
|
|
train_df = data_dictionary["train_features"]
|
|
|
|
test_df = data_dictionary["test_features"]
|
|
|
|
|
|
|
|
# price data for model training and evaluation
|
|
|
|
tf = self.config['timeframe']
|
|
|
|
ohlc_list = [f'%-{coin}raw_open_{tf}', f'%-{coin}raw_low_{tf}',
|
|
|
|
f'%-{coin}raw_high_{tf}', f'%-{coin}raw_close_{tf}']
|
|
|
|
rename_dict = {f'%-{coin}raw_open_{tf}': 'open', f'%-{coin}raw_low_{tf}': 'low',
|
|
|
|
f'%-{coin}raw_high_{tf}': ' high', f'%-{coin}raw_close_{tf}': 'close'}
|
|
|
|
|
|
|
|
prices_train = train_df.filter(ohlc_list, axis=1)
|
|
|
|
prices_train.rename(columns=rename_dict, inplace=True)
|
|
|
|
prices_train.reset_index(drop=True)
|
|
|
|
|
|
|
|
prices_test = test_df.filter(ohlc_list, axis=1)
|
|
|
|
prices_test.rename(columns=rename_dict, inplace=True)
|
|
|
|
prices_test.reset_index(drop=True)
|
|
|
|
|
|
|
|
return prices_train, prices_test
|
|
|
|
|
2022-08-15 16:01:15 +00:00
|
|
|
# TODO take care of this appendage. Right now it needs to be called because FreqAI enforces it.
|
|
|
|
# But FreqaiRL needs more objects passed to fit() (like DK) and we dont want to go refactor
|
|
|
|
# all the other existing fit() functions to include dk argument. For now we instantiate and
|
|
|
|
# leave it.
|
|
|
|
def fit(self, data_dictionary: Dict[str, Any], pair: str = '') -> Any:
|
|
|
|
return
|
|
|
|
|
2022-08-21 17:58:36 +00:00
|
|
|
|
2022-08-24 10:54:02 +00:00
|
|
|
def make_env(env_id: str, rank: int, seed: int, train_df: DataFrame, price: DataFrame,
|
|
|
|
reward_params: Dict[str, int], window_size: int, monitor: bool = False,
|
|
|
|
config: Dict[str, Any] = {}) -> Callable:
|
2022-08-20 14:35:29 +00:00
|
|
|
"""
|
|
|
|
Utility function for multiprocessed env.
|
|
|
|
|
|
|
|
:param env_id: (str) the environment ID
|
|
|
|
:param num_env: (int) the number of environment you wish to have in subprocesses
|
|
|
|
:param seed: (int) the inital seed for RNG
|
|
|
|
:param rank: (int) index of the subprocess
|
|
|
|
:return: (Callable)
|
|
|
|
"""
|
|
|
|
def _init() -> gym.Env:
|
|
|
|
|
|
|
|
env = MyRLEnv(df=train_df, prices=price, window_size=window_size,
|
2022-08-21 18:33:09 +00:00
|
|
|
reward_kwargs=reward_params, id=env_id, seed=seed + rank, config=config)
|
2022-08-20 14:35:29 +00:00
|
|
|
if monitor:
|
|
|
|
env = Monitor(env, ".")
|
|
|
|
return env
|
|
|
|
set_random_seed(seed)
|
|
|
|
return _init
|
2022-08-15 08:26:44 +00:00
|
|
|
|
2022-08-21 17:58:36 +00:00
|
|
|
|
2022-08-18 14:07:19 +00:00
|
|
|
class MyRLEnv(Base5ActionRLEnv):
|
|
|
|
"""
|
|
|
|
User can override any function in BaseRLEnv and gym.Env. Here the user
|
2022-08-23 12:58:38 +00:00
|
|
|
sets a custom reward based on profit and trade duration.
|
2022-08-18 14:07:19 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def calculate_reward(self, action):
|
|
|
|
|
2022-08-24 16:32:40 +00:00
|
|
|
# first, penalize if the action is not valid
|
|
|
|
if not self._is_valid(action):
|
|
|
|
return -15
|
2022-08-18 14:07:19 +00:00
|
|
|
|
2022-08-23 12:58:38 +00:00
|
|
|
pnl = self.get_unrealized_profit()
|
2022-08-24 16:32:40 +00:00
|
|
|
rew = np.sign(pnl) * (pnl + 1)
|
|
|
|
factor = 100
|
|
|
|
|
|
|
|
# reward agent for entering trades
|
|
|
|
if action in (Actions.Long_enter.value, Actions.Short_enter.value):
|
|
|
|
return 25
|
|
|
|
# discourage agent from not entering trades
|
|
|
|
if action == Actions.Neutral.value and self._position == Positions.Neutral:
|
|
|
|
return -15
|
|
|
|
|
|
|
|
max_trade_duration = self.rl_config.get('max_trade_duration_candles', 300)
|
2022-08-23 12:58:38 +00:00
|
|
|
trade_duration = self._current_tick - self._last_trade_tick
|
|
|
|
|
|
|
|
if trade_duration <= max_trade_duration:
|
|
|
|
factor *= 1.5
|
|
|
|
elif trade_duration > max_trade_duration:
|
|
|
|
factor *= 0.5
|
|
|
|
|
2022-08-24 16:32:40 +00:00
|
|
|
# discourage sitting in position
|
|
|
|
if self._position in (Positions.Short, Positions.Long):
|
|
|
|
return -50 * trade_duration / max_trade_duration
|
|
|
|
|
2022-08-18 14:07:19 +00:00
|
|
|
# close long
|
2022-08-20 14:35:29 +00:00
|
|
|
if action == Actions.Long_exit.value and self._position == Positions.Long:
|
2022-08-24 16:32:40 +00:00
|
|
|
if pnl > self.profit_aim * self.rr:
|
2022-08-23 12:58:38 +00:00
|
|
|
factor *= self.rl_config['model_reward_parameters'].get('win_reward_factor', 2)
|
2022-08-24 16:32:40 +00:00
|
|
|
return float(rew * factor)
|
2022-08-18 14:07:19 +00:00
|
|
|
|
|
|
|
# close short
|
2022-08-20 14:35:29 +00:00
|
|
|
if action == Actions.Short_exit.value and self._position == Positions.Short:
|
2022-08-24 16:32:40 +00:00
|
|
|
if pnl > self.profit_aim * self.rr:
|
2022-08-23 12:58:38 +00:00
|
|
|
factor *= self.rl_config['model_reward_parameters'].get('win_reward_factor', 2)
|
2022-08-24 16:32:40 +00:00
|
|
|
return float(rew * factor)
|
2022-08-15 08:26:44 +00:00
|
|
|
|
2022-08-18 14:07:19 +00:00
|
|
|
return 0.
|