This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR904_Model_SS60_July2019_EURUSD.py
218 lines (166 loc) · 7.74 KB
/
R904_Model_SS60_July2019_EURUSD.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
# this uses code for Deep Nexus version 1 (code R904) completed July 2019
import numpy as np
from keras import backend as K
from keras.models import load_model
import tensorflow as tf
from flask import request
from flask import jsonify
from flask import Flask
from flask_cors import CORS
import pandas as pd
from sklearn import metrics, preprocessing
# Oanda imports...(to be simplified later)
from oandapyV20 import API
import oandapyV20.endpoints.instruments as v20instruments
from Oanda_Token import token
from collections import OrderedDict
from waitress import serve
app = Flask(__name__)
CORS(app)
app.config['PROPAGATE_EXCEPTIONS'] = True
def smooth_L1_loss(y_true, y_pred):
return tf.losses.huber_loss(y_true, y_pred)
# this can be used as a metric. 1 is a perfect score; an exact match to the target
def coeff_determination(y_true, y_pred):
SS_res = K.sum(K.square(y_true-y_pred))
SS_tot = K.sum(K.square(y_true - K.mean(y_true)))
return (1 - SS_res/(SS_tot + K.epsilon()))
def SuperSmooth(ssdata, period=60):
'''
SuperSmooth - proposed by John Ehler to get rid of undersample noise.
'''
# close = df[['close']].values.ravel()
f = (1.414 * np.pi) / period
a = np.exp(-f)
c2 = 2 * a * np.cos(f)
c3 = - a * a
c1 = 1 - c2 - c3
ss = np.zeros(ssdata.shape)
for i in range(len(ssdata)):
if i < 2:
continue
else:
ss[i] = c1 * (ssdata[i] + ssdata[i - 1]) * 0.5 + c2 * ss[i - 1] + c3 * ss[i - 2]
return ss
def get_model():
global model
model = load_model('.hdf5',
custom_objects={'smooth_L1_loss': smooth_L1_loss, 'coeff_determination': coeff_determination})
model._make_predict_function()
print('Model loaded!')
# any incoming data preprocessing, do it here'
def DataFrameFactory(r, colmap=None, conv=None):
def convrec(r, m):
"""convrec - convert OANDA candle record.
return array of values, dynamically constructed, corresponding with config in mapping m.
"""
v = []
for keys in [x.split(":") for x in m.keys()]:
_v = r.get(keys[0])
for k in keys[1:]:
_v = _v.get(k)
v.append(_v)
return v
record_converter = convrec if conv is None else conv
column_map_ohlcv = OrderedDict([
('time', 'Date'),
('mid:o', 'Open'),
('mid:h', 'High'),
('mid:l', 'Low'),
('mid:c', 'Close'),
#('volume', 'Vol')
])
cmap = column_map_ohlcv if colmap is None else colmap
df = pd.DataFrame([list(record_converter(rec, cmap)) for rec in r.get('candles')])
df.columns = list(cmap.values())
# df.rename(columns=colmap, inplace=True) # no need to use rename, cmap values are ordered
df.set_index(pd.DatetimeIndex(df['Date']), inplace=True)
del df['Date']
df = df.apply(pd.to_numeric) # OANDA returns string values: make all numeric
return df
print('Loading Keras model...')
get_model()
@app.route('/eurusd', methods=['POST', 'GET'])
def prediction():
if request.method == 'GET':
# message = request.get_json(force=True)
# encoded = message['ticker']
# decoded = base64.b64decode(encoded)
# dataX = open(message)
# any incoming data preprocessing, do it here'
if __name__ == "__main__":
api = API(access_token=token)
params = {
"count": 600, # number of bars to download
"granularity": "M1" # timeframe
}
# instruments = ["USD_ZAR", "EUR_USD", "GBP_USD", "USD_JPY", "NZD_USD", "AUD_USD"]
instruments = ["EUR_USD"]
df = dict()
for instr in instruments:
try:
r = v20instruments.InstrumentsCandles(instrument=instr,
params=params)
api.request(r)
except Exception as err:
print("Error: {}".format(err))
exit(2)
else:
df.update({instr: DataFrameFactory(r.response)})
for I in instruments:
print(df[I].tail())
ticker = (df['EUR_USD'].iloc[:])
ssdata0 = ticker.Close
ssdata = np.array(ssdata0)
ticker['ss'] = SuperSmooth(ssdata, period=60)
ticker['target'] = np.log2(ticker.ss / ticker.ss.shift(180)).fillna(0.0001)
ticker['target1'] = np.log2(ticker.ss / ticker.ss.shift(90)).fillna(0.0001)
ticker['target2'] = np.log2(ticker.ss / ticker.ss.shift(1)).fillna(0.0001)
targetnp = np.array(ticker.target)
target1np = np.array(ticker.target1)
target2np = np.array(ticker.target2)
# inital pre-processing complete
data = np.column_stack((targetnp, target1np, target2np))
Xdata0 = np.delete(data, np.s_[0:185], axis=0) # cut off initial nan's due to preprocessing: ~60+180
# early scaling, can possibly be moved to after train/test split.
scaler = preprocessing.RobustScaler()
Xdata = scaler.fit_transform(Xdata0)
X = np.array(Xdata[:, :]) # selects the entire range of columns available
dX = []
n_pre = 180
n_post = 15
for i in range(len(X) - 179): # - 12):
dX.append(X[i:i + 180])
# print(i)
# print(dX)
# dY.append(Y[i + n_pre:i + n_pre + n_post])
dataX = np.array(dX)
# ------------------------
print('Printing dataX...')
print(dataX)
print('About to predict...')
predict = model.predict(dataX, batch_size=1000) # .tolist()
print('Prediction complete.')
# simplified reshape and no inverse scaling; remain scaled for trade logic
inverse_p = np.array(predict).reshape((len(predict), -1))
price_action = pd.DataFrame({
'p01': inverse_p[:, 0], 'p02': inverse_p[:, 1], 'p03': inverse_p[:, 2],
'p04': inverse_p[:, 3], 'p05': inverse_p[:, 4], 'p06': inverse_p[:, 5],
'p07': inverse_p[:, 6], 'p08': inverse_p[:, 7], 'p09': inverse_p[:, 8],
'p10': inverse_p[:, 9], 'p11': inverse_p[:, 10], 'p12': inverse_p[:, 11],
'p13': inverse_p[:, 12], 'p14': inverse_p[:, 13], 'p15': inverse_p[:, 14]})
price_action['p08s10'] = price_action.p08.shift(10) # index/column 15 in MT4
price_action['p06s3'] = price_action.p06.shift(3) # index/column 16 in MT4
price_action['p03s18'] = price_action.p03.shift(18) # index/column 17 in MT4
price_action['p01s5'] = price_action.p01.shift(5) # index/column 18 in MT4
price_action['p15s5'] = price_action.p15.shift(5) # index/column 19 in MT4
result1 = price_action.iloc[-1, :]
print(result1.shape)
print(result1)
result = result1.to_json()
return jsonify(result)
if __name__ == "__main__":
print(("* Loading Keras model and Flask starting server..."
"please wait until server has fully started"))
# waitress command to serve flask apps
serve(app, listen='127.0.0.5:80')