-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathStockPortfolioEnv.py
337 lines (285 loc) · 11.7 KB
/
StockPortfolioEnv.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
from __future__ import annotations
import random
import gym
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from gym import spaces
from gym.utils import seeding
from stable_baselines3.common.vec_env import DummyVecEnv
matplotlib.use("Agg")
class StockPortfolioEnv(gym.Env):
"""A single stock trading environment for OpenAI gym
Attributes
----------
df: DataFrame
input data
stock_dim : int
number of unique stocks
hmax : int
maximum number of shares to trade
initial_amount : int
start money
transaction_cost_pct: float
transaction cost percentage per trade
reward_scaling: float
scaling factor for reward, good for training
state_space: int
the dimension of input features
action_space: int
equals stock dimension
tech_indicator_list: list
a list of technical indicator names
turbulence_threshold: int
a threshold to control risk aversion
day: int
an increment number to control date
Methods
-------
_sell_stock()
perform sell action based on the sign of the action
_buy_stock()
perform buy action based on the sign of the action
step()
at each step the agent will return actions, then
we will calculate the reward, and return the next observation.
reset()
reset the environment
render()
use render to return other functions
save_asset_memory()
return account value at each time step
save_action_memory()
return actions/positions at each time step
"""
metadata = {"render.modes": ["human"]}
def __init__(
self,
df,
stock_dim,
hmax,
initial_amount,
transaction_cost_pct,
reward_scaling,
state_space,
action_space,
tech_indicator_list,
turbulence_threshold=None,
lookback=252,
day=0,
):
# super(StockEnv, self).__init__()
# money = 10 , scope = 1
self.day = day
self.lookback = lookback
self.df = df
self.stock_dim = stock_dim
self.hmax = hmax
self.initial_amount = initial_amount
self.transaction_cost_pct = transaction_cost_pct
self.reward_scaling = reward_scaling
self.state_space = state_space
self.action_space = action_space
self.tech_indicator_list = tech_indicator_list
# action_space normalization and shape is self.stock_dim
self.action_space = spaces.Box(low=0, high=1, shape=(self.action_space,))
# Shape = (34, 30)
# covariance matrix + technical indicators
self.observation_space = spaces.Box(
low=-np.inf,
high=np.inf,
# shape=(self.state_space + len(self.tech_indicator_list), self.state_space),
shape=(len(self.tech_indicator_list), self.state_space)
)
# load data from a pandas dataframe
self.data = self.df.loc[self.day, :]
# self.covs = self.data["cov_list"].values[0]
# self.state = np.append(
# np.array(self.covs),
# [self.data[tech].values.tolist() for tech in self.tech_indicator_list],
# axis=0,
# )
self.state = [self.data[tech].values.tolist() for tech in self.tech_indicator_list]
self.terminal = False
self.turbulence_threshold = turbulence_threshold
# initalize state: inital portfolio return + individual stock return + individual weights
self.portfolio_value = self.initial_amount
# memorize portfolio value each step
self.asset_memory = [self.initial_amount]
# memorize portfolio return each step
self.portfolio_return_memory = [0]
self.actions_memory = []
self.date_memory = [self.data.date.unique()[0]]
def step(self, actions):
self.terminal = self.day >= len(self.df.index.unique()) - 1
if self.terminal:
weights = actions
last_day_memory = self.data
self.state = [self.data[tech].values.tolist() for tech in self.tech_indicator_list]
# print(self.state)
# calcualte portfolio return
# individual stocks' return * weight
portfolio_return = sum(
((self.data.close.values / last_day_memory.close.values) - 1) * weights
)
# update portfolio value
last_portfolio_value = self.portfolio_value
new_portfolio_value = self.portfolio_value * (1 + portfolio_return)
self.portfolio_value = new_portfolio_value
# # save into memory
# self.portfolio_return_memory.append(portfolio_return)
# self.date_memory.append(self.data.date.unique()[0])
# self.asset_memory.append(new_portfolio_value)
# the reward is the new portfolio value or end portfolo value
self.reward = (new_portfolio_value - last_portfolio_value) / last_portfolio_value
df = pd.DataFrame(self.portfolio_return_memory)
df.columns = ["daily_return"]
# # plt.plot(df.daily_return.cumsum(), "r")
# plt.plot((df.daily_return+1).cumprod(), "r")
# (df.daily_return+1).cumprod().to_csv('results/cumulative_reward.csv', index=False, header=False)
# plt.savefig("results/cumulative_reward.png")
# plt.close()
# plt.plot(self.portfolio_return_memory, "r")
# plt.savefig("results/rewards.png")
# plt.close()
print("=================================")
print(f"begin_total_asset:{self.asset_memory[0]}")
print(f"end_total_asset:{self.portfolio_value}")
df_daily_return = pd.DataFrame(self.portfolio_return_memory)
df_daily_return.columns = ["daily_return"]
if df_daily_return["daily_return"].std() != 0:
sharpe = (
(252**0.5)
* df_daily_return["daily_return"].mean()
/ df_daily_return["daily_return"].std()
)
print("Sharpe: ", sharpe)
print("=================================")
return self.state, self.reward, self.terminal, {}
else:
# print("Model actions: ",actions)
# actions are the portfolio weight
# normalize to sum of 1
# if (np.array(actions) - np.array(actions).min()).sum() != 0:
# norm_actions = (np.array(actions) - np.array(actions).min()) / (np.array(actions) - np.array(actions).min()).sum()
# else:
# norm_actions = actions
# print(actions.sum())
weights = actions
if not np.isclose(np.sum(weights), 1.0):
weights = self.softmax_normalization(actions)
# print("Normalized actions: ", weights)
self.actions_memory.append(weights)
last_day_memory = self.data
# load next state
self.day += 1
self.data = self.df.loc[self.day, :]
# self.covs = self.data["cov_list"].values[0]
# self.state = np.append(
# np.array(self.covs),
# [self.data[tech].values.tolist() for tech in self.tech_indicator_list],
# axis=0,
# )
self.state = [self.data[tech].values.tolist() for tech in self.tech_indicator_list]
# print(self.state)
# calcualte portfolio return
# individual stocks' return * weight
portfolio_return = sum(
((self.data.close.values / last_day_memory.close.values) - 1) * weights
)
# update portfolio value
last_portfolio_value = self.portfolio_value
new_portfolio_value = self.portfolio_value * (1 + portfolio_return)
self.portfolio_value = new_portfolio_value
# save into memory
self.portfolio_return_memory.append(portfolio_return)
self.date_memory.append(self.data.date.unique()[0])
self.asset_memory.append(new_portfolio_value)
# the reward is the new portfolio value or end portfolo value
self.reward = (new_portfolio_value - last_portfolio_value) / last_portfolio_value
# print("Step reward: ", self.reward)
# self.reward = self.reward*self.reward_scaling
return self.state, self.reward, self.terminal, {}
def reset(self):
self.asset_memory = [self.initial_amount]
self.day = 0
self.data = self.df.loc[self.day, :]
# load states
# self.covs = self.data["cov_list"].values[0]
# self.state = np.append(
# np.array(self.covs),
# [self.data[tech].values.tolist() for tech in self.tech_indicator_list],
# axis=0,
# )
self.state = [self.data[tech].values.tolist() for tech in self.tech_indicator_list]
self.portfolio_value = self.initial_amount
# self.cost = 0
# self.trades = 0
self.terminal = False
self.portfolio_return_memory = [0]
self.actions_memory = []
self.date_memory = [self.data.date.unique()[0]]
return self.state
def render(self, mode="human"):
return self.state
def softmax_normalization(self, actions):
numerator = np.exp(actions)
denominator = np.sum(np.exp(actions))
softmax_output = numerator / denominator
return softmax_output
def save_asset_memory(self):
date_list = self.date_memory
portfolio_return = self.portfolio_return_memory
# print(len(date_list))
# print(len(asset_list))
df_account_value = pd.DataFrame(
{"date": date_list, "daily_return": portfolio_return}
)
return df_account_value
def save_action_memory(self):
# date and close price length must match actions length
date_list = self.date_memory
df_date = pd.DataFrame(date_list)
df_date.columns = ["date"]
action_list = self.actions_memory
df_actions = pd.DataFrame(action_list)
df_actions.columns = self.data.tic.values
df_actions.index = df_date.date
# df_actions = pd.DataFrame({'date':date_list,'actions':action_list})
return df_actions
def _seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def get_sb_env(self):
e = DummyVecEnv([lambda: self])
obs = e.reset()
return e, obs
def sample_from_env(i, env, weights):
random.seed(i)
obs = env.reset()
done = False
obs_ = []
next_obs_ = []
action_ = []
reward_ = []
done_ = []
day = 0
while not done:
action = list(weights[day])
day += 1
next_obs, reward, done, _ = env.step(action)
if done: break
obs_.append(obs)
next_obs_.append(next_obs)
action_.append(action)
reward_.append(reward)
done_.append(done)
obs = next_obs
return {
'observations': np.array(obs_),
'actions': np.array(action_),
'next_observations': np.array(next_obs_),
'rewards': np.array(reward_),
'terminals': np.array(done_),
}