forked from AI4Finance-Foundation/FinRL-Meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
79 lines (67 loc) · 2.36 KB
/
test.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
# use three drl libraries: elegantrl, rllib, and SB3
from agents.elegantrl_models import DRLAgent as DRLAgent_erl
from agents.rllib_models import DRLAgent as DRLAgent_rllib
from agents.stablebaselines3_models import DRLAgent as DRLAgent_sb3
from meta.data_processor import DataProcessor
def test(
start_date,
end_date,
ticker_list,
data_source,
time_interval,
technical_indicator_list,
drl_lib,
env,
model_name,
if_vix=True,
**kwargs
):
# process data using unified data processor
dp = DataProcessor(data_source, start_date, end_date, time_interval, **kwargs)
price_array, tech_array, turbulence_array = dp.run(
ticker_list, technical_indicator_list, if_vix
)
data_config = {
"price_array": price_array,
"tech_array": tech_array,
"turbulence_array": turbulence_array,
}
# build environment using processed data
env_instance = env(config=data_config)
env_config = {
"price_array": price_array,
"tech_array": tech_array,
"turbulence_array": turbulence_array,
"if_train": False,
}
env_instance = env(config=env_config)
# elegantrl needs state dim, action dim and net dim
net_dimension = kwargs.get("net_dimension", 2**7)
cwd = kwargs.get("cwd", "./" + str(model_name))
print("price_array: ", len(price_array))
if drl_lib == "elegantrl":
episode_total_assets = DRLAgent_erl.DRL_prediction(
model_name=model_name,
cwd=cwd,
net_dimension=net_dimension,
environment=env_instance,
)
return episode_total_assets
elif drl_lib == "rllib":
# load agent
episode_total_assets = DRLAgent_rllib.DRL_prediction(
model_name=model_name,
env=env,
price_array=price_array,
tech_array=tech_array,
turbulence_array=turbulence_array,
agent_path=cwd,
)
return episode_total_assets
elif drl_lib == "stable_baselines3":
episode_total_assets = DRLAgent_sb3.DRL_prediction_load_from_file(
model_name=model_name, environment=env_instance, cwd=cwd
)
return episode_total_assets
else:
raise ValueError("DRL library input is NOT supported. Please check.")