-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaper_trading_1.py
301 lines (237 loc) · 12.7 KB
/
paper_trading_1.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
# -*- coding: utf-8 -*-
"""paper_trading.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ynUeKsAkAAwXPbBAg29AXskYhc-Ijd2A
# 1. Environment Setup
"""
## install FinRL library
#!pip install git+https://github.com/AI4Finance-Foundation/FinRL.git
#some problem downloading FinRL, still seeking solutions...
# from finrl.config_tickers import DOW_30_TICKER
# from finrl.config import INDICATORS
# from finrl.meta.env_stock_trading.env_stocktrading_np import StockTradingEnv
# from finrl.meta.env_stock_trading.env_stock_papertrading import AlpacaPaperTrading
# from finrl.meta.data_processor import DataProcessor
# from finrl.plot import backtest_stats, backtest_plot, get_daily_return, get_baseline
"""# 2. Reinforcement Learning Agent (in process...)"""
import numpy as np
import pandas as pd
import os
import time
import gym
import numpy as np
import numpy.random as rd
import torch
import torch.nn as nn
from copy import deepcopy
from torch import Tensor
from torch.distributions.normal import Normal
#Actor-critic network
class ActorPPO(nn.Module):
def __init__(self, dims, state_dim: int, action_dim: int):
super().__init__()
self.net = build_mlp(dims=[state_dim, *dims, action_dim])
self.action_std_log = nn.Parameter(torch.zeros((1, action_dim)), requires_grad=True)
def forward(self, state: Tensor) -> Tensor:
return self.net(state).tanh()
def get_action(self, state: Tensor):
action_avg = self.net(state)
action_std = self.action_std_log.exp()
dist = Normal(action_avg, action_std)
action = dist.sample()
logprob = dist.log_prob(action).sum(1)
return action, logprob
def get_logprob_entropy(self, state: Tensor, action: Tensor):
action_avg = self.net(state)
action_std = self.action_std_log.exp()
dist = Normal(action_avg, action_std)
logprob = dist.log_prob(action).sum(1)
entropy = dist.entropy().sum(1)
return logprob, entropy
@staticmethod
def convert_action_for_env(action: Tensor) -> Tensor:
return action.tanh()
class CriticPPO(nn.Module):
def __init__(self, dims, state_dim: int, _action_dim: int):
super().__init__()
self.net = build_mlp(dims=[state_dim, *dims, 1])
def forward(self, state: Tensor) -> Tensor:
return self.net(state)
def build_mlp(dims) -> nn.Sequential: # MLP (MultiLayer Perceptron)
net_list = []
for i in range(len(dims) - 1):
net_list.extend([nn.Linear(dims[i], dims[i + 1]), nn.ReLU()])
del net_list[-1]
return nn.Sequential(*net_list)
class Config:
def __init__(self, agent_class=None, env_class=None, env_args=None):
self.env_class = env_class
self.env_args = env_args
if env_args is None:
env_args = {'env_name': None, 'state_dim': None, 'action_dim': None, 'if_discrete': None}
self.env_name = env_args['env_name'] # the name of environment. Be used to set 'cwd'.
self.state_dim = env_args['state_dim'] # vector dimension (feature number) of state
self.action_dim = env_args['action_dim'] # vector dimension (feature number) of action
self.if_discrete = env_args['if_discrete'] # discrete or continuous action space
self.agent_class = agent_class
self.gamma = 0.99 # discount factor of future rewards
self.reward_scale = 1.0 # an approximate target reward usually be closed to 256
self.gpu_id = int(0)
self.net_dims = (64, 32) # the middle layer dimension of MLP (MultiLayer Perceptron)
self.learning_rate = 6e-5
self.soft_update_tau = 5e-3
self.batch_size = int(128) # num of transitions sampled from replay buffer.
self.horizon_len = int(2000) # collect horizon_len step while exploring, then update network
self.buffer_size = None # ReplayBuffer size
self.repeat_times = 8.0 # repeatedly update network using ReplayBuffer
self.cwd = None # current working directory to save model. None means set automatically
self.break_step = +np.inf # break training if 'total_step > break_step'
self.eval_times = int(32) # number of times that get episodic cumulative return
self.eval_per_step = int(2e4) # evaluate the agent per training steps
def init_before_training(self):
if self.cwd is None: # set cwd (current working directory) for saving model
self.cwd = f'./{self.env_name}_{self.agent_class.__name__[5:]}'
os.makedirs(self.cwd, exist_ok=True)
def get_gym_env_args(env, if_print: bool) -> dict:
if {'unwrapped', 'observation_space', 'action_space', 'spec'}.issubset(dir(env)):
env_name = env.unwrapped.spec.id
state_shape = env.observation_space.shape
state_dim = state_shape[0] if len(state_shape) == 1 else state_shape
if_discrete = isinstance(env.action_space, gym.spaces.Discrete)
if if_discrete: # discrete action space
action_dim = env.action_space.n
elif isinstance(env.action_space, gym.spaces.Box): # continuous action space
action_dim = env.action_space.shape[0]
env_args = {'env_name': env_name, 'state_dim': state_dim, 'action_dim': action_dim, 'if_discrete': if_discrete}
print(f"env_args = {repr(env_args)}") if if_print else None
return env_args
def kwargs_filter(function, kwargs: dict) -> dict:
import inspect
sign = inspect.signature(function).parameters.values()
sign = {val.name for val in sign}
common_args = sign.intersection(kwargs.keys())
return {key: kwargs[key] for key in common_args} # filtered kwargs
def build_env(env_class=None, env_args=None):
if env_class.__module__ == 'gym.envs.registration': # special rule
env = env_class(id=env_args['env_name'])
else:
env = env_class(**kwargs_filter(env_class.__init__, env_args.copy()))
for attr_str in ('env_name', 'state_dim', 'action_dim', 'if_discrete'):
setattr(env, attr_str, env_args[attr_str])
return env
class AgentBase:
def __init__(self, net_dims, state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
self.state_dim = state_dim
self.action_dim = action_dim
self.gamma = args.gamma
self.batch_size = args.batch_size
self.repeat_times = args.repeat_times
self.reward_scale = args.reward_scale
self.soft_update_tau = args.soft_update_tau
self.states = None # assert self.states == (1, state_dim)
self.device = torch.device(f"cuda:{gpu_id}" if (torch.cuda.is_available() and (gpu_id >= 0)) else "cpu")
act_class = getattr(self, "act_class", None)
cri_class = getattr(self, "cri_class", None)
self.act = self.act_target = act_class(net_dims, state_dim, action_dim).to(self.device)
self.cri = self.cri_target = cri_class(net_dims, state_dim, action_dim).to(self.device) \
if cri_class else self.act
self.act_optimizer = torch.optim.Adam(self.act.parameters(), args.learning_rate)
self.cri_optimizer = torch.optim.Adam(self.cri.parameters(), args.learning_rate) \
if cri_class else self.act_optimizer
self.criterion = torch.nn.SmoothL1Loss()
@staticmethod
def optimizer_update(optimizer, objective: Tensor):
optimizer.zero_grad()
objective.backward()
optimizer.step()
@staticmethod
def soft_update(target_net: torch.nn.Module, current_net: torch.nn.Module, tau: float):
for tar, cur in zip(target_net.parameters(), current_net.parameters()):
tar.data.copy_(cur.data * tau + tar.data * (1.0 - tau))
class AgentPPO(AgentBase):
def __init__(self, net_dims, state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
self.if_off_policy = False
self.act_class = getattr(self, "act_class", ActorPPO)
self.cri_class = getattr(self, "cri_class", CriticPPO)
AgentBase.__init__(self, net_dims, state_dim, action_dim, gpu_id, args)
self.ratio_clip = getattr(args, "ratio_clip", 0.25) # `ratio.clamp(1 - clip, 1 + clip)`
self.lambda_gae_adv = getattr(args, "lambda_gae_adv", 0.95) # could be 0.80~0.99
self.lambda_entropy = getattr(args, "lambda_entropy", 0.01) # could be 0.00~0.10
self.lambda_entropy = torch.tensor(self.lambda_entropy, dtype=torch.float32, device=self.device)
def explore_env(self, env, horizon_len: int):
states = torch.zeros((horizon_len, self.state_dim), dtype=torch.float32).to(self.device)
actions = torch.zeros((horizon_len, self.action_dim), dtype=torch.float32).to(self.device)
logprobs = torch.zeros(horizon_len, dtype=torch.float32).to(self.device)
rewards = torch.zeros(horizon_len, dtype=torch.float32).to(self.device)
dones = torch.zeros(horizon_len, dtype=torch.bool).to(self.device)
ary_state = self.states[0]
get_action = self.act.get_action
convert = self.act.convert_action_for_env
for i in range(horizon_len):
state = torch.as_tensor(ary_state, dtype=torch.float32, device=self.device)
action, logprob = [t.squeeze(0) for t in get_action(state.unsqueeze(0))[:2]]
ary_action = convert(action).detach().cpu().numpy()
ary_state, reward, done, _ = env.step(ary_action)
if done:
ary_state = env.reset()
states[i] = state
actions[i] = action
logprobs[i] = logprob
rewards[i] = reward
dones[i] = done
self.states[0] = ary_state
rewards = (rewards * self.reward_scale).unsqueeze(1)
undones = (1 - dones.type(torch.float32)).unsqueeze(1)
return states, actions, logprobs, rewards, undones
def update_net(self, buffer):
with torch.no_grad():
states, actions, logprobs, rewards, undones = buffer
buffer_size = states.shape[0]
'''get advantages reward_sums'''
bs = 2 ** 10 # set a smaller 'batch_size' when out of GPU memory.
values = [self.cri(states[i:i + bs]) for i in range(0, buffer_size, bs)]
values = torch.cat(values, dim=0).squeeze(1) # values.shape == (buffer_size, )
advantages = self.get_advantages(rewards, undones, values) # advantages.shape == (buffer_size, )
reward_sums = advantages + values # reward_sums.shape == (buffer_size, )
del rewards, undones, values
advantages = (advantages - advantages.mean()) / (advantages.std(dim=0) + 1e-5)
assert logprobs.shape == advantages.shape == reward_sums.shape == (buffer_size,)
'''update network'''
obj_critics = 0.0
obj_actors = 0.0
update_times = int(buffer_size * self.repeat_times / self.batch_size)
assert update_times >= 1
for _ in range(update_times):
indices = torch.randint(buffer_size, size=(self.batch_size,), requires_grad=False)
state = states[indices]
action = actions[indices]
logprob = logprobs[indices]
advantage = advantages[indices]
reward_sum = reward_sums[indices]
value = self.cri(state).squeeze(1) # critic network predicts the reward_sum (Q value) of state
obj_critic = self.criterion(value, reward_sum)
self.optimizer_update(self.cri_optimizer, obj_critic)
new_logprob, obj_entropy = self.act.get_logprob_entropy(state, action)
ratio = (new_logprob - logprob.detach()).exp()
surrogate1 = advantage * ratio
surrogate2 = advantage * ratio.clamp(1 - self.ratio_clip, 1 + self.ratio_clip)
obj_surrogate = torch.min(surrogate1, surrogate2).mean()
obj_actor = obj_surrogate + obj_entropy.mean() * self.lambda_entropy
self.optimizer_update(self.act_optimizer, -obj_actor)
obj_critics += obj_critic.item()
obj_actors += obj_actor.item()
a_std_log = getattr(self.act, 'a_std_log', torch.zeros(1)).mean()
return obj_critics / update_times, obj_actors / update_times, a_std_log.item()
def get_advantages(self, rewards: Tensor, undones: Tensor, values: Tensor) -> Tensor:
advantages = torch.empty_like(values) # advantage value
masks = undones * self.gamma
horizon_len = rewards.shape[0]
next_state = torch.tensor(self.states, dtype=torch.float32).to(self.device)
next_value = self.cri(next_state).detach()[0, 0]
advantage = 0 # last_gae_lambda
for t in range(horizon_len - 1, -1, -1):
delta = rewards[t] + masks[t] * next_value - values[t]
advantages[t] = advantage = delta + masks[t] * self.lambda_gae_adv * advantage
next_value = values[t]
return advantages