forked from Gab0/japonicus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart_web.py
332 lines (293 loc) · 11 KB
/
chart_web.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import datetime
import numpy as np
import pandas as pd
import json
import os
import quantmod as qm
import flask
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
from flask_caching import Cache
#from plotInfo import plotEvolutionSummary
import gekkoWrapper
import Settings
import coreFunctions
import evolution_bayes
gsettings = Settings.getSettings()['global']
settings = Settings.getSettings()['bayesian']
MA_SMA, MA_EMA, MA_WMA, MA_DEMA, MA_TEMA, MA_TRIMA, MA_KAMA, MA_MAMA, MA_T3 = range(9)
rename = {
"DEMA": {
"long": "timeperiod",
},
"MACD": {
"short": "fastperiod",
"long": "slowperiod",
"signal": "signalperiod",
},
"PPO": {
"short": "fastperiod",
"long": "slowperiod",
"signal": "signalperiod",
},
"RSI": {
"interval": "timeperiod",
},
"StochRSI": {
"interval": "timeperiod",
},
"CCI": {
"interval": "timeperiod",
},
}
indicators = rename.keys()
def talib_dict(params):
# dict key rename
newparams = {}
for k in rename.keys():
newparams[k] = {}
if k == "STOCHRSI":
k = "StochRSI"
for old, new in rename[k.upper()].items():
newparams[k.upper()][new] = params[k].pop(old)
# add matype
newparams["PPO"]["matype"] = MA_EMA
#newparams["STOCHRSI"]["matype"] = MA_EMA
return newparams
def run_server():
# Setup the app
server = flask.Flask(__name__)
#server.secret_key = os.environ.get('secret_key', 'secret')
app = dash.Dash(__name__, server=server, csrf_protect=False)
app.scripts.config.serve_locally = False
dcc._js_dist[0]['external_url'] = 'https://cdn.plot.ly/plotly-finance-1.28.0.min.js'
# Setup config
responses, configs = get_json()
def setup_config(filename=None):
if filename != None and filename in responses:
config_filename = filename.replace("response", "config")
res = load_json(filename)
gekko_config = load_json(config_filename)
else:
res = load_json(responses[-1])
gekko_config = load_json(configs[-1])
filename = gsettings['configFilename']
configjs = Settings.get_configjs(filename)
config = {k:v for k,v in configjs.items() if k in indicators}
config2 = {k:v for k,v in gekko_config["gekkoConfig"].items() if k in indicators}
config.update(config2.copy())
strategy = gekko_config["gekkoConfig"]["tradingAdvisor"]["method"]
return strategy, config, res
# Setup chart
def setup_chart(res):
candles = pd.DataFrame.from_dict(res['candles'])
candles["start"] = pd.to_datetime(candles["start"])
candles.index = candles["start"]
trades = pd.DataFrame.from_dict(res['trades'])
trades["start"] = pd.to_datetime(trades["date"])
trades["color"] = 'rgba(0, 0, 0, 0.)'
trades["symbol"] = 'triangle-down'
trades.loc[trades.action.str.match("buy"), "color"] = 'rgba(255, 182, 193, .5)'
trades.loc[trades.action.str.match("sell"), "color"] = 'rgba(182, 193, 255, .5)'
trades.loc[trades.action.str.match("buy"), "symbol"] = 'triangle-up'
trade_scatter = dict(
x=trades["start"],
y=trades["price"],
name=trades["action"],
mode="markers",
marker = dict(
symbol = trades["symbol"],
size = 15,
color = trades["color"],
showscale=True,
)
)
return candles, trade_scatter
strategy, config, res = setup_config()
candles, trade_scatter = setup_chart(res)
# Add caching
cache = Cache(app.server, config={'CACHE_TYPE': 'simple'})
timeout = 60 * 60 # 1 hour
# Controls
src = dict(
index = 'start',
op = 'open',
hi = 'high',
lo = 'low',
cl = 'close',
aop = None,
ahi = None,
alo = None,
acl = None,
vo = 'volume',
di = None,
)
logs = responses
logs = [dict(label=str(log), value=str(log))
for log in logs]
# Dynamic binding
functions = dir(qm.ta)[9:-4]
functions = [dict(label=str(function[4:]), value=str(function))
for function in functions]
# Layout
app.layout = html.Div(
[
html.Div([
html.H2(
'gekkoJaponicus Charts',
style={'padding-top': '20', 'text-align': 'center'}
),
html.Div([
html.Label('Select log:'),
dcc.Dropdown(
id='dropdown',
options=logs,
value=str(responses[0]),
)],
style={
'width': '510', 'display': 'inline-block',
'padding-left': '40', 'margin-bottom': '20'}
),
html.Div([
html.Label('Select technical indicators:'),
dcc.Dropdown(
id='multi',
options=functions,
multi=True,
value=["add_"+strategy.upper()],
)],
style={
'width': '510', 'display': 'inline-block',
'padding-right': '40', 'margin-bottom': '20'}
),
]),
html.Div([
html.Label('Specify parameters of technical indicators:'),
dcc.Input(
id='arglist',
style={'height': '32', 'width': '1020'},
value=json.dumps(config),
)],
id='arg-controls',
style={'display': 'none'}
),
dcc.Graph(id='output')
],
style={
'width': '1100',
'margin-left': 'auto',
'margin-right': 'auto',
'font-family': 'overpass',
'background-color': '#F3F3F3'
}
)
@app.callback(Output('arg-controls', 'style'), [Input('multi', 'value')])
def display_control(multi):
if not multi:
return {'display': 'none'}
else:
return {'margin-bottom': '20', 'padding-left': '40'}
@cache.memoize(timeout=timeout)
@app.callback(Output('output', 'figure'), [Input('dropdown', 'value'),
Input('multi', 'value'),
Input('arglist', 'value')])
def update_graph_from_dropdown(dropdown, multi, arglist):
# Get Quantmod Chart
print('Loading')
strategy, config, res = setup_config(dropdown)
candles, trade_scatter = setup_chart(res)
ch = qm.Chart(candles, src=src)
# Get functions and arglist for technical indicators
if arglist:
for function in multi:
try:
config = talib_dict(json.loads(arglist))
indicator = function.split("_")[1]
newargs = config[indicator]
# Dynamic calling
fn = getattr(qm, function)
fn(ch, **newargs)
except Exception as e:
print(e)
getattr(qm, function)(ch)
pass
else:
for function in multi:
# Dynamic calling
getattr(qm, function)(ch)
fig = ch.to_figure(width=1100)
# hack figure
index = 0
for i in range(len(fig["layout"].keys())):
axis = "yaxis"+str(i)
if axis in fig["layout"]:
index = i + 1
yrange = [candles["low"].min(), candles["high"].max()]
fig["layout"]["yaxis"]["range"] = yrange
fig["layout"]["yaxis"+str(index)] = fig["layout"]["yaxis2"]
fig["layout"]["plot_bgcolor"] = 'rgba(0, 0, 0, 0.00)'
trade_scatter["yaxis"] = "y1"
fig["data"].append(trade_scatter)
return fig
# External css
external_css = ["https://fonts.googleapis.com/css?family=Overpass:400,400i,700,700i",
"https://cdn.rawgit.com/plotly/dash-app-stylesheets/c6a126a684eaaa94a708d41d6ceb32b28ac78583/dash-technical-charting.css"]
for css in external_css:
app.css.append_css({"external_url": css})
# Run the Dash app
if __name__ == '__main__':
app.server.run(debug=True)
#app.server.run()
def get_json():
files1 = os.path.join(gsettings["save_dir"], '*_response.json')
files2 = os.path.join(gsettings["save_dir"], '*_config.json')
response_files = list(filter(os.path.isfile, glob.glob(files1)))
response_files.sort(key=lambda x: -os.path.getmtime(x))
config_file = list(filter(os.path.isfile, glob.glob(files2)))
config_file.sort(key=lambda x: -os.path.getmtime(x))
return response_files, config_file
def load_json(filename):
f = open(filename, "r")
result = json.loads(f.read())
f.close
return result
def create_first_chart():
print("log file not found: try to fetch")
strategy = settings["Strategy"]
deltaDays = settings['deltaDays']
filename = gsettings['configFilename']
configjs = Settings.get_configjs(filename)
watch = settings["watch"]
dateset = gekkoWrapper.getAvailableDataset(watch)
daterange = coreFunctions.getRandomDateRange(dateset, deltaDays=deltaDays)
config = evolution_bayes.compressing_flatten_dict(configjs[strategy], strategy)
config["watch"] = watch
gekko_config = gekkoWrapper.createConfig(config, daterange)
res = evolution_bayes.EvaluateRaw(watch, daterange, configjs[strategy], strategy)
score = res['report']['relativeProfit']
filename = "_".join([watch["exchange"], watch["currency"], watch["asset"], strategy, datetime.datetime.now().strftime('%Y%m%d_%H%M%S'), str(score)])
save_dir = gsettings["save_dir"]
json_filename = os.path.join(save_dir, filename) + "_config.json"
json2_filename = os.path.join(save_dir, filename) + "_response.json"
if not os.path.exists(save_dir):
os.mkdir(save_dir)
f = open(json_filename, "w")
f.write(json.dumps(gekko_config, indent=2))
f.close()
print("Saved: " + json_filename)
f = open(json2_filename, "w")
f.write(json.dumps(res, indent=2))
f.close()
print("Saved: " + json2_filename)
if __name__ == '__main__':
res, config = get_json()
if len(res) > 0 and len(config) > 0:
run_server()
else:
create_first_chart()
run_server()