2022-08-20 14:35:29 +00:00
|
|
|
import logging
|
2022-12-04 12:54:30 +00:00
|
|
|
from typing import Any, Dict
|
2022-08-20 14:35:29 +00:00
|
|
|
|
2022-09-23 17:30:56 +00:00
|
|
|
from pandas import DataFrame
|
2023-06-11 19:20:15 +00:00
|
|
|
from sb3_contrib.common.maskable.callbacks import MaskableEvalCallback
|
|
|
|
from sb3_contrib.common.maskable.utils import is_masking_supported
|
2023-04-17 18:27:18 +00:00
|
|
|
from stable_baselines3.common.vec_env import SubprocVecEnv, VecMonitor
|
|
|
|
|
2022-08-28 17:21:57 +00:00
|
|
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
2022-11-26 10:51:08 +00:00
|
|
|
from freqtrade.freqai.prediction_models.ReinforcementLearner import ReinforcementLearner
|
2022-12-04 12:54:30 +00:00
|
|
|
from freqtrade.freqai.RL.BaseReinforcementLearningModel import make_env
|
2023-05-12 07:56:44 +00:00
|
|
|
from freqtrade.freqai.tensorboard.TensorboardCallback import TensorboardCallback
|
2022-08-20 14:35:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-11-26 10:51:08 +00:00
|
|
|
class ReinforcementLearner_multiproc(ReinforcementLearner):
|
2022-08-20 14:35:29 +00:00
|
|
|
"""
|
2022-11-26 10:51:08 +00:00
|
|
|
Demonstration of how to build vectorized environments
|
2022-08-20 14:35:29 +00:00
|
|
|
"""
|
|
|
|
|
2022-09-23 17:17:27 +00:00
|
|
|
def set_train_and_eval_environments(self, data_dictionary: Dict[str, Any],
|
|
|
|
prices_train: DataFrame, prices_test: DataFrame,
|
|
|
|
dk: FreqaiDataKitchen):
|
2022-08-20 14:35:29 +00:00
|
|
|
"""
|
2022-09-23 17:17:27 +00:00
|
|
|
User can override this if they are using a custom MyRLEnv
|
2022-11-13 16:43:52 +00:00
|
|
|
:param data_dictionary: dict = common data dictionary containing train and test
|
2022-09-23 17:17:27 +00:00
|
|
|
features/labels/weights.
|
2022-11-13 16:43:52 +00:00
|
|
|
:param prices_train/test: DataFrame = dataframe comprised of the prices to be used in
|
2022-09-23 17:17:27 +00:00
|
|
|
the environment during training
|
|
|
|
or testing
|
2022-11-13 16:43:52 +00:00
|
|
|
:param dk: FreqaiDataKitchen = the datakitchen for the current pair
|
2022-08-20 14:35:29 +00:00
|
|
|
"""
|
|
|
|
train_df = data_dictionary["train_features"]
|
|
|
|
test_df = data_dictionary["test_features"]
|
|
|
|
|
2023-02-24 10:19:54 +00:00
|
|
|
if self.train_env:
|
|
|
|
self.train_env.close()
|
|
|
|
if self.eval_env:
|
|
|
|
self.eval_env.close()
|
|
|
|
|
2023-02-10 13:45:50 +00:00
|
|
|
env_info = self.pack_env_dict(dk.pair)
|
2022-12-14 19:03:05 +00:00
|
|
|
|
2023-03-20 14:54:58 +00:00
|
|
|
eval_freq = len(train_df) // self.max_threads
|
|
|
|
|
2022-08-25 09:46:18 +00:00
|
|
|
env_id = "train_env"
|
2023-03-20 14:54:58 +00:00
|
|
|
self.train_env = VecMonitor(SubprocVecEnv([make_env(self.MyRLEnv, env_id, i, 1,
|
2023-04-17 18:27:18 +00:00
|
|
|
train_df, prices_train,
|
|
|
|
env_info=env_info) for i
|
|
|
|
in range(self.max_threads)]))
|
2022-08-25 09:46:18 +00:00
|
|
|
|
|
|
|
eval_env_id = 'eval_env'
|
2023-03-20 14:54:58 +00:00
|
|
|
self.eval_env = VecMonitor(SubprocVecEnv([make_env(self.MyRLEnv, eval_env_id, i, 1,
|
2023-04-17 18:27:18 +00:00
|
|
|
test_df, prices_test,
|
|
|
|
env_info=env_info) for i
|
|
|
|
in range(self.max_threads)]))
|
|
|
|
|
2023-06-11 19:20:15 +00:00
|
|
|
self.eval_callback = MaskableEvalCallback(self.eval_env, deterministic=True,
|
|
|
|
render=False, eval_freq=eval_freq,
|
|
|
|
best_model_save_path=str(dk.data_path),
|
|
|
|
use_masking=(self.model_type == 'MaskablePPO' and
|
|
|
|
is_masking_supported(self.eval_env)))
|
2022-12-03 11:30:04 +00:00
|
|
|
|
2023-04-17 18:27:18 +00:00
|
|
|
# TENSORBOARD CALLBACK DOES NOT RECOMMENDED TO USE WITH MULTIPLE ENVS,
|
|
|
|
# IT WILL RETURN FALSE INFORMATIONS, NEVERTHLESS NOT THREAD SAFE WITH SB3!!!
|
2022-12-04 12:54:30 +00:00
|
|
|
actions = self.train_env.env_method("get_actions")[0]
|
|
|
|
self.tensorboard_callback = TensorboardCallback(verbose=1, actions=actions)
|