-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdashboard.py
396 lines (378 loc) · 14.5 KB
/
dashboard.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
import os
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from data_operations import *
from reports_cols import root_cols_desc
from ticker_finder import find_similar_ticker
from utils import *
from fetch_data import fetch_all_reports
# Load ticker data from Yahoo Finance
yahoo_tickers = load_yahoo_tk_data()
# Create a Dash web application instance
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.SUPERHERO])
# Check if the database exists
if os.path.exists("data.db"):
print("Checking for new reports...")
# If the database exists, check for any new records
check_for_new_records()
else:
print("Database does not exist.")
print("Creating database.")
# If the database does not exist, fetch all reports to create the database
fetch_all_reports()
app.title = "Positions Explorer"
# Define the layout of the web application
app.layout = dbc.Container(
[
dbc.Row(
[
# Dropdown for selecting CFTC report
dbc.Col(
[
dbc.Label("Select CFTC report:"),
dcc.Dropdown(
id="report-dropdown",
options=get_reports_opts(), # Populate with options from get_reports_opts
value="report_legacy_futures_only", # Default value
placeholder="Select a report",
optionHeight=50,
className="dash-dropdown",
style={"borderRadius": "15px"},
),
],
className="col-lg-6 col-12 my-2 text-center",
),
# Dropdown for selecting commodity subgroup
dbc.Col(
[
dbc.Label("Select a commodity subgroup:"),
dcc.Dropdown(
id="commodities-subgroup-dropdown",
placeholder="Select a commodity group",
optionHeight=50,
className="dash-dropdown",
style={"borderRadius": "15px"},
),
],
className="col-lg-6 col-12 my-2 text-center",
),
# Dropdown for selecting market and exchange names
dbc.Col(
[
dbc.Label("Select a specific commodity market/commodity exchange name: "),
dcc.Dropdown(
id="market-and-exchange-names-dropdown",
placeholder="Select a market/exchange",
optionHeight=50,
className="dash-dropdown",
style={"borderRadius": "15px"},
),
],
className="col-12 my-2 text-center",
),
]
),
dbc.Row(
[
# RangeSlider for selecting date range
dbc.Col(
[
dbc.Label("Select min max dates:"),
dcc.RangeSlider(
id="year-slider",
step=1,
className="col-12 px-10 custom-range-slider",
),
],
className="px-2 text-center",
),
],
className="p-2",
),
dbc.Row(
[
# Dropdown for selecting price chart
dbc.Col(
[
dbc.Label("Select a price chart: ", id="price-chart-label"),
dcc.Dropdown(
id="chart-price-dropdown",
options=[
{"label": f"Price of {j.upper()}", "value": i}
for i, j in yahoo_tickers.items()
],
placeholder="Select price",
optionHeight=50,
className="dash-bootstrap",
style={"borderRadius": "15px"},
),
],
className="col-lg-6 col-12 my-2 text-center",
),
# Dropdown for selecting position types
dbc.Col(
[
dbc.Label("Position types : "),
dcc.Dropdown(
id="position-type",
multi=True,
placeholder="Select a position type",
className="dash-bootstrap",
style={"borderRadius": "15px"},
),
],
className="col-lg-6 col-12 my-2 text-center",
),
]
),
dbc.Row(
[
# Checklists and buttons for additional chart options
dbc.Col(
dbc.Checklist(
id="display-chart",
options=[
{"label": "Display additional price", "value": True},
],
value=[True],
inline=True,
labelStyle={
"display": "inline-block",
"margin": "0 0 0 15px",
"text-align": "center",
"font-size": "18px",
},
switch=True,
className="text-center",
),
className="col-lg-3 col-6 my-2",
),
dbc.Col(
dbc.Checklist(
id="add-price-line",
options=[
{"label": "Add price line", "value": True},
],
value=False,
inline=True,
labelStyle={
"display": "inline-block",
"margin": "0 0 0 15px",
"text-align": "center",
"font-size": "18px",
},
switch=True,
className="text-center",
),
className="col-lg-2 col-6 my-2",
),
# Buttons to show or hide correlation card
dbc.Col(
dbc.Button(
"Show correlations",
id="show-corr",
color="success",
style={
"display": "inline-block",
"max-height": "30px",
"border-radius": "15px",
"text-align": "center",
"margin": "0",
"padding": "0",
"width": "calc(90%)",
},
),
className="col-lg-2 col-3 my-2 text-center",
),
dbc.Col(
dbc.Button(
"Hide correlations",
id="hide-corr",
color="success",
style={
"display": "inline-block",
"max-height": "30px",
"border-radius": "15px",
"text-align": "center",
"margin": "0",
"padding": "0",
"width": "calc(90%)",
},
),
className="col-lg-2 col-3 my-2 text-center",
),
# Checklist to show different options
dbc.Col(
dbc.Checklist(
id="show-option",
options=[
{"label": i.capitalize(), "value": i}
for i in ["net", "long", "short"]
],
value=["net"],
inline=True,
labelStyle={
"display": "inline-block",
"margin": "0 0 0 15px",
"font-weight": "bold",
"font-size": "18px",
},
className="text-center",
),
className="col-lg-3 col-6 my-2",
),
]
),
dbc.Row(
[
# Display for correlation card and graphs
dbc.Col(
dbc.Card(
[html.Div(id="correlation-card", style={"display": "none"})],
),
className="col-12 my-2 mx-auto text-center",
),
dbc.Col(
dcc.Graph(
id="price-graph",
className="col-12 my-2",
),
className="col-12",
),
dbc.Col(
dcc.Graph(
id="positions-graph",
className="col-12 my-2",
),
className="col-12",
),
dbc.Col(
dcc.Graph(
id="percentage-graph",
className="col-12 my-2",
),
className="col-12",
),
],
className="p-2",
),
],
fluid=True,
)
# Callback to update commodities dropdown options based on the selected report
@app.callback(
Output("commodities-subgroup-dropdown", "options"),
Input("report-dropdown", "value"),
)
def update_commodities_dropdown(report):
if report is None:
return []
return get_commodities_subgroup_opts(report)
# Callback to update market dropdown options based on selected commodity and report
@app.callback(
Output("market-and-exchange-names-dropdown", "options"),
Input("commodities-subgroup-dropdown", "value"),
Input("report-dropdown", "value"),
)
def update_market_dropdown(selected_commodity_subgroup, selected_report):
if selected_report and not selected_commodity_subgroup:
return get_market_opts(report=selected_report)
elif selected_report and selected_commodity_subgroup:
return get_market_opts(selected_report, selected_commodity_subgroup)
else:
return [], []
# Callback to update various components including price chart label, slider, and more
@app.callback(
Output("price-chart-label", "children"),
Output("chart-price-dropdown", "value"),
Output("year-slider", "min"),
Output("year-slider", "max"),
Output("year-slider", "value"),
Output("year-slider", "marks"),
Input("market-and-exchange-names-dropdown", "value"),
Input("report-dropdown", "value"),
)
def update_year_slider_and_price_dropdown_value(selected, report):
if selected is None or report is None:
return "Select a price chart:", None, 0, 0, [0, 0], {}
name_market = json.loads(selected)["name_market"]
ticker_dropdown = find_similar_ticker(name_market, yahoo_tickers)
price_chart_label = (
"Price chart not found; may need to select one manually:"
if ticker_dropdown is None
else f"Price chart found: {ticker_dropdown}, you can adjust manually if needed"
)
min_y, max_y, values, marks = get_slider_opts(selected, report)
return price_chart_label, ticker_dropdown, min_y, max_y, values, marks
# Callback to update position types based on the selected report
@app.callback(
Output("position-type", "options"),
Output("position-type", "value"),
Input("report-dropdown", "value"),
)
def update_position_types(report):
if report is None:
return [], []
selected_report = report.split("_")[1]
categories = root_cols_desc.get(selected_report, {})
options = [{"label": label, "value": value} for value, label in categories.items()]
default_value = options[1]["value"] if options else None
return options, [default_value]
# Callback to toggle the visibility of the price graph based on user input
@app.callback(
Output("price-graph", "style"),
Input("display-chart", "value"),
)
def toggle_price_graph_visibility(chart_together_value):
return {"display": "none"} if chart_together_value != [True] else {}
# Callback to toggle the correlation card's visibility
@app.callback(
Output("correlation-card", "style"),
Input("show-corr", "n_clicks"),
Input("hide-corr", "n_clicks"),
)
def toggle_correlation_card(show_clicks, hide_clicks):
ctx = dash.callback_context
if not ctx.triggered:
return {"display": "none"}
button_id = ctx.triggered[0]["prop_id"].split(".")[0]
if button_id == "show-corr" and show_clicks:
return {"display": "block"}
elif button_id == "hide-corr" and hide_clicks:
return {"display": "none"}
else:
return dash.no_update
# Callback to update various graphs based on user input
@app.callback(
Output("price-graph", "figure"),
Output("positions-graph", "figure"),
Output("correlation-card", "children"),
Output("percentage-graph", "figure"),
Input("report-dropdown", "value"),
Input("market-and-exchange-names-dropdown", "value"),
Input("position-type", "value"),
Input("year-slider", "value"),
Input("show-option", "value"),
Input("chart-price-dropdown", "value"),
Input("add-price-line", "value"),
)
def update_graphs_callback(
report_type, market_commodity, positions, years, options, ticker, add_price
):
if report_type is None or market_commodity is None:
return dash.no_update, dash.no_update, dash.no_update, dash.no_update
return make_graphs_and_cards(
yahoo_tickers,
report_type,
market_commodity,
positions,
years,
options,
ticker,
add_price,
)
# Main entry point of the application
if __name__ == "__main__":
app.run(debug=False, port=2077)