-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathray_result_dqn_noisy.py
80 lines (71 loc) · 2.31 KB
/
ray_result_dqn_noisy.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
import warnings
import pandas as pd
import ray
from ray.tune.registry import register_env
import ray.rllib.agents.dqn as dqn
from env.env_long2 import TradingEnvLong
from utils import result_plt, year_frac
warnings.filterwarnings('ignore')
def create_env(env_kwargs={}):
data_df = pd.read_csv("data_simple2.csv")
data_df['Date'] = pd.to_datetime(data_df['Date'])
train = data_df[(data_df.Date >= '2000-01-01')]
# the index needs to start from 0
train = train.reset_index(drop=True)
env = TradingEnvLong(df=train, log=True, **env_kwargs)
return env
register_env("TestEnv", create_env)
ray.init()
checkpoint_path = 'model/DQN_noisy/checkpoint_000500/checkpoint-500'
# Restore agent
agent = dqn.DQNTrainer(
env="TestEnv",
config={
"env": "TradingEnv",
"log_level": "WARN",
"framework": "tf",
"ignore_worker_failures": True,
"num_gpus": 1,
"num_atoms": 1,
"noisy": True,
"v_min": -10000.0,
"v_max": 10000.0,
"gamma": 0.9,
"lr": .0001,
"dueling": False,
"hiddens": [512],
"learning_starts": 10000,
"buffer_size": 50000,
"rollout_fragment_length": 4,
"train_batch_size": 32,
"double_q": False,
"exploration_config": {
"epsilon_timesteps": 2,
"final_epsilon": 0.0,
},
"target_network_update_freq": 500,
"prioritized_replay": False,
"prioritized_replay_alpha": 0.5,
"final_prioritized_replay_beta": 1.0,
"prioritized_replay_beta_annealing_timesteps": 400000,
"n_step": 1
}
)
agent.restore(checkpoint_path)
test_gym = create_env()
out = []
#%%
for _ in range(17):
done = False
obs = test_gym.reset()
while not done:
action = agent.compute_action(obs)
obs, reward, done, tmp = test_gym.step(action)
#test_gym.render()
out.append(tmp)
out_df = pd.DataFrame(out, columns=['Net Pnl', 'rtn_on_MDD', 'PF', 'CAGR', 'num', 'winning_rate'])
result_plt(title='dqn_noisy')
year_num = year_frac(test_gym.equity_memory['date'].iloc[0],
test_gym.equity_memory[test_gym.equity_memory.equity_tmp > 0]['date'].iloc[-1])
cagr = ((out_df['Net Pnl'].mean() + test_gym.init_equity) / test_gym.init_equity) ** (1 / year_num) - 1
print(round(cagr * 100, 3))