forked from sostrader/thesisbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
executable file
·217 lines (145 loc) · 5.85 KB
/
model.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
import numpy as np
import pandas as pd
import random
from collections import deque
# tensorflow import
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM, BatchNormalization
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
import os
from settings import seq_len, EPOCHS, BATCH_SIZE, mt
from kerastuner.tuners import RandomSearch
from indicator import Indicators
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def classify(current,return_next):
if float(return_next) > float(current):
return 1
else:
return 0
def preprocess_df(df):
df = df.drop("return_next", 1)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
indexes = df.index
df_scaled = scaler.fit_transform(df)
df = pd.DataFrame(df_scaled,index = indexes)
sequential_data = []
prev_days = deque(maxlen=seq_len)
for i in df.values:
prev_days.append([n for n in i[:-1]])
if len(prev_days) == seq_len:
sequential_data.append([np.array(prev_days), i[-1]])
random.shuffle(sequential_data)
buys = []
sells = []
for seq, target in sequential_data:
if target == 0:
sells.append([seq, target])
elif target == 1:
buys.append([seq, target])
random.shuffle(buys)
random.shuffle(sells)
lower = min(len(buys), len(sells))
buys = buys[:lower]
sells = sells[:lower]
sequential_data = buys+sells
random.shuffle(sequential_data)
X = []
y = []
for seq, target in sequential_data:
X.append(seq)
y.append(target)
return np.array(X), y
def train_data(symbol,timeframe):
df = mt.history("EURUSD","M1",2)
print("traning",df)
df.isnull().sum().sum() # there are no nans
df.fillna(method="ffill", inplace=True)
df = df.loc[~df.index.duplicated(keep = 'first')]
# indicators
df = Indicators(df)
df = df.dropna()
df = df.fillna(method="ffill")
df = df.dropna()
df.sort_index(inplace = True)
df['target'] = list(map(classify, df['return'], df['return_next']))
print(df)
df.dropna(inplace=True)
df['target'].value_counts()
df.dropna(inplace=True)
df = df.astype('float32')
df = preprocess_df(df)
train_x, train_y = df
validation_x, validation_y = df
train_y = np.asarray(train_y)
validation_y = np.asarray(validation_y)
print(('%% of Class0 : %f Sell' % (np.count_nonzero(train_y == 0)/float(len(train_y)))))
print(('%% of Class1 : %f Buy' % (np.count_nonzero(train_y == 1)/float(len(train_y)))))
def build_model(hp):
model = Sequential()
model.add(LSTM(hp.Int('units', min_value=10, max_value=50, step=1), input_shape=(train_x.shape[1:]), return_sequences=True))
model.add(Dropout(0.1))
model.add(BatchNormalization())
model.add(LSTM(units=hp.Int('units',
min_value=10,
max_value=50,
step=1), return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(LSTM(units=hp.Int('units',
min_value=10,
max_value=50,
step=1)))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Dense(hp.Int('units',
min_value=10,
max_value=50,
step=1),
activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(2, activation='softmax'))
# Compile model
model.compile(
optimizer=Adam(
hp.Choice('learning_rate',
values=[1e-2])),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
tuner = RandomSearch(
build_model,
objective='val_accuracy',
max_trials=10,
executions_per_trial=1,
directory='TUN',
project_name='IQOTC')
# tuner.search_space_summary()
stop_early = EarlyStopping(monitor='val_loss', patience=15)
tuner.search(train_x,train_y,batch_size=BATCH_SIZE, epochs=EPOCHS,validation_split=0.2, verbose=1, callbacks=[stop_early]),
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]
print(f"""
The optimal number of units layer is {best_hps.get('units')}
and the optimal learning rate for the optimizer is {best_hps.get('learning_rate')}.
""")
filepath = "ThesisBrain"
# Build the model with the optimal hyperparameters and train it on the data for 50 epochs
model = tuner.hypermodel.build(best_hps)
history = model.fit(train_x, train_y,batch_size=BATCH_SIZE, epochs=EPOCHS, validation_split=0.2, verbose=1)
val_acc_per_epoch = history.history['val_accuracy']
best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1
print(('Best epoch: %d' % (best_epoch,)))
hypermodel = tuner.hypermodel.build(best_hps)
scores = model.evaluate(validation_x, validation_y, verbose=0)
print(("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)))
del model
del history
# Retrain the model
hypermodel.fit(validation_x, validation_y,batch_size=BATCH_SIZE, epochs=best_epoch, verbose=1)
hypermodel.save("models/{}.h5".format(filepath))
scores = hypermodel.evaluate(validation_x, validation_y, verbose=0)
print(("%s: %.2f%%" % (hypermodel.metrics_names[1], scores[1]*100)))
scores = scores[1]*100
return scores