-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathStockTradingEnv.py
191 lines (159 loc) · 6.36 KB
/
StockTradingEnv.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
from __future__ import annotations
import gym
import numpy as np
from numpy import random as rd
from gym import spaces
class StockTradingEnv(gym.Env):
def __init__(
self,
config,
gamma=0.99,
min_stock=1,
initial_capital=1e2,
buy_cost_pct=0,
sell_cost_pct=0,
reward_scaling=1e-1,
initial_stocks=None,
):
price_ary = config["price_array"]
tech_ary = config["tech_array"]
label_ary = config["label_array"]
if_train = config["if_train"]
self.price_ary = price_ary.astype(np.float32)
self.tech_ary = tech_ary.astype(np.float32)
self.label_ary = label_ary.astype(np.float32)
self.if_train = if_train
stock_dim = self.price_ary.shape[1]
self.gamma = gamma
self.min_stock = min_stock
self.buy_cost_pct = buy_cost_pct
self.sell_cost_pct = sell_cost_pct
self.reward_scaling = reward_scaling
self.initial_capital = initial_capital
self.initial_stocks = (
np.zeros(stock_dim, dtype=np.float32)
if initial_stocks is None
else initial_stocks
)
# reset()
self.day = None
self.amount = None
self.stocks = None
self.total_asset = None
self.gamma_reward = None
self.initial_total_asset = None
self.actions_memory = []
# environment information
self.env_name = "StockEnv"
# amount + (price, stock) * stock_dim + tech_dim
self.state_dim = 1 + 3 * stock_dim + self.tech_ary.shape[1]
self.stocks_cool_down = None
self.action_dim = 3
self.max_step = self.price_ary.shape[0] - 1
self.episode_return = 0.0
self.observation_space = gym.spaces.Box(
low=-np.inf, high=np.inf, shape=(self.state_dim,), dtype=np.float32
)
# self.action_space = gym.spaces.Discrete(3)
self.action_space = spaces.Box(low=0, high=1, shape=(stock_dim, self.action_dim))
def reset(self):
self.day = 0
price = self.price_ary[self.day]
if self.if_train:
self.stocks = (
self.initial_stocks + rd.randint(0, 64, size=self.initial_stocks.shape)
).astype(np.float32)
self.stocks_cool_down = np.zeros_like(self.stocks)
self.amount = (
self.initial_capital * rd.uniform(0.95, 1.05)
- (self.stocks * price).sum()
)
else:
self.stocks = self.initial_stocks.astype(np.float32)
self.stocks_cool_down = np.zeros_like(self.stocks)
self.amount = self.initial_capital
self.total_asset = self.amount + (self.stocks * price).sum()
self.initial_total_asset = self.total_asset
self.gamma_reward = 0.0
self.actions_memory = []
return self.get_state(price) # state
def step(self, actions):
actions = (actions).astype(int)
self.day += 1
price = self.price_ary[self.day]
# actions = self.label_ary[self.day]
self.stocks_cool_down += 1
min_action = int(self.min_stock) # stock_cd
# print(actions)
for index in np.where(actions <= -min_action)[0]: # sell_index:
if price[index] > 0: # Sell only if current asset is > 0
sell_num_shares = min(self.stocks[index], -actions[index])
self.stocks[index] -= sell_num_shares
self.amount += (
price[index] * sell_num_shares * (1 - self.sell_cost_pct)
)
self.stocks_cool_down[index] = 0
for index in np.where(actions >= min_action)[0]: # buy_index:
if (
price[index] > 0
): # Buy only if the price is > 0 (no missing data in this particular date)
# print(self.amount // price[index], actions)
buy_num_shares = min(self.amount // price[index], actions[index])
self.stocks[index] += buy_num_shares
self.amount -= (
price[index] * buy_num_shares * (1 + self.buy_cost_pct)
)
self.stocks_cool_down[index] = 0
self.actions_memory.append(actions)
state = self.get_state(price)
total_asset = self.amount + (self.stocks * price).sum()
reward = (total_asset - self.total_asset) * self.reward_scaling
self.total_asset = total_asset
self.gamma_reward = self.gamma_reward * self.gamma + reward
done = self.day == self.max_step
self.episode_return += reward
# if done:
# # reward = self.gamma_reward
# self.episode_return = total_asset / self.initial_total_asset
# # print(self.actions_memory)
if done:
return state, self.episode_return , done, False
else:
return state, reward, done, False
# return state, reward, done, False
def get_state(self, price):
amount = np.array(self.amount * (2**-6), dtype=np.float32)
scale = np.array(2**-6, dtype=np.float32)
return np.hstack(
(
amount,
price * scale,
self.stocks * scale,
self.stocks_cool_down,
self.tech_ary[self.day],
)
) # state.astype(np.float32)
def sample_from_env(seed, env, weights):
rd.seed(seed)
done = False
obs_, next_obs_, action_, reward_, done_ = [], [], [], [], []
obs = env.reset()
day = 0
while not done:
day += 1
action = weights[day].reshape(-1)
next_obs, reward, done, _, _ = env.step(action)
action = [0,1] if weights[day] else [1,0]
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_),
}