-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
161 lines (130 loc) · 6.2 KB
/
agent.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
import numpy as np
import random
import pandas as pd
import pandas_ta as ta
from tqdm import tqdm
from collections import deque
import matplotlib.pyplot as plt
import os
from keras.models import Model, load_model
from keras.layers import Dense, LSTM, Input, Activation
from keras.optimizers import Adam
from sklearn.metrics import mean_squared_error
from utilities import *
class TradeAgent:
def __init__(self, ticker, lookback = 30, features = 9):
self.lookback = lookback
self.features = features
self.input_shape = (lookback, features)
self.action_size = 1 #predict scaled close difference
self.stock = ticker
self.model_exists = False
if("model_" + ticker + "_" + str(lookback) + "lb.keras" in os.listdir('models')):
filename = "models/model_"+ticker+"_"+str(lookback)+"lb.keras"
self.model = load_model(filename)
self.model_exists = True
else:
self.model = self._model()
print("----------------------------------------------------------------Model Created----------------------------------------------------------------")
# To be used for evaluation only
self.X_test = None
self.Y_test = None
def _model(self):
input_layer = Input(self.input_shape)
lstm_layer = LSTM(150)(input_layer)
# dense_layer = Dense(64, activation = 'linear')(lstm_layer)
output_layer = Dense(1, activation = 'linear')(lstm_layer)
model = Model(inputs = input_layer, outputs = output_layer)
model.compile(optimizer = Adam(), loss = 'mse')
model.summary()
return model
def train(self, batch_size = 128, epochs = 100):
if(not self.model_exists):
X_train, Y_train, self.X_test, self.Y_test = preprocess(self.stock, self.lookback, self.features)
# print(X_train.shape, Y_train.shape, self.Y_test.shape, self.X_test.shape)
self.model.fit(x = X_train,
y = Y_train,
batch_size = batch_size,
epochs = epochs,
shuffle = True,
validation_split = 0.1)
print("----------------------------------------------------------------Model Trained----------------------------------------------------------------")
else:
print("Model already trained.")
def evaluate(self, test_size = 0, graph = True):
if not self.model_exists:
print("Model doesn't exist or is not trained.")
return None
if(self.X_test is None):
_, __, self.X_test, self.Y_test = preprocess(self.stock)
X_test, Y_test = self.X_test, self.Y_test
if(test_size != 0):
X_test = self.X_test[-test_size:]
Y_test = self.Y_test[-test_size:]
y_pred = self.model.predict(X_test)
mse = mean_squared_error(Y_test, y_pred)
print("Test size: ", X_test.shape[0])
print("Mean Squared Error: ", mse)
if(graph):
plt.figure(figsize=(10,6))
plt.plot(Y_test, color = 'black', label = 'Test')
plt.plot(y_pred, color = 'blue', label = 'pred')
plt.title('Stock Price Trend for '+ self.stock)
plt.xlabel('# of Test Days')
plt.ylabel('Scaled Price Change Value')
plt.legend()
plt.show()
print("Mean Squared Error: {}".format(mse))
def save_model(self):
if(not self.model_exists):
filename = "models/model_" + self.stock + "_" + str(self.lookback) + "lb.keras"
self.model.save(filename)
print("Model saved successfully at", filename)
self.model_exists = True
class DQNAgent:
def __init__(self, num_stocks):
self.state_size = (num_stocks, 1)
self.action_size = 3 #buy, sell, sit
self.replay_buffer = deque(maxlen = 50)
self.inventory = []
self.gamma = 0.9 # discount rate
self.epsilon = 1.0
self.epsilon_threshold = 0.01
self.epsilon_decay = 0.995
self.model = self._model()
def _model(self):
input_layer = Input(self.state_size)
dense1 = Dense(64, activation = 'relu')(input_layer)
dense2 = Dense(32, activation = 'relu')(dense1)
output = Dense(self.action_size, activation = 'linear')(dense2)
model = Model(inputs = input_layer, outputs = output)
model.compile(optimizer = Adam(), loss = 'mse')
model.summary()
return model
def store_transition(self, state, action, reward, next_state, done):
self.replay_buffer.append((state, action, reward, next_state, done))
def epsilon_greedy(self, state, eval = False):
if not eval and np.random.rand() <= self.epsilon:
return np.array([random.randrange(self.action_size) for _ in range(self.state_size[0])])
prediction = self.model.predict(state, verbose = 0)
for i, li in zip(range(4), self.inventory.values()):
if(len(li)== 0):
prediction[i,0,2] = 0 # if not already bought, dont expect selling reward
return np.array([np.argmax(prediction[i]) for i in range(len(prediction))])
def exp_replay(self, batch_size):
mini_batch = []
l = len(self.replay_buffer)
for i in range(max(0,l-batch_size),l):
mini_batch.append(self.replay_buffer[i])
for state, actions, reward, next_state, done in mini_batch:
if not done:
expect_q = self.model.predict(next_state, verbose = 0)
target_Q = (reward + self.gamma * np.amax(expect_q, axis = 2))
else:
target_Q = reward
Q_values = self.model.predict(state, verbose = 0)
for i in range(self.action_size):
Q_values[i, 0, actions[i]] = target_Q[i, 0]
self.model.fit(state, Q_values, epochs = 1, verbose = 0)
if(self.epsilon > self.epsilon_threshold):
self.epsilon *= self.epsilon_decay