-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.py
750 lines (672 loc) · 22 KB
/
main.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
import dash
from dash import html
from dash import dcc
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
import business_logic as bl
import layout_configs as lc
import support_functions as sf
#############################################################################
# Style modifications
#############################################################################
CONTENT_STYLE = {
"margin-left": "2rem",
"margin-right": "2rem",
}
TEXT_STYLE = {"textAlign": "center"}
DROPDOWN_STYLE = {"textAlign": "left"}
#############################################################################
# Content
#############################################################################
# Reference cards for bottom disclosures
# References relating to DEACOT report
deacot_reference_card = (
dbc.Card(
[
dbc.CardBody(
[
dbc.Button("?", id="deacot_card_open", color="dark"),
dbc.Modal(
[
dbc.ModalHeader("DEACOT Report References"),
dbc.ModalBody(
[
html.P(
"Speculators are traders with a commercial interest in the underlying commodity"
),
html.P(
"Funds are traders with no commercial interest in the underlying commodity"
),
html.P(
"Others / Non-Reporting are aggregated traders not required to individually register positions"
),
html.P(
"Dashed lines signify short position levels."
),
html.P("Data retrieved from CFTC.gov:"),
dbc.CardLink(
"DEACOT Report",
href="https://www.cftc.gov/files/dea/history/deacot2021.zip",
),
]
),
dbc.ModalFooter(
dbc.Button(
"Close",
id="deacot_card_close",
className="ml-auto",
)
),
],
id="deacot_card_modal",
),
]
),
],
style={"width": "5rem"},
),
)
# References relating to Disaggregation report
da_reference_card = (
dbc.Card(
[
dbc.CardBody(
[
dbc.Button("?", id="da_reference_card_open", color="dark"),
dbc.Modal(
[
dbc.ModalHeader("Disaggregation Report References"),
dbc.ModalBody(
[
html.P(
"A “producer / merchant / processor / user” is an entity that predominantly engages in the production, processing, packing or handling of a physical commodity."
),
html.P(
"A “swap dealer” is an entity that deals primarily in swaps for a commodity. The swap dealer counterparties may be speculative traders, like hedge funds, or traditional commercial clients."
),
html.P(
"A “money manager” is a registered commodity trading advisor (CTA); a registered commodity pool operator (CPO); or an unregistered fund identified by CFTC."
),
html.P(
"Every other reportable trader that is not placed into one of the other three categories is placed into the “other reportables” category."
),
html.P("Data retrieved from CFTC.gov:"),
dbc.CardLink(
"Disaggregation Report",
href="https://www.cftc.gov/files/dea/history/fut_disagg_txt_2021.zip",
),
]
),
dbc.ModalFooter(
dbc.Button(
"Close",
id="da_reference_card_close",
className="ml-auto",
)
),
],
id="da_reference_card_modal",
),
]
)
],
style={"width": "5rem"},
),
)
# References relating to 3D surface for all
# Populates da_3d_legend_row - unused for now
da_reference_card_3d = (
dbc.Card(
[
dbc.CardBody(
[
dbc.Button("?", id="surface_card_open", color="dark"),
dbc.Modal(
[
dbc.ModalHeader("DA 3D Report Reference"),
dbc.ModalBody(
[
html.P(
[
"Pr: Producer",
html.Br(),
"SD: Swap Dealer",
html.Br(),
"MM: Money Manager",
html.Br(),
"NR: Non-reporting",
html.Br(),
"Ot: Other",
]
),
html.P(
[
"L: long positions / Demand",
html.Br(),
"S: short positions / Supply",
html.Br(),
"Net: long minus short",
]
),
]
),
dbc.ModalFooter(
dbc.Button(
"Close",
id="surface_card_close",
className="ml-auto",
)
),
],
id="surface_card_modal",
),
]
)
],
style={"width": "5rem"},
),
)
# Create drop-down selector
future_select = dbc.Row(
[
dbc.Col(
[
html.Div(
[
dcc.Dropdown(
id="future",
options=[{"label": i, "value": i} for i in bl.da_list],
value="SILVER - COMMODITY EXCHANGE INC.",
),
],
className="dash-bootstrap",
),
],
md=6,
)
]
)
# Info Bar
info_bar = html.Div(
id="summary",
)
# Container for sentiment charts
sentiment_direction = dbc.Row(
[
dbc.Col(
dcc.Graph(
id="deacot_sent",
style={"height": "70vh"},
config=lc.tool_config,
),
md=11,
),
dbc.Col(
html.Div(deacot_reference_card),
md=1,
),
]
)
# This is a busy chart so give it its own row
DA_direction = dbc.Row(
[
dbc.Col(
dcc.Graph(
id="da_sent",
style={"height": "70vh"},
config=lc.tool_config,
),
md=11,
),
dbc.Col(
html.Div(da_reference_card),
md=1,
),
]
)
# Container for postion charts
da_postiions = dbc.Row(
[
dbc.Col(
dcc.Graph(
id="da_pos_all",
style={"height": "70vh"},
config=lc.tool_config,
),
md=6,
),
dbc.Col(
dcc.Graph(
id="da_pos_pct",
style={"height": "70vh"},
config=lc.tool_config,
),
md=6,
),
]
)
# Container for 3d Net Postion Chart
da_3d_positions = dbc.Row(
[
dbc.Col(
[
dcc.Graph(
id="da_3d_net",
style={"height": "70vh"},
config=lc.tool_config,
),
dcc.RangeSlider(
id="da_3d_net_range_slider",
min=bl.df_da["week_number"].min(),
max=bl.df_da["week_number"].max(),
value=[
bl.df_da["week_number"].min(),
bl.df_da["week_number"].max(),
],
allowCross=False,
),
],
md=6,
),
# dbc.Col(
# html.Div(da_reference_card_3d),
# align="center",
# md=1,
# ),
dbc.Col(
[
dcc.Graph(
id="da_3d_all",
style={"height": "70vh"},
config=lc.tool_config,
),
dcc.RangeSlider(
id="da_3d_all_range_slider",
min=bl.df_da["week_number"].min(),
max=bl.df_da["week_number"].max(),
value=[
bl.df_da["week_number"].min(),
bl.df_da["week_number"].max(),
],
allowCross=False,
),
],
md=6,
),
]
)
# Currently unused. Can't quite get a formatting I like
# sticking a pin in it for now.
da_3d_legend_row = dbc.Row(
[
dbc.Col(
md=5,
),
dbc.Col(
html.Div(da_reference_card_3d),
align="center",
md=2,
),
dbc.Col(
md=5,
),
]
)
# Container for relative change bar charts
da_diffs = dbc.Row(
[
dbc.Col(
dcc.Graph(
id="da_diff_all",
style={"height": "75vh"},
config=lc.tool_config,
),
md=6,
),
dbc.Col(
dcc.Graph(
id="da_diff_pct",
style={"height": "75vh"},
config=lc.tool_config,
),
md=6,
),
]
)
# Container for the latest week position bar chart
da_pos_snap = dbc.Row(
[
dbc.Col(
dcc.Graph(
id="da_bar",
style={"height": "75vh"},
config=lc.tool_config,
),
md=12,
),
]
)
# Container to put the reference row together
references = dbc.Row(
[
dbc.Col(
html.Div(deacot_reference_card),
md=6,
),
dbc.Col(
html.Div(da_reference_card),
md=6,
),
]
)
####################################################
# Layout Creation Section
####################################################
main_page = html.Div(
[
html.Hr(),
html.H5("Futures Market Comparison and Analysis", style=TEXT_STYLE),
html.Hr(),
future_select,
html.Hr(),
info_bar,
html.Hr(),
sentiment_direction,
html.Hr(),
DA_direction,
html.Hr(),
da_postiions,
html.Hr(),
da_3d_positions,
html.Hr(),
da_pos_snap,
html.Hr(),
da_diffs,
html.Hr(),
],
style=CONTENT_STYLE,
)
#############################################################################
# Application parameters
#############################################################################
app = dash.Dash(
__name__,
suppress_callback_exceptions=True,
external_stylesheets=[dbc.themes.CYBORG],
)
app.config.suppress_callback_exceptions = True
app.title = "CFTC Data Analysis"
app.layout = html.Div(
[dcc.Location(id="url", refresh=False), html.Div(id="page-content")]
)
# Multi-page selector callback - not really used, but left in for future use
@app.callback(Output("page-content", "children"), Input("url", "pathname"))
def display_page(pathname):
# Left in because I'm not sure if this will be a muli-page app at some point
# if pathname == "/market-sentiment":
# return volumes
# else:
return main_page
####################################################
# Callbacks - Modals
####################################################
# Deacot Reference Card
@app.callback(
Output("deacot_card_modal", "is_open"),
[
Input("deacot_card_open", "n_clicks"),
Input("deacot_card_close", "n_clicks"),
],
[State("deacot_card_modal", "is_open")],
)
def toggle_modal(n1, n2, is_open):
if n1 or n2:
return not is_open
return is_open
# DA Reference card
@app.callback(
Output("da_reference_card_modal", "is_open"),
[
Input("da_reference_card_open", "n_clicks"),
Input("da_reference_card_close", "n_clicks"),
],
[State("da_reference_card_modal", "is_open")],
)
def toggle_modal(n1, n2, is_open):
if n1 or n2:
return not is_open
return is_open
# DA Reference card
@app.callback(
Output("surface_card_modal", "is_open"),
[
Input("surface_card_open", "n_clicks"),
Input("surface_card_close", "n_clicks"),
],
[State("surface_card_modal", "is_open")],
)
def toggle_modal(n1, n2, is_open):
if n1 or n2:
return not is_open
return is_open
####################################################
# Callbacks - charts
####################################################
# Sentiment charts
@app.callback(
dash.dependencies.Output("deacot_sent", "figure"),
[dash.dependencies.Input("future", "value")],
)
def deacot_sentiment(future1):
df1 = bl.df_deacot[bl.df_deacot["Exchange"] == future1]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
fig = sf.make_sentiment_chart(df1, asset)
return fig
@app.callback(
dash.dependencies.Output("da_sent", "figure"),
[dash.dependencies.Input("future", "value")],
)
def all_positions_da(future1):
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
fig = sf.make_chart_DA(df1, asset, "Contracts")
return fig
# Positions Charts
@app.callback(
dash.dependencies.Output("da_pos_all", "figure"),
[dash.dependencies.Input("future", "value")],
)
def net_positions_actual_da(future1):
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
fig = sf.make_net_DA_pos(df1, asset, "Contracts")
return fig
@app.callback(
dash.dependencies.Output("da_pos_pct", "figure"),
[dash.dependencies.Input("future", "value")],
)
def net_positions_pct_da(future1):
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
fig = sf.make_net_DA(df1, asset, "Contracts")
return fig
# 3d postiion chart
@app.callback(
dash.dependencies.Output("da_3d_net", "figure"),
[
dash.dependencies.Input("future", "value"),
dash.dependencies.Input("da_3d_net_range_slider", "value"),
],
)
def da_3d_position_net(future1, week):
# Rangeslider - set initial values if none are set
if week is None:
first_week = bl.df_da["week_number"].min()
last_week = bl.df_da["week_number"].max()
else:
first_week = week[0]
last_week = week[1]
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
# Rangeslider - filter by the selected slide ends
df1 = df1[(df1["week_number"] >= first_week) & (df1["week_number"] <= last_week)]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
fig = sf.da_3d_surface(df1, asset)
return fig
# 3d postiion chart for all positions
@app.callback(
dash.dependencies.Output("da_3d_all", "figure"),
[
dash.dependencies.Input("future", "value"),
dash.dependencies.Input("da_3d_all_range_slider", "value"),
],
)
def da_3d_position_all(future1, week):
# Rangeslider - set initial values if none are set
if week is None:
first_week = bl.df_da["week_number"].min()
last_week = bl.df_da["week_number"].max()
else:
first_week = week[0]
last_week = week[1]
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
# Rangeslider - filter by the selected slide ends
df1 = df1[(df1["week_number"] >= first_week) & (df1["week_number"] <= last_week)]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
fig = sf.da_3d_surface_all(df1, asset)
return fig
# Week-over-week diffs in positions charts
@app.callback(
dash.dependencies.Output("da_diff_all", "figure"),
[dash.dependencies.Input("future", "value")],
)
def diff_position_actual_barchart_da(future1):
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
df = df1.iloc[-2:]
fig = sf.make_diff_barchart_DA_actual(df, asset, "Contracts")
return fig
@app.callback(
dash.dependencies.Output("da_diff_pct", "figure"),
[dash.dependencies.Input("future", "value")],
)
def diff_position_pct_barchart_da(future1):
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
df = df1.iloc[-2:]
fig = sf.make_diff_barchart_DA(df, asset, "Contracts")
return fig
# Total position breakdown chart
@app.callback(
dash.dependencies.Output("da_bar", "figure"),
[dash.dependencies.Input("future", "value")],
)
def total_position_barchart_da(future1):
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
df1.set_index("Date", inplace=True)
arr = df1["commodity"].unique()
asset = arr[0]
df = df1.iloc[-1:]
fig = sf.make_barchart_DA(df, asset, "Contracts")
return fig
###################################################
# Summary Block
###################################################
@app.callback(
dash.dependencies.Output("summary", "children"),
[dash.dependencies.Input("future", "value")],
)
def dashboard_summary_numbers(future1):
# Grab some values from the most recent DA datafame
df1 = bl.df_da[bl.df_da["Exchange"] == future1]
df1.set_index("Date", inplace=True)
# I only care about the most recent row so pull it
# It makes reference easier further down.
df = df1.iloc[-1:]
# Return the entire structured block
return html.Div(
dbc.Row(
[
dbc.Col(
dbc.Alert(
[
html.H6("Latest Date: "),
html.H6(df.index),
],
color="light",
),
md=2,
),
dbc.Col(
dbc.Alert(
[
html.H6("Open Interest: "),
html.H6(df.Open_Interest),
],
color="success",
),
md=2,
),
dbc.Col(
dbc.Alert(
[
html.H6("Total Reporting Long: "),
html.H6(df.total_report_long_all),
],
color="primary",
),
md=2,
),
dbc.Col(
dbc.Alert(
[
html.H6("Total Reporting Short: "),
html.H6(df.total_report_short_all),
],
color="warning",
),
md=2,
),
dbc.Col(
dbc.Alert(
[
html.H6("Total Non-Reporting Long: "),
html.H6(df.nonreport_long_all),
],
color="primary",
),
md=2,
),
dbc.Col(
dbc.Alert(
[
html.H6("Total Non-Reporting Short: "),
html.H6(df.nonreport_short_all),
],
color="warning",
),
md=2,
),
]
)
)
###################################################
# Server Run
###################################################
if __name__ == "__main__":
app.run_server(debug=True, host="0.0.0.0", port=8050, dev_tools_hot_reload=True)