-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsb3_models.py
757 lines (682 loc) · 28.7 KB
/
sb3_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# DRL models from Stable Baselines 3
from __future__ import annotations
import time
import numpy as np
import pandas as pd
from stable_baselines3 import A2C
from stable_baselines3 import DDPG
from stable_baselines3 import PPO
from stable_baselines3 import SAC
from stable_baselines3 import TD3
from stable_baselines3.common.callbacks import BaseCallback
from stable_baselines3.common.noise import NormalActionNoise
from stable_baselines3.common.noise import OrnsteinUhlenbeckActionNoise
from stable_baselines3.common.vec_env import DummyVecEnv
from finrl import config
from finrl.meta.env_stock_trading.env_stocktrading import StockTradingEnv
from finrl.meta.preprocessor.preprocessors import data_split
MODELS = {"a2c": A2C, "ddpg": DDPG, "td3": TD3, "sac": SAC, "ppo": PPO}
MODEL_KWARGS = {x: config.__dict__[f"{x.upper()}_PARAMS"] for x in MODELS.keys()}
NOISE = {
"normal": NormalActionNoise,
"ornstein_uhlenbeck": OrnsteinUhlenbeckActionNoise,
}
class TensorboardCallback(BaseCallback):
"""
Custom callback for plotting additional values in tensorboard.
"""
def __init__(self, verbose=0, callback=None, end_training_time=None):
super().__init__(verbose)
self.callback = callback
self.end_training_time = end_training_time
def _on_step(self) -> bool:
try:
self.logger.record(key="train/reward", value=self.locals["rewards"][0])
except BaseException as error:
try:
self.logger.record(key="train/reward", value=self.locals["reward"][0])
except BaseException as inner_error:
# Handle the case where neither "rewards" nor "reward" is found
self.logger.record(key="train/reward", value=None)
# Print the original error and the inner error for debugging
print("Original Error:", error)
print("Inner Error:", inner_error)
print("locals", self.locals['infos'])
if self.callback: self.callback(self.logger.name_to_value)
return time.time() < self.end_training_time if self.end_training_time != None else True
class DRLAgent:
"""Provides implementations for DRL algorithms
Attributes
----------
env: gym environment class
user-defined class
Methods
-------
get_model()
setup DRL algorithms
train_model()
train DRL algorithms in a train dataset
and output the trained model
DRL_prediction()
make a prediction in a test dataset and get results
"""
def __init__(self, env):
self.env = env
def get_model(
self,
model_name,
policy="MlpPolicy",
policy_kwargs=None,
model_kwargs=None,
verbose=1,
seed=None,
tensorboard_log=None,
):
if model_name not in MODELS:
raise ValueError(
f"Model '{model_name}' not found in MODELS."
) # this is more informative than NotImplementedError("NotImplementedError")
if model_kwargs is None:
model_kwargs = MODEL_KWARGS[model_name]
if "action_noise" in model_kwargs:
n_actions = self.env.action_space.shape[-1]
model_kwargs["action_noise"] = NOISE[model_kwargs["action_noise"]](
mean=np.zeros(n_actions), sigma=0.1 * np.ones(n_actions)
)
print(model_kwargs)
return MODELS[model_name](
policy=policy,
env=self.env,
tensorboard_log=tensorboard_log,
verbose=verbose,
policy_kwargs=policy_kwargs,
seed=seed,
**model_kwargs,
)
@staticmethod
def train_model(
model, tb_log_name, total_timesteps=2**31, callback=None, train_time_seconds=None
): # this function is static method, so it can be called without creating an instance of the class
end_training_time = int(time.time()) + train_time_seconds if train_time_seconds != None else None
model = model.learn(
total_timesteps=total_timesteps,
tb_log_name=tb_log_name,
callback=TensorboardCallback(callback=callback, end_training_time=end_training_time),
)
return model
@staticmethod
def DRL_prediction(model, environment, deterministic=True):
"""make a prediction and get results"""
test_env, test_obs = environment.get_sb_env()
account_memory = None # This help avoid unnecessary list creation
actions_memory = None # optimize memory consumption
# state_memory=[] #add memory pool to store states
test_env.reset()
max_steps = len(environment.df.index.unique()) - 1
for i in range(len(environment.df.index.unique())):
action, _states = model.predict(test_obs, deterministic=deterministic)
# account_memory = test_env.env_method(method_name="save_asset_memory")
# actions_memory = test_env.env_method(method_name="save_action_memory")
test_obs, rewards, dones, info = test_env.step(action)
if (
i == max_steps - 1
): # more descriptive condition for early termination to clarify the logic
account_memory = test_env.env_method(method_name="save_asset_memory")
actions_memory = test_env.env_method(method_name="save_action_memory")
# add current state to state memory
# state_memory=test_env.env_method(method_name="save_state_memory")
if dones[0]:
print("hit end!")
break
return account_memory[0], actions_memory[0]
@staticmethod
def DRL_prediction_load_from_file(model_name, environment, cwd, deterministic=True):
if model_name not in MODELS:
raise ValueError(
f"Model '{model_name}' not found in MODELS."
) # this is more informative than NotImplementedError("NotImplementedError")
try:
# load agent
model = MODELS[model_name].load(cwd)
print("Successfully load model", cwd)
except BaseException as error:
raise ValueError(f"Failed to load agent. Error: {str(error)}") from error
# test on the testing env
state = environment.reset()
episode_returns = [] # the cumulative_return / initial_account
episode_total_assets = [environment.initial_total_asset]
done = False
while not done:
action = model.predict(state, deterministic=deterministic)[0]
state, reward, done, _ = environment.step(action)
total_asset = (
environment.amount
+ (environment.price_ary[environment.day] * environment.stocks).sum()
)
episode_total_assets.append(total_asset)
episode_return = total_asset / environment.initial_total_asset
episode_returns.append(episode_return)
print("episode_return", episode_return)
print("Test Finished!")
return episode_total_assets
class DRLEnsembleAgent:
@staticmethod
def get_model(
model_name,
env,
policy="MlpPolicy",
policy_kwargs=None,
model_kwargs=None,
seed=None,
verbose=1,
):
if model_name not in MODELS:
raise ValueError(
f"Model '{model_name}' not found in MODELS."
) # this is more informative than NotImplementedError("NotImplementedError")
if model_kwargs is None:
temp_model_kwargs = MODEL_KWARGS[model_name]
else:
temp_model_kwargs = model_kwargs.copy()
if "action_noise" in temp_model_kwargs:
n_actions = env.action_space.shape[-1]
temp_model_kwargs["action_noise"] = NOISE[
temp_model_kwargs["action_noise"]
](mean=np.zeros(n_actions), sigma=0.1 * np.ones(n_actions))
print(temp_model_kwargs)
return MODELS[model_name](
policy=policy,
env=env,
tensorboard_log=f"{config.TENSORBOARD_LOG_DIR}/{model_name}",
verbose=verbose,
policy_kwargs=policy_kwargs,
seed=seed,
**temp_model_kwargs,
)
@staticmethod
def train_model(model, model_name, tb_log_name, iter_num, total_timesteps=5000, callback=None):
model = model.learn(
total_timesteps=total_timesteps,
tb_log_name=tb_log_name,
callback=TensorboardCallback(callback=callback),
)
model.save(
f"{config.TRAINED_MODEL_DIR}/{model_name.upper()}_{total_timesteps // 1000}k_{iter_num}"
)
return model
@staticmethod
def get_validation_sharpe(iteration, model_name):
"""Calculate Sharpe ratio based on validation results"""
df_total_value = pd.read_csv(
f"results/account_value_validation_{model_name}_{iteration}.csv"
)
# If the agent did not make any transaction
if df_total_value["daily_return"].var() == 0:
if df_total_value["daily_return"].mean() > 0:
return np.inf
else:
return 0.0
else:
return (
(4**0.5)
* df_total_value["daily_return"].mean()
/ df_total_value["daily_return"].std()
)
def __init__(
self,
df,
train_period,
val_test_period,
rebalance_window,
validation_window,
stock_dim,
hmax,
initial_amount,
buy_cost_pct,
sell_cost_pct,
reward_scaling,
state_space,
action_space,
tech_indicator_list,
print_verbosity,
):
self.df = df
self.train_period = train_period
self.val_test_period = val_test_period
self.unique_trade_date = df[
(df.date > val_test_period[0]) & (df.date <= val_test_period[1])
].date.unique()
self.rebalance_window = rebalance_window
self.validation_window = validation_window
self.stock_dim = stock_dim
self.hmax = hmax
self.initial_amount = initial_amount
self.buy_cost_pct = buy_cost_pct
self.sell_cost_pct = sell_cost_pct
self.reward_scaling = reward_scaling
self.state_space = state_space
self.action_space = action_space
self.tech_indicator_list = tech_indicator_list
self.print_verbosity = print_verbosity
self.train_env = None # defined in train_validation() function
def DRL_validation(self, model, test_data, test_env, test_obs):
"""validation process"""
for _ in range(len(test_data.index.unique())):
action, _states = model.predict(test_obs)
test_obs, rewards, dones, info = test_env.step(action)
def DRL_prediction(
self, model, name, last_state, iter_num, turbulence_threshold, initial
):
"""make a prediction based on trained model"""
# trading env
trade_data = data_split(
self.df,
start=self.unique_trade_date[iter_num - self.rebalance_window],
end=self.unique_trade_date[iter_num],
)
trade_env = DummyVecEnv(
[
lambda: StockTradingEnv(
df=trade_data,
stock_dim=self.stock_dim,
hmax=self.hmax,
initial_amount=self.initial_amount,
num_stock_shares=[0] * self.stock_dim,
buy_cost_pct=[self.buy_cost_pct] * self.stock_dim,
sell_cost_pct=[self.sell_cost_pct] * self.stock_dim,
reward_scaling=self.reward_scaling,
state_space=self.state_space,
action_space=self.action_space,
tech_indicator_list=self.tech_indicator_list,
turbulence_threshold=turbulence_threshold,
initial=initial,
previous_state=last_state,
model_name=name,
mode="trade",
iteration=iter_num,
print_verbosity=self.print_verbosity,
)
]
)
trade_obs = trade_env.reset()
for i in range(len(trade_data.index.unique())):
action, _states = model.predict(trade_obs)
trade_obs, rewards, dones, info = trade_env.step(action)
if i == (len(trade_data.index.unique()) - 2):
# print(env_test.render())
last_state = trade_env.envs[0].render()
df_last_state = pd.DataFrame({"last_state": last_state})
df_last_state.to_csv(f"results/last_state_{name}_{i}.csv", index=False)
return last_state
def run_ensemble_strategy(
self, A2C_model_kwargs, PPO_model_kwargs, DDPG_model_kwargs, timesteps_dict
):
"""Ensemble Strategy that combines PPO, A2C and DDPG"""
print("============Start Ensemble Strategy============")
# for ensemble model, it's necessary to feed the last state
# of the previous model to the current model as the initial state
last_state_ensemble = []
ppo_sharpe_list = []
ddpg_sharpe_list = []
a2c_sharpe_list = []
model_use = []
validation_start_date_list = []
validation_end_date_list = []
iteration_list = []
insample_turbulence = self.df[
(self.df.date < self.train_period[1])
& (self.df.date >= self.train_period[0])
]
insample_turbulence_threshold = np.quantile(
insample_turbulence.turbulence.values, 0.90
)
start = time.time()
for i in range(
self.rebalance_window + self.validation_window,
len(self.unique_trade_date),
self.rebalance_window,
):
validation_start_date = self.unique_trade_date[
i - self.rebalance_window - self.validation_window
]
validation_end_date = self.unique_trade_date[i - self.rebalance_window]
validation_start_date_list.append(validation_start_date)
validation_end_date_list.append(validation_end_date)
iteration_list.append(i)
print("============================================")
# initial state is empty
if i - self.rebalance_window - self.validation_window == 0:
# inital state
initial = True
else:
# previous state
initial = False
# Tuning trubulence index based on historical data
# Turbulence lookback window is one quarter (63 days)
end_date_index = self.df.index[
self.df["date"]
== self.unique_trade_date[
i - self.rebalance_window - self.validation_window
]
].to_list()[-1]
start_date_index = end_date_index - 63 + 1
historical_turbulence = self.df.iloc[
start_date_index : (end_date_index + 1), :
]
historical_turbulence = historical_turbulence.drop_duplicates(
subset=["date"]
)
historical_turbulence_mean = np.mean(
historical_turbulence.turbulence.values
)
# print(historical_turbulence_mean)
if historical_turbulence_mean > insample_turbulence_threshold:
# if the mean of the historical data is greater than the 90% quantile of insample turbulence data
# then we assume that the current market is volatile,
# therefore we set the 90% quantile of insample turbulence data as the turbulence threshold
# meaning the current turbulence can't exceed the 90% quantile of insample turbulence data
turbulence_threshold = insample_turbulence_threshold
else:
# if the mean of the historical data is less than the 90% quantile of insample turbulence data
# then we tune up the turbulence_threshold, meaning we lower the risk
turbulence_threshold = np.quantile(
insample_turbulence.turbulence.values, 1
)
turbulence_threshold = np.quantile(
insample_turbulence.turbulence.values, 0.99
)
print("turbulence_threshold: ", turbulence_threshold)
# Environment Setup starts
# training env
train = data_split(
self.df,
start=self.train_period[0],
end=self.unique_trade_date[
i - self.rebalance_window - self.validation_window
],
)
self.train_env = DummyVecEnv(
[
lambda: StockTradingEnv(
df=train,
stock_dim=self.stock_dim,
hmax=self.hmax,
initial_amount=self.initial_amount,
num_stock_shares=[0] * self.stock_dim,
buy_cost_pct=[self.buy_cost_pct] * self.stock_dim,
sell_cost_pct=[self.sell_cost_pct] * self.stock_dim,
reward_scaling=self.reward_scaling,
state_space=self.state_space,
action_space=self.action_space,
tech_indicator_list=self.tech_indicator_list,
print_verbosity=self.print_verbosity,
)
]
)
validation = data_split(
self.df,
start=self.unique_trade_date[
i - self.rebalance_window - self.validation_window
],
end=self.unique_trade_date[i - self.rebalance_window],
)
# Environment Setup ends
# Training and Validation starts
print(
"======Model training from: ",
self.train_period[0],
"to ",
self.unique_trade_date[
i - self.rebalance_window - self.validation_window
],
)
# print("training: ",len(data_split(df, start=20090000, end=test.datadate.unique()[i-rebalance_window]) ))
# print("==============Model Training===========")
print("======A2C Training========")
model_a2c = self.get_model(
"a2c", self.train_env, policy="MlpPolicy", model_kwargs=A2C_model_kwargs
)
model_a2c = self.train_model(
model_a2c,
"a2c",
tb_log_name=f"a2c_{i}",
iter_num=i,
total_timesteps=timesteps_dict["a2c"],
) # 100_000
print(
"======A2C Validation from: ",
validation_start_date,
"to ",
validation_end_date,
)
val_env_a2c = DummyVecEnv(
[
lambda: StockTradingEnv(
df=validation,
stock_dim=self.stock_dim,
hmax=self.hmax,
initial_amount=self.initial_amount,
num_stock_shares=[0] * self.stock_dim,
buy_cost_pct=[self.buy_cost_pct] * self.stock_dim,
sell_cost_pct=[self.sell_cost_pct] * self.stock_dim,
reward_scaling=self.reward_scaling,
state_space=self.state_space,
action_space=self.action_space,
tech_indicator_list=self.tech_indicator_list,
turbulence_threshold=turbulence_threshold,
iteration=i,
model_name="A2C",
mode="validation",
print_verbosity=self.print_verbosity,
)
]
)
val_obs_a2c = val_env_a2c.reset()
self.DRL_validation(
model=model_a2c,
test_data=validation,
test_env=val_env_a2c,
test_obs=val_obs_a2c,
)
sharpe_a2c = self.get_validation_sharpe(i, model_name="A2C")
print("A2C Sharpe Ratio: ", sharpe_a2c)
print("======PPO Training========")
model_ppo = self.get_model(
"ppo", self.train_env, policy="MlpPolicy", model_kwargs=PPO_model_kwargs
)
model_ppo = self.train_model(
model_ppo,
"ppo",
tb_log_name=f"ppo_{i}",
iter_num=i,
total_timesteps=timesteps_dict["ppo"],
) # 100_000
print(
"======PPO Validation from: ",
validation_start_date,
"to ",
validation_end_date,
)
val_env_ppo = DummyVecEnv(
[
lambda: StockTradingEnv(
df=validation,
stock_dim=self.stock_dim,
hmax=self.hmax,
initial_amount=self.initial_amount,
num_stock_shares=[0] * self.stock_dim,
buy_cost_pct=[self.buy_cost_pct] * self.stock_dim,
sell_cost_pct=[self.sell_cost_pct] * self.stock_dim,
reward_scaling=self.reward_scaling,
state_space=self.state_space,
action_space=self.action_space,
tech_indicator_list=self.tech_indicator_list,
turbulence_threshold=turbulence_threshold,
iteration=i,
model_name="PPO",
mode="validation",
print_verbosity=self.print_verbosity,
)
]
)
val_obs_ppo = val_env_ppo.reset()
self.DRL_validation(
model=model_ppo,
test_data=validation,
test_env=val_env_ppo,
test_obs=val_obs_ppo,
)
sharpe_ppo = self.get_validation_sharpe(i, model_name="PPO")
print("PPO Sharpe Ratio: ", sharpe_ppo)
print("======DDPG Training========")
model_ddpg = self.get_model(
"ddpg",
self.train_env,
policy="MlpPolicy",
model_kwargs=DDPG_model_kwargs,
)
model_ddpg = self.train_model(
model_ddpg,
"ddpg",
tb_log_name=f"ddpg_{i}",
iter_num=i,
total_timesteps=timesteps_dict["ddpg"],
) # 50_000
print(
"======DDPG Validation from: ",
validation_start_date,
"to ",
validation_end_date,
)
val_env_ddpg = DummyVecEnv(
[
lambda: StockTradingEnv(
df=validation,
stock_dim=self.stock_dim,
hmax=self.hmax,
initial_amount=self.initial_amount,
num_stock_shares=[0] * self.stock_dim,
buy_cost_pct=[self.buy_cost_pct] * self.stock_dim,
sell_cost_pct=[self.sell_cost_pct] * self.stock_dim,
reward_scaling=self.reward_scaling,
state_space=self.state_space,
action_space=self.action_space,
tech_indicator_list=self.tech_indicator_list,
turbulence_threshold=turbulence_threshold,
iteration=i,
model_name="DDPG",
mode="validation",
print_verbosity=self.print_verbosity,
)
]
)
val_obs_ddpg = val_env_ddpg.reset()
self.DRL_validation(
model=model_ddpg,
test_data=validation,
test_env=val_env_ddpg,
test_obs=val_obs_ddpg,
)
sharpe_ddpg = self.get_validation_sharpe(i, model_name="DDPG")
ppo_sharpe_list.append(sharpe_ppo)
a2c_sharpe_list.append(sharpe_a2c)
ddpg_sharpe_list.append(sharpe_ddpg)
print(
"======Best Model Retraining from: ",
self.train_period[0],
"to ",
self.unique_trade_date[i - self.rebalance_window],
)
# Environment setup for model retraining up to first trade date
# train_full = data_split(self.df, start=self.train_period[0],
# end=self.unique_trade_date[i - self.rebalance_window])
# self.train_full_env = DummyVecEnv([lambda: StockTradingEnv(train_full,
# self.stock_dim,
# self.hmax,
# self.initial_amount,
# self.buy_cost_pct,
# self.sell_cost_pct,
# self.reward_scaling,
# self.state_space,
# self.action_space,
# self.tech_indicator_list,
# print_verbosity=self.print_verbosity
# )])
# Model Selection based on sharpe ratio
if (sharpe_ppo >= sharpe_a2c) & (sharpe_ppo >= sharpe_ddpg):
model_use.append("PPO")
model_ensemble = model_ppo
# model_ensemble = self.get_model("ppo",
# self.train_full_env,
# policy="MlpPolicy",
# model_kwargs=PPO_model_kwargs)
# model_ensemble = self.train_model(model_ensemble,
# "ensemble",
# tb_log_name="ensemble_{}".format(i),
# iter_num = i,
# total_timesteps=timesteps_dict['ppo']) #100_000
elif (sharpe_a2c > sharpe_ppo) & (sharpe_a2c > sharpe_ddpg):
model_use.append("A2C")
model_ensemble = model_a2c
# model_ensemble = self.get_model("a2c",
# self.train_full_env,
# policy="MlpPolicy",
# model_kwargs=A2C_model_kwargs)
# model_ensemble = self.train_model(model_ensemble,
# "ensemble",
# tb_log_name="ensemble_{}".format(i),
# iter_num = i,
# total_timesteps=timesteps_dict['a2c']) #100_000
else:
model_use.append("DDPG")
model_ensemble = model_ddpg
# model_ensemble = self.get_model("ddpg",
# self.train_full_env,
# policy="MlpPolicy",
# model_kwargs=DDPG_model_kwargs)
# model_ensemble = self.train_model(model_ensemble,
# "ensemble",
# tb_log_name="ensemble_{}".format(i),
# iter_num = i,
# total_timesteps=timesteps_dict['ddpg']) #50_000
# Training and Validation ends
# Trading starts
print(
"======Trading from: ",
self.unique_trade_date[i - self.rebalance_window],
"to ",
self.unique_trade_date[i],
)
# print("Used Model: ", model_ensemble)
last_state_ensemble = self.DRL_prediction(
model=model_ensemble,
name="ensemble",
last_state=last_state_ensemble,
iter_num=i,
turbulence_threshold=turbulence_threshold,
initial=initial,
)
# Trading ends
end = time.time()
print("Ensemble Strategy took: ", (end - start) / 60, " minutes")
df_summary = pd.DataFrame(
[
iteration_list,
validation_start_date_list,
validation_end_date_list,
model_use,
a2c_sharpe_list,
ppo_sharpe_list,
ddpg_sharpe_list,
]
).T
df_summary.columns = [
"Iter",
"Val Start",
"Val End",
"Model Used",
"A2C Sharpe",
"PPO Sharpe",
"DDPG Sharpe",
]
return df_summary