forked from zefengw/conuhacks-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
449 lines (352 loc) · 16.1 KB
/
app.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import dash
from dash import html, dcc, Output, Input
import plotly.graph_objects as go
import plotly.express as px
from utils import *
import pandas as pd
from trade_duration import generate_duration_graph_df
from g1 import generateOptions, price, timestamp, symbol, remove_outliers
app = dash.Dash()
app.layout = html.Div(className="g1_container", children=[
html.Div(className="hero", style={"height":"90vh"}, children=[
html.Img(src="assets/logo2.png", className="logo"),
html.H2("Visualization deriving crucial trading insights", style={"color": "#CBCBCB", "padding-top": "15px", "font-size":"23px","transform":"translateY(-75px)"})
]),
html.Div(style={"height":"10vh"}),
html.Div(style={"height":"10vh"}),
# Graph 1
html.H1("Graph 1", className="g_h1 g1_h1"),
html.Div(className="g1_label", children=[
html.P(children=["Market: "], style={"color":"#ffffff", "margin": "10px"}),
dcc.Dropdown(id="market_selected",
className="dropdown",
options=[
{"label": "Aequitas", "value": "Aequitas"},
{"label": "Alpha", "value": "Alpha"},
{"label": "TSX", "value": "TSX"}
],
multi=False,
value="Aequitas",
style={'width':"200px", "background-color": "#D5D5D5"}
)]),
html.Div(className="g1_label", children=[
html.P(children=["Symbol: "], style={"color":"#ffffff", "margin": "10px"}),
dcc.Dropdown(id="symbol_selected",
className="dropdown",
options=generateOptions(),
multi=False,
value="All",
style={'width':"200px", "background-color": "#D5D5D5"}
)]),
html.Br(),
html.Br(),
dcc.Graph(id='graph1', figure={}, style={'height': '500px', "margin": "0px 30px 30px 30px"}),
html.Div(style={"height":"11vh"}),
html.Div(className="parralax", style={"height":"85vh"}, children=[
html.H4("Price evolution for given trade symbols can be analyzed with Graph 1. Since price evolution over small time intervals tends to follow continuous trends, these plots allow the detection of anomalies in the trading data. With continuous live data, this graph has the potential to be used to train predictive models for anomaly detection.", className="parralax_text")
]),
html.Div(style={"height":"10vh"}),
# Graph 2
html.H1("Graph 2", className="g_h1 g2_h1"),
html.P(children=["Market: "], style={"color":"#ffffff", "margin": "10px"}),
dcc.Dropdown(id='market_selected',
className="dropdown",
options=[
{'label': 'Aequitas', 'value': 'Aequitas'},
{'label': 'Alpha', 'value': 'Alpha'},
{'label': 'TSX', 'value': 'TSX'}],
multi=False,
value='Aequitas',
style={
'width':'220px'
}),
html.P(children=["Message type: "], style={"color":"#ffffff", "margin": "35px 0px 20px 10px"}),
# Don't know if we'll need all types of messages. Can change later.
html.Div(className="radio_div", children=[
dcc.RadioItems(id='message_selected',
className="radio",
options=[
{'label': ' NewOrderRequest', 'value': 'NewOrderRequest'},
{'label': ' NewOrderAcknowledged', 'value': 'NewOrderAcknowledged'},
{'label': ' CancelRequest', 'value': 'CancelRequest'},
{'label': ' Cancel', 'value': 'Cancel'},
{'label': ' Trade', 'value': 'Trade'}],
value='Trade')]),
dcc.Input(id='top_x', className="inputt", type='number', min=0, placeholder='# of Symbols'),
html.Br(),
html.Br(),
html.Br(),
dcc.Graph(
id='stock_distribution',
figure={}
),
html.Div(style={"height":"11vh"}),
html.Div(className="parralax", style={"height":"85vh"}, children=[
html.H4("Graph 2, in the form of a tree map, demonstrates the distribution of traded symbols in a given market. Allowing for at-a-glance comparisons, graph 2 can provide insight on the user portfolio efficiently and effectively.", className="parralax_text")
]),
html.Div(style={"height":"14vh"}),
# Graph 3
html.H1("Graph 3", className="g_h1 g3_h1"),
html.P(children=["Market: "], style={"color":"#ffffff", "margin": "10px"}),
dcc.Dropdown(["TSX", "Aequitas", "Alpha"], "Aequitas", id='exchange-dropdown-menu', style={"width": "220px"}, className="dropdown", ),
html.Br(),
html.P(children=["Transaction type: "], style={"color":"#ffffff", "margin": "10px"}),
html.Div(className="radio_div", children=[
dcc.RadioItems(options=[
{"label": " New Order Requests", "value": "NewOrder"},
{"label": " Cancel Requests", "value": "Cancel"}
],
className="radio",
value="NewOrder",
id='request-type'
)]),
html.Br(),
html.Br(),
dcc.Graph(id="duration-graph"),
dcc.Slider(10, 500, value=10, id="num-intervals-slider"),
html.Div(style={"height":"11vh"}),
html.Div(className="parralax", style={"height":"85vh"}, children=[
html.H4("Graph 3 shows aggregate data in the form of a heatmap to represent the distribution of order completion over the 4 minute period from 9:28:00 to 9:32:00. Order completion time peaks around 9:30:00 for all 3 exchanges, which corresponds to the peak in order requests at market opening.", className="parralax_text")
]),
html.Div(style={"height":"16vh"}),
# Graph 4
html.H1("Graph 4", className="g_h1 g4_h1"),
html.P(children=["Market: "], style={"color":"#ffffff", "margin": "10px"}),
dcc.Dropdown(id="dropdown", className="dropdown", options=[{'label': 'TSX', 'value': 'TSX'}, {'label': 'Aequitas', 'value': 'Aequitas'}, {
'label': 'Alpha', 'value': 'Alpha'}], value='TSX', style={'width':'220px', "margin-bottom": "20px"}),
html.Div(className="radio_div", children=[
dcc.RadioItems(
id = "radio",
className="radio",
options=[
{'label': 'New Order Request', 'value': 'NOR'},
{'label': 'Orders Filled', 'value': 'OF'}
],
value='NOR'
)
]),
html.Br(),
html.Div(className="graph_container", children=[dcc.Graph(id="time-volume-price-graph", figure={})]),
html.Div(className="parralax", style={"height":"85vh"}, children=[
html.H4("Graph 4 shows the relationship between the number of orders and prices in smaller time intervals between 9:28:00 and 9:32:00. The number of orders tends to increase as the price of stock options decrease in relation to its respective time interval.", className="parralax_text")
]),
])
# Graph 1 callbacks
@app.callback(
Output(component_id='graph1', component_property='figure'),
[Input(component_id='market_selected', component_property='value'), Input(component_id='symbol_selected', component_property='value')]
)
def update_graph(m_selected, s_selected):
#dff = dff[dff["Year"] == span_selected]
#dff = dff[dff["Affected by"] == "Varroa_mites"]
# Plotly Express
match m_selected:
case "Aequitas":
i, j = 0, 3917
case "Alpha":
i, j = 3918, 3949
case "TSX":
i, j = 3950, 45900
# Aequitas 0 - 3917
# Alpha 3918 - 3949
# TSX 3950 - 45900
copy_price = price[i:j]
copy_timestamp = timestamp[i:j]
copy_symbol = symbol[i:j]
copy2_price = []
copy2_timestamp = []
if s_selected != "All":
for index, value in enumerate(copy_symbol):
if value == s_selected:
copy2_price.append(copy_price[index])
copy2_timestamp.append(copy_timestamp[index])
else:
copy2_price = copy_price
copy2_timestamp = copy_timestamp
display_dt = []
for k in copy2_timestamp:
display_dt.append(epoch_to_datetime(int(k)))
if len(display_dt) == 0 or len(copy2_price) == 0:
fig = px.area(title="This symbol is not traded in the selected market")
else:
processed_data = remove_outliers(display_dt, copy2_price)
clean_df = processed_data[0]
outliers_df = processed_data[1]
fig = px.area(x=clean_df["Time"], y=clean_df["Price"], title=f"Order requests over time for {s_selected}",labels=dict(x="Time ", y="Price ($) "))
fig.add_scatter(x=outliers_df["Time"], y=outliers_df["Price"], showlegend=False, mode="markers")
#fig.add_trace(px.scatter(outliers_df))
fig.update_layout(paper_bgcolor="#303030",
plot_bgcolor="#303030",
font_color="#919191",
margin_pad=30,
)
fig.layout.yaxis.gridcolor = "#4A4A4A"
fig.layout.yaxis.dividercolor = "#4A4A4A"
fig.layout.xaxis.gridcolor = "#4A4A4A"
fig.layout.xaxis.gridcolor = "#4A4A4A"
return fig
# Graph 2 callbacks
@app.callback(
Output(component_id='stock_distribution', component_property='figure'),
Input(component_id='market_selected', component_property='value'),
Input(component_id='message_selected', component_property='value'),
Input(component_id='top_x', component_property='value')
)
def generate_treemap(m_selected, message, top_x):
if m_selected == 'Aequitas':
with open('data/AequitasData.json') as f:
data = json.load(f)
elif m_selected == 'Alpha':
with open('data/AlphaData.json') as f:
data = json.load(f)
elif m_selected == 'TSX':
with open('data/TSXData.json') as f:
data = json.load(f)
# Create a dictionary of successful trades where the key is the symbol and the value is the count
symbol_dict = {}
for data_point in data:
# Computing TRADED symbols
if data_point['MessageType'] == message:
# If the symbol is already in the dictionary, add to count
symbol = data_point['Symbol']
if symbol in symbol_dict.keys():
symbol_dict[symbol] += 1
# If symbol is not in the dict, set default to 1
else:
symbol_dict[symbol] = 1
# Plotting the data into tree map
if len(symbol_dict.keys()) == 0:
fig = px.treemap(names=['No symbols available'], parents=[m_selected],
title='Distribution of Stocks by Market ({})'.format(message),
labels={
'names': 'Symbol',
'values': 'Traded',
'parents': 'Market'
})
else:
# Might want to add a note if top_x is larger than num of keys
if top_x is not None and top_x <= len(symbol_dict.keys()):
sorted_sym_dict = dict(sorted(symbol_dict.items(), key=lambda x:x[1], reverse=True))
l = list(sorted_sym_dict.items())
while len(l) != top_x:
l.pop()
sorted_sym_dict = dict(l)
fig = px.treemap(names=sorted_sym_dict.keys(), parents=[m_selected] * top_x, values=sorted_sym_dict.values(), title='Distribution of Stocks by Market ({}, Top {} Symbols)'.format(message, top_x),
labels={
'names': 'Symbol',
'values': 'Traded',
'parents': 'Market'
})
else:
fig = px.treemap(names=symbol_dict.keys(), parents=[m_selected] * len(symbol_dict.keys()), values=symbol_dict.values(), title='Distribution of Stocks by Market ({})'.format(message),
labels={
'names': 'Symbol',
'values': 'Traded',
'parents': 'Market'
})
fig.update_layout(paper_bgcolor="#303030",
plot_bgcolor="#303030",
font_color="#919191",
margin_pad=10
)
return fig
# Graph 3 callbacks
@app.callback(
Output('duration-graph', 'figure'),
Input('num-intervals-slider', 'value'),
Input('exchange-dropdown-menu', 'value'),
Input('request-type', 'value')
)
def display_selected_intervals(selected_intervals, selected_exchange, request_type):
df = generate_duration_graph_df(selected_exchange, selected_intervals, request_type)
fig = go.Figure(data=go.Heatmap(
z=df["Number of Orders"],
x=df["Order Time"],
y=df["Response Time"],
colorscale='RdBu_r'))
fig.update_layout(paper_bgcolor="#303030",
title="Distribution of average request completion times",
xaxis_title="Time",
yaxis_title="Request completion time",
plot_bgcolor="#303030",
font_color="#919191",
margin_pad=30,
)
return fig
# Graph 4 callbacks
@app.callback(
Output("time-volume-price-graph", "figure"),
[Input("radio", "value"), Input("dropdown", "value")]
)
def update_graph(radio_value, dropdown_value):
if radio_value == 'NOR':
obj = load_json(dropdown_value + "Data.json" )
num_intervals = 100
end = END_EPOCH/(10**9)
start = START_EPOCH/10**9
diff = (end - start) / num_intervals
df = pd.DataFrame(columns=["Interval", "Price", "Number of Orders"])
intervals = []
volume = defaultdict(int)
prices = defaultdict(list)
for i in range(1, num_intervals + 1):
interval = str(start + i * diff)
intervals.append(interval)
for i in range(len(obj)):
if obj[i]["MessageType"] == "NewOrderRequest":
for j in intervals:
epoch_timestamp = int(obj[i]["TimeStampEpoch"]) / 10**9
if epoch_timestamp < float(j):
volume[j] += 1
prices[j].append(obj[i]["OrderPrice"])
break
avg_prices = defaultdict(float)
for i in prices:
avg_prices[i] = sum(prices[i]) / len(prices[i])
for i in intervals:
price = avg_prices[i]
num_orders = volume[i]
date_obj = epoch_seconds_to_datetime(int(float(i))).strftime("%H:%M:%S")
new_row = pd.DataFrame(
{"Interval": [date_obj], "Price": [price], "Number of Orders": [num_orders]})
df = pd.concat([new_row, df.loc[:]]).reset_index(drop=True)
fig = px.scatter_3d(df, x='Interval', y='Price', z='Number of Orders', height=750, width=750, opacity=0.7, title="Order volume and price evolution", template="plotly_dark")
fig.update_layout(title_font_color="#4e4e4e"),
return fig
elif radio_value == 'OF':
obj = load_json(dropdown_value + "DataByOrderID.json" )
num_intervals = 100
end = END_EPOCH/(10**9)
start = START_EPOCH/10**9
diff = (end - start) / num_intervals
df = pd.DataFrame(columns=["Interval", "Price", "Order Filled"])
intervals = []
volume = defaultdict(int)
prices = defaultdict(list)
for i in range(1, num_intervals + 1):
interval = str(start + i * diff)
intervals.append(interval)
for i in obj:
for j in range(len(obj[i])):
if obj[i][j]["MessageType"] == "Trade":
for k in intervals:
epoch_timestamp = int(obj[i][j]["TimeStampEpoch"]) / 10**9
if epoch_timestamp < float(k):
volume[k] += 1
prices[k].append(obj[i][j]["OrderPrice"])
break
avg_prices = defaultdict(float)
for i in prices:
avg_prices[i] = sum(prices[i]) / len(prices[i])
for i in intervals:
price = avg_prices[i]
num_orders = volume[i]
date_obj = epoch_seconds_to_datetime(int(float(i))).strftime("%H:%M:%S")
new_row = pd.DataFrame(
{"Interval": [date_obj], "Price": [price], "Order Filled": [num_orders]})
df = pd.concat([new_row, df.loc[:]]).reset_index(drop=True)
fig = px.scatter_3d(df, x='Interval', y='Price', z='Order Filled', opacity=0.7, title="Order volume and price evolution", template="plotly_dark")
fig.update_layout(title_font_color="#4e4e4e"),
return fig
if __name__ == "__main__":
app.run_server(debug=True)