-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
440 lines (380 loc) · 14.4 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
import time
import typing
from concurrent.futures import ThreadPoolExecutor
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from dash_table import DataTable
import numpy as np
import pandas as pd
import api
import prepare_data
import plots
def get_new_data():
"""Updates the global variable 'df' with new data"""
global df, balances
df = api.get_data("options_active")
# the status from the subgraph data will only change if
# unlock and unlockAll API is called. this is currently done manually!
# to address this I check for it and set samples with active status
# but expiration in the past (smaller than timestamp utc now) to EXPIRED
df = df[df["expiration"] >= pd.Timestamp.utcnow().tz_localize(None)]
df = prepare_data.get_projected_profit(df)
balances = prepare_data.get_pool_balances()
def get_historical_oi():
df_full = pd.read_parquet("df_full.parquet")
df_oi_hist = prepare_data.prepare_historical_open_interest(df_full)
return df_oi_hist
def update_expanding_oi():
global df_oi, dict_oi_expanding
today = pd.to_datetime("today").normalize()
df["amount_usd"] = df["amount"] * df["current_price"]
df["date"] = today
X = df.groupby(["date", "symbol"])[["amount", "amount_usd"]].sum().reset_index()
dict_oi_expanding[today] = X
df_oi_expanding = pd.concat(dict_oi_expanding).reset_index(drop=True)
df_oi = pd.concat([df_oi_hist, df_oi_expanding]).reset_index(drop=True)
def get_new_data_every(period=300):
"""Update the data every 300 seconds"""
while True:
get_new_data()
update_expanding_oi()
print("data updated")
time.sleep(period)
def make_layout():
return html.Div(
children=[
html.Div(
className="row",
children=[
html.Div(
className="four columns div-user-controls",
children=[
html.Div(
id="invisible-div-callback-trigger"
), # needed for the plots in combi with the auto update
html.H1("HEGIC OPTIONS ANALYTICS TOOL"),
dcc.Markdown(
"""
Interactive charts displaying active ETH/WBTC option amount (bubble size) updated every 5min from [*subgraph*](https://thegraph.com/explorer/subgraph/ppunky/hegic-v888).
"""
),
html.Div(html.H2("SYMBOL")),
html.Div(
className="div-for-radio",
children=[
dcc.RadioItems(
id="symbol",
options=[
{"label": "WBTC", "value": "WBTC"},
{"label": "ETH", "value": "ETH"},
],
value="WBTC", # default
labelStyle={"display": "inline-block"},
className="dropdown_selector",
),
],
),
html.Div(html.H2("PERIOD OF HOLDING (IN DAYS)")),
html.Div(
className="div-for-dropdown",
children=[
dcc.Dropdown(
id="period",
options=[
{"label": "1", "value": "1"},
{"label": "7", "value": "7"},
{"label": "14", "value": "14"},
{"label": "21", "value": "21"},
{"label": "28", "value": "28"},
],
value=["1", "7", "14", "21", "28"], # default
multi=True,
className="dropdown_selector",
),
],
),
html.Div(html.H2("SELECT OPTION-SIZE BY DECILE")),
html.Div(
className="div-for-slider",
children=[
dcc.RangeSlider(
id="amounts",
min=0,
max=10,
step=None,
marks={
int(i): str(i) for i in np.arange(0, 11, 1)
},
value=[1, 10],
allowCross=False,
className="dropdown_selector",
),
],
),
html.Div(
html.P(
"Deciles 0-1 covers the lowest 10% of options, deciles 9-10 the top 10% of options etc."
)
),
html.Div(html.H2("SEARCH BY OPTION ID or ACCOUNT")),
html.Div(
className="div-for-input",
children=[
dcc.Input(
id="id",
type="text",
placeholder="ID or Account",
className="input_selector",
multiple=False,
)
],
),
html.Div(
html.P(
"Clear the search box to return to options overview."
)
),
html.Div(html.H2("GENERAL INFO")),
html.Div(
className="div-for-dropdown", # to shorten the distance btw header and text
children=[
html.P(
"Click-and-select directly on the plots to filter to a specific date range/cluster of options."
)
],
)
],
),
html.Div(
className="eight columns div-for-charts bg-grey",
children=[
dbc.Row(
dbc.Col(
dcc.Graph(
id="chart2d_bubble",
config={"displayModeBar": False},
),
xs=12,
xl=12, # xs is for phones to use the full width of the device, need xl in here as well to make sure the layout for large screens is being kept as defined in the css
),
),
dbc.Row(
[
dbc.Col(
dcc.Graph(
id="chart2d_pnl_pct_change",
config={"displayModeBar": False},
),
xs=12,
xl=6,
),
dbc.Col(
dcc.Graph(
id="chart2d_pnl",
config={"displayModeBar": False},
),
xs=12,
xl=6,
),
]
),
dbc.Row(
[
dbc.Col(
dcc.Graph(
id="chart2d_balance",
config={"displayModeBar": False},
),
xs=12,
xl=6,
),
dbc.Col(
dcc.Graph(
id="chart2d_putcall",
config={"displayModeBar": False},
),
xs=12,
xl=6,
),
]
),
dbc.Row(
dbc.Col(
dcc.Graph(
id="chart2d_open_interest",
config={"displayModeBar": False},
)
)
),
],
),
],
)
]
)
# Initialise the app
# meta tags will only be applied to XS devices (mobile phones)
app = dash.Dash(
__name__,
meta_tags=[
{
"name": "viewport",
"content": "width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0",
}
],
)
# for gunicorn
server = app.server
# get initial data
get_new_data()
# calculate historical OI (we do this once, and then append the current day whos values
# get updated every 5min)
df_oi_hist = get_historical_oi()
dict_oi_expanding = {}
update_expanding_oi()
# # we need to set layout to be a function so that for each new page load
# # the layout is re-created with the current data, otherwise they will see
# # data that was generated when the Dash app was first initialised
app.layout = make_layout
@app.callback(
Output("chart2d_bubble", "figure"),
[
Input("symbol", "value"),
Input("period", "value"),
Input("amounts", "value"),
Input("id", "value"),
Input("invisible-div-callback-trigger", "children"),
],
)
def chart2d_bubble(
symbol: str,
period: str,
amounts: typing.List[int],
id_: str,
_,
):
global df
X = df.copy()
X, bubble_size, current_price, current_iv = prepare_data.prepare_bubble(
X, symbol, period, amounts
)
if id_ is not None and len(id_) > 0:
if len(id_) >= 40:
X = X[X["Account"].str.lower() == id_.lower()]
else:
X = X[X["Option ID"] == id_]
fig = plots.plot_bubble(
X=X,
bubble_size=bubble_size,
current_price=current_price,
current_iv=current_iv,
symbol=symbol,
)
return fig
@app.callback(
Output("chart2d_pnl", "figure"),
[
Input("chart2d_bubble", "relayoutData"),
Input("symbol", "value"),
Input("period", "value"),
Input("amounts", "value"),
Input("id", "value"),
Input("invisible-div-callback-trigger", "children"),
],
)
def chart2d_pnl(
relayoutData: dict,
symbol: str,
period: str,
amounts: typing.List[int],
id_: str,
_,
):
global df, balances
X = df.copy()
agg = prepare_data.prepare_pnl(X, symbol, period, amounts, relayoutData, id_)
fig = plots.plot_pnl(agg=agg, balances=balances, symbol=symbol)
return fig
@app.callback(
Output("chart2d_balance", "figure"),
[
Input("symbol", "value"),
Input("invisible-div-callback-trigger", "children"),
],
)
def chart2d_balance(
symbol: str,
_,
):
global balances
fig = plots.plot_pool_balance(balances, symbol)
return fig
@app.callback(
Output("chart2d_putcall", "figure"),
[
Input("symbol", "value"),
Input("invisible-div-callback-trigger", "children"),
],
)
def chart2d_putcall(
symbol: str,
_,
):
global df
fig = plots.plot_put_call_ratio(df, symbol)
return fig
@app.callback(
Output("chart2d_pnl_pct_change", "figure"),
[
Input("chart2d_bubble", "relayoutData"),
Input("symbol", "value"),
Input("period", "value"),
Input("amounts", "value"),
Input("invisible-div-callback-trigger", "children"),
],
)
def chart2d_pnl_pct_change(
relayoutData: dict,
symbol: str,
period: str,
amounts: typing.List[int],
_,
):
global df, balances
X, current_price = prepare_data.prepare_pnl_pct_changes(
df,
balances,
relayoutData,
symbol,
period,
amounts,
)
fig = plots.plot_pnl_pct_change(X, current_price)
return fig
@app.callback(
Output("chart2d_open_interest", "figure"),
[
Input("symbol", "value"),
Input("invisible-div-callback-trigger", "children"),
],
)
def chart2d_open_interest(
symbol: str,
_,
):
"""
given that this is static its better to calcuate this with every n-th update once
instead of on every user interaction
"""
global df_oi
fig = plots.plot_open_interest(df_oi, symbol)
return fig
# for the data refresh
executor = ThreadPoolExecutor(max_workers=1)
executor.submit(get_new_data_every)
# Run the app
if __name__ == "__main__":
app.run_server()