-
Notifications
You must be signed in to change notification settings - Fork 2
/
fugle_realtime_websocket_api.py
352 lines (242 loc) · 10.7 KB
/
fugle_realtime_websocket_api.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python
# coding: utf-8
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output, State
import pandas as pd
import numpy as np
import datetime
import websocket
import sys
import trace
import threading
import time
import requests
import json
class create_Thread(threading.Thread):
def __init__(self, *args, **keywords):
threading.Thread.__init__(self, *args, **keywords)
self.killed = False
def start(self):
self.__run_backup = self.run
self.run = self.__run
threading.Thread.start(self)
def __run(self):
sys.settrace(self.globaltrace)
self.__run_backup()
self.run = self.__run_backup
def globaltrace(self, frame, event, arg):
if event == 'call':
return self.localtrace
else:
return None
def localtrace(self, frame, event, arg):
if self.killed:
if event == 'line':
raise SystemExit()
return self.localtrace
def kill(self):
self.killed = True
class chart_websocket_api():
def __init__(self, api_token):
self.api_token = api_token
def chart_websocket(self, symbol_id):
def on_message(ws, message):
self.chart_msg = json.loads(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
# if __name__ == "__main__":
websocket.enableTrace(False)
ws = websocket.WebSocketApp('wss://api.fugle.tw/realtime/v0/intraday/chart?symbolId=' + symbol_id +
'&apiToken=' + self.api_token,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
def get_chart_msg(self, symbol_id):
try:
symbol = self.chart_msg['data']['info']['symbolId']
if symbol_id != symbol:
while [i for i in threading.enumerate() if i.name == symbol+'chart'] != []:
[i for i in threading.enumerate() if i.name == symbol+'chart'][0].kill()
time.sleep(1)
print('kill complete!')
t = create_Thread(target=self.chart_websocket, args=[symbol_id], name=symbol_id+'chart')
t.start()
time.sleep(2)
else:
print('pass')
except (NameError, AttributeError):
t = create_Thread(target=self.chart_websocket, args=[symbol_id], name=symbol_id+'chart')
t.start()
time.sleep(2)
print('new')
def get_chart_data(self, n, symbol_id):
self.get_chart_msg(symbol_id)
now = datetime.datetime.now()
today = now.strftime('%Y-%m-%d')
close_time = datetime.datetime(now.year,now.month,now.day, 13, 30)
time_index = pd.date_range(start=f'{today} 09:00:00',
end=f'{today} 13:30:00', freq=f'{n}T', closed='right')
df_time = pd.DataFrame(time_index, columns=['at'])
ohlc = self.chart_msg['data']['chart']
df = pd.DataFrame(ohlc.values())
df['at'] = pd.DataFrame(ohlc.keys())
df['at'] = pd.to_datetime(df['at']).dt.tz_localize(None) + datetime.timedelta(hours=8)
df = df.set_index('at')
df = df.asfreq('1T')
df['close'] = df['close'].fillna(method='ffill')
df['volume'] = df['volume'].fillna(0)
df = df.fillna(axis=1, method='ffill')
df_ohlc = df.resample(f'{n}T', kind='period').agg({'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
'volume': 'sum'
})
df_ohlc.index = df_ohlc.index + datetime.timedelta(minutes=n-1)
df_ohlc = df_ohlc.to_timestamp().reset_index()
df_ohlc = pd.merge(df_time, df_ohlc, on='at', how='outer')
df_ohlc['at'] = df_ohlc['at'].apply(lambda x: x if x <= close_time else close_time)
df_ohlc['at'] = df_ohlc['at'].astype(str)
return df_ohlc
def plot_ohlc(self, df, rise_color, down_color):
df['at'] = df['at'].astype(str)
return {
'type':'candlestick',
'x':df['at'],
'open':df['open'],
'high':df['high'],
'low':df['low'],
'close':df['close'],
'name':'K線圖',
'increasing':{'line':{'color':rise_color}},
'decreasing':{'line':{'color':down_color}}
}
def plot_MA(self, df, n, line_color, line_width):
df[f'{n}MA'] = df['close'].rolling(n).mean()
return {
'type':'scatter',
'x':df['at'],
'y':df[f'{n}MA'],
'mode':'lines',
'line':{'color':line_color, 'width':line_width},
'name':f'{n}MA'
}
def plot_volume_bar(self, df, rise_color, down_color):
color = []
for i in range(len(df)):
try:
if df['close'][i] > df['open'][i]:
color.append(rise_color)
elif df['close'][i] < df['open'][i]:
color.append(down_color)
elif df['close'][i] == df['open'][i]:
if df['close'][i] > df['close'][i-1]:
color.append(rise_color)
elif df['close'][i] < df['close'][i-1]:
color.append(down_color)
elif df['close'][i] == df['close'][i-1]:
color.append(color[-1])
except (IndexError, KeyError):
color.append(rise_color)
return {
'type':'bar',
'x':df['at'],
'y':df['volume']/1000,
'marker':{'color':color},
'name':'volume_bar',
'xaxis':'x',
'yaxis':'y2'
}
class quote_websocket_api():
def __init__(self, api_token):
self.api_token = api_token
def quote_websocket(self, symbol_id):
def on_message(ws, message):
global quote_msg
quote_msg = json.loads(message)
#print(json.loads(message))
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
#if __name__ == "__main__":
websocket.enableTrace(False)
ws = websocket.WebSocketApp('wss://api.fugle.tw/realtime/v0/intraday/quote?symbolId=' + symbol_id +
'&apiToken=' + self.api_token,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
def get_quote_msg(self, symbol_id):
try:
symbol = quote_msg['data']['info']['symbolId']
if symbol_id != symbol:
while [i for i in threading.enumerate() if i.name == symbol+'quote'] != []:
[i for i in threading.enumerate() if i.name == symbol+'quote'][0].kill()
time.sleep(1)
print('kill complete!')
t = create_Thread(target=self.quote_websocket, args=[symbol_id], name=symbol_id+'quote')
t.start()
else:
print('pass')
except (NameError, AttributeError):
t = create_Thread(target=self.quote_websocket, args=[symbol_id], name=symbol_id+'quote')
t.start()
print('new')
def get_first_quote_data(self, message):
symbol = message['data']['info']['symbolId']
ask = message['data']['quote']['order']['bestAsks']
ask.reverse()
df_ask = pd.DataFrame(ask, columns=['price', 'unit']).rename(columns={'unit': 'ask_unit'})
bid = message['data']['quote']['order']['bestBids']
bid.reverse()
df_bid = pd.DataFrame(bid, columns=['unit', 'price']).rename(columns={'unit':'bid_unit'})
df_quote = pd.merge(df_ask, df_bid, on='price', how='outer')
df_quote = df_quote[['bid_unit', 'price', 'ask_unit']]
price_list = list(df_quote['price'])
return df_quote, price_list, symbol
def get_new_quote_data(self, message, df_quote):
df_quote2, price_list, symbol = self.get_first_quote_data(message)
df_quote = pd.concat([df_quote, df_quote2], axis=0).drop_duplicates(subset='price', keep='last')
df_quote = df_quote.sort_values('price', ascending=False).reset_index(drop=True)
return df_quote, price_list, symbol
def update_quote_data(self, input_symbol):
global df_quote, symbol
self.get_quote_msg(input_symbol)
try:
if input_symbol == symbol:
df_quote, price_list, symbol = self.get_new_quote_data(quote_msg, df_quote)
else:
time.sleep(1)
df_quote, price_list, symbol = self.get_first_quote_data(quote_msg)
except:
time.sleep(1)
df_quote, price_list, symbol = self.get_first_quote_data(quote_msg)
return df_quote, price_list, symbol
def plot_order_book(self, dataframe, price_list, symbol_id):
rows = []
for i in range(len(dataframe)):
row = []
for col in dataframe.columns:
value = dataframe.iloc[i][col]
if col == 'price':
if value not in price_list:
cell = html.Td(html.A(href='https://www.fugle.tw/ai/'+symbol_id, children=value,style={'color':'gray'}),
style={'font-size':16,'text-align':'center'})
elif value in price_list:
cell = html.Td(html.A(href='https://www.fugle.tw/ai/'+symbol_id, children=value),
style={'font-size':16,'text-align':'center'})
else:
cell = html.Td(children=value,style={'font-size':16,'text-align':'center'})
row.append(cell)
rows.append(html.Tr(row))
return html.Table(
[html.Tr([html.Th(col) for col in dataframe.columns],
style={'font-size':16,'text-align':'center', 'table-align':'center'})] +rows
)