-
Notifications
You must be signed in to change notification settings - Fork 1
/
stock2.py
274 lines (239 loc) · 9.26 KB
/
stock2.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
import sys
import datetime as datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
import yfinance as yf
import seaborn as sns
import time
com_lst = {'IBM':'IBM','RELIANCE':'RELIANCE.NS','SAP':'SAP','AMAZON':'AMZN'}
company = sys.argv[1]
amt=sys.argv[3]
for keys,values in com_lst.items():
if company==keys:
company=values
sns.set()
day = sys.argv[2]
now = datetime.datetime.now()
d = datetime.timedelta(days=int(day))
date = now - d
dat = date.strftime('%Y-%m-%d')
df = pdr.get_data_yahoo(str(company), start=dat).reset_index()
close = df.Close.values.tolist()
window_size = 30
class Agent:
POPULATION_SIZE = 15
SIGMA = 0.1
LEARNING_RATE = 0.03
def __init__(
self, model, money, max_buy, max_sell, close, window_size, skip
):
self.window_size = window_size
self.skip = skip
self.close = close
self.model = model
self.initial_money = money
self.max_buy = max_buy
self.max_sell = max_sell
self.es = Deep_Evolution_Strategy(
self.model.get_weights(),
self.get_reward,
self.POPULATION_SIZE,
self.SIGMA,
self.LEARNING_RATE,
)
def act(self, sequence):
decision, buy = self.model.predict(np.array(sequence))
return np.argmax(decision[0]), int(buy[0])
def get_reward(self, weights):
initial_money = self.initial_money
starting_money = initial_money
len_close = len(self.close) - 1
self.model.weights = weights
state = get_state(self.close, 0, self.window_size + 1)
inventory = []
quantity = 0
for t in range(0, len_close, self.skip):
action, buy = self.act(state)
next_state = get_state(self.close, t + 1, self.window_size + 1)
if action == 1 and initial_money >= self.close[t]:
if buy < 0:
buy = 1
if buy > self.max_buy:
buy_units = self.max_buy
else:
buy_units = buy
total_buy = buy_units * self.close[t]
initial_money -= total_buy
inventory.append(total_buy)
quantity += buy_units
elif action == 2 and len(inventory) > 0:
if quantity > self.max_sell:
sell_units = self.max_sell
else:
sell_units = quantity
quantity -= sell_units
total_sell = sell_units * self.close[t]
initial_money += total_sell
state = next_state
return ((initial_money - starting_money) / starting_money) * 100
def fit(self, iterations, checkpoint):
self.es.train(iterations, print_every = checkpoint)
def buy(self):
initial_money = self.initial_money
len_close = len(self.close) - 1
state = get_state(self.close, 0, self.window_size + 1)
starting_money = initial_money
states_sell = []
states_buy = []
inventory = []
quantity = 0
column_names = ["time", "buy_units", "total_buy","sell_units", "total_sell","initial_money"]
state_data = pd.DataFrame(columns = column_names)
for t in range(0, len_close, self.skip):
action, buy = self.act(state)
next_state = get_state(self.close, t + 1, self.window_size + 1)
if action == 1 and initial_money >= self.close[t]:
if buy < 0:
buy = 1
if buy > self.max_buy:
buy_units = self.max_buy
else:
buy_units = buy
total_buy = buy_units * self.close[t]
initial_money -= total_buy
inventory.append(total_buy)
quantity += buy_units
states_buy.append(t)
state_data = state_data.append({'time':t,'buy_units':buy_units,'total_buy':total_buy,'initial_money':initial_money},ignore_index=True)
# print(
# 'day %d: buy %d units at price %f, total balance %f'
# % (t, buy_units, total_buy, initial_money)
# )
elif action == 2 and len(inventory) > 0:
bought_price = inventory.pop(0)
if quantity > self.max_sell:
sell_units = self.max_sell
else:
sell_units = quantity
if sell_units < 1:
continue
quantity -= sell_units
total_sell = sell_units * self.close[t]
initial_money += total_sell
states_sell.append(t)
try:
invest = ((total_sell - bought_price) / bought_price) * 100
except:
invest = 0
# print(
# 'day %d, sell %d units at price %f, investment %f %%, total balance %f,'
# % (t, sell_units, total_sell, invest, initial_money)
# )
state_data = state_data.append({'time':t,'sell_units':sell_units,'total_sell':total_sell,'initial_money':initial_money},ignore_index=True)
state = next_state
invest = ((initial_money - starting_money) / starting_money) * 100
# print(
# '\ntotal gained %f, total investment %f %%'
# % (initial_money - starting_money, invest)
# )
# print(state_data)
close_price=pd.DataFrame([])
close_price['close']=close
close_price.to_csv('./assets/static/close.csv',index = False)
state_data.to_csv('./assets/static/state_data2.csv',index = False)
# plt.figure(figsize = (20, 10))
# plt.plot(close, label = 'true close', c = 'g')
# plt.plot(
# close, 'X', label = 'predict buy', markevery = states_buy, c = 'b'
# )
# plt.plot(
# close, 'o', label = 'predict sell', markevery = states_sell, c = 'r'
# )
# plt.legend()
# # plt.savefig('output/'+name+'.png')
# plt.show()
class Model:
def __init__(self, input_size, layer_size, output_size):
self.weights = [
np.random.randn(input_size, layer_size),
np.random.randn(layer_size, output_size),
np.random.randn(layer_size, 1),
np.random.randn(1, layer_size),
]
def predict(self, inputs):
feed = np.dot(inputs, self.weights[0]) + self.weights[-1]
decision = np.dot(feed, self.weights[1])
buy = np.dot(feed, self.weights[2])
return decision, buy
def get_weights(self):
return self.weights
def set_weights(self, weights):
self.weights = weights
class Deep_Evolution_Strategy:
def __init__(
self, weights, reward_function, population_size, sigma, learning_rate
):
self.weights = weights
self.reward_function = reward_function
self.population_size = population_size
self.sigma = sigma
self.learning_rate = learning_rate
def _get_weight_from_population(self, weights, population):
weights_population = []
for index, i in enumerate(population):
jittered = self.sigma * i
weights_population.append(weights[index] + jittered)
return weights_population
def get_weights(self):
return self.weights
def train(self, epoch = 100, print_every = 1):
lasttime = time.time()
for i in range(epoch):
population = []
rewards = np.zeros(self.population_size)
for k in range(self.population_size):
x = []
for w in self.weights:
x.append(np.random.randn(*w.shape))
population.append(x)
for k in range(self.population_size):
weights_population = self._get_weight_from_population(
self.weights, population[k]
)
rewards[k] = self.reward_function(weights_population)
rewards = (rewards - np.mean(rewards)) / np.std(rewards)
for index, w in enumerate(self.weights):
A = np.array([p[index] for p in population])
self.weights[index] = (
w
+ self.learning_rate
/ (self.population_size * self.sigma)
* np.dot(A.T, rewards).T
)
if (i + 1) % print_every == 0:
print(
'iter %d. reward: %f'
% (i + 1, self.reward_function(self.weights))
)
print('time taken to train:', time.time() - lasttime, 'seconds')
def get_state(data, t, n):
d = t - n + 1
block = data[d : t + 1] if d >= 0 else -d * [data[0]] + data[: t + 1]
res = []
for i in range(n - 1):
res.append(block[i + 1] - block[i])
return np.array([res])
model = Model(input_size = window_size, layer_size = 500, output_size = 3)
agent = Agent(
model = model,
money = int(amt),
max_buy = 5,
max_sell = 5,
close = close,
window_size = 30,
skip = 1,
)
agent.fit(iterations = 100, checkpoint = 10)
agent.buy()