forked from bitkarrot/dca-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
348 lines (305 loc) · 9.39 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
from dash import Dash, dcc, html, Input, Output, State
import plotly.express as px
from cache_data import get_data_from_file, get_cached_data
from datetime import datetime as dt
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import load_figure_template
from dash.exceptions import PreventUpdate
"""
Bitcoin DCA calculator
x axis = date, y axis = total amount dca'd
total = sum date start - date end of [amt * frequency]
unit rate = fiat amt / btc rate in fiat
frequency = (daily, weekly, bi-weekly, monthly)
currency = hkd or usd
Inputs: amount, currency, date range, frequency
"""
LOGO = "https://rates.bitcoin.org.hk/static/images/BAHK_black_square.svg"
# stylesheet with the .dbc class
dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
app = Dash(external_stylesheets=[dbc.themes.VAPOR, dbc_css])
app.title = "DCA"
server = app.server
# datafile = "./btc_historical"
# df = get_data_from_file(datafile)
df = get_cached_data()
# Find the latest date in the index
latest_date = df.index.max()
print(latest_date)
# Check for duplicates
duplicates = df.index.duplicated(keep="first")
# Remove duplicate rows
df = df[~duplicates]
# df = get_cached_data()
df_weekly = df.resample("W").last()
df_biweekly = df.resample("2W").last()
df_monthly = df.resample("M").last()
load_figure_template(
["sketchy", "cyborg", "minty", "darkly", "vapor", "slate", "superhero", "quartz"]
)
template_type = "vapor"
items_bar = dbc.Row(
[
dbc.Col(
dbc.NavItem(dbc.NavLink("Rates", href="https://rates.bitcoin.org.hk/"))
),
dbc.Col(dbc.NavItem(dbc.NavLink("Sats", href="https://sats.bitcoin.org.hk/"))),
dbc.Col(
dbc.NavItem(dbc.NavLink("Blocks", href="https://blocks.bitcoin.org.hk/"))
),
],
className="text-white ms-auto flex-nowrap mt-3 mt-md-0",
align="center",
)
navbar = dbc.Navbar(
dbc.Container(
[
html.A(
# Use row and col to control vertical alignment of logo / brand
dbc.Row(
[
dbc.Col(html.Img(src=LOGO, height="60px")),
dbc.Col(dbc.NavbarBrand("Bitcoin HK", className="ms-2")),
],
align="center",
),
href="https://bitcoin.org.hk",
style={"textDecoration": "none"},
),
dbc.NavbarToggler(id="navbar-toggler", n_clicks=0),
dbc.Collapse(
items_bar,
id="navbar-collapse",
is_open=False,
navbar=True,
),
],
),
color="dark",
dark=True,
)
# add callback for toggling the collapse on small screens
@app.callback(
Output("navbar-collapse", "is_open"),
[Input("navbar-toggler", "n_clicks")],
[State("navbar-collapse", "is_open")],
)
def toggle_navbar_collapse(n, is_open):
if n:
return not is_open
return is_open
currency_type = html.Div(
[
dbc.Label("Currency: ", className="ms-2"),
dbc.RadioItems(
options=[
{"label": "HKD", "value": "HKD"},
{"label": "USD", "value": "USD"},
],
value="HKD",
id="currency",
inline=True,
className="mb-3",
),
],
className="mt-3 mb-4 mt-md-0",
)
amount_input = html.Div(
[
dbc.Label("Enter Amount: ", className="ms-2"),
dbc.Input(
size="lg",
value=100,
className="mb-3",
type="number",
id="amount",
min=0,
max=1000000,
step=1,
),
]
)
inline_radioitems = html.Div(
[
dbc.Label("Frequency: ", className="ms-2"),
dbc.RadioItems(
options=[
{"label": "Daily", "value": "daily"},
{"label": "Weekly", "value": "weekly"},
{"label": "Bi-weekly", "value": "bi-weekly"},
{"label": "Monthly", "value": "monthly"},
],
value="monthly",
id="freq",
inline=True,
className="mb-3",
),
],
className="mt-3 mb-4 mt-md-0",
)
date_range = html.Div(
[
dbc.Label("Date Range: ", className="ms-2"),
dcc.DatePickerRange(
id="date-picker",
min_date_allowed=dt(2010, 7, 28),
max_date_allowed=latest_date,
initial_visible_month=dt(2023, 1, 1),
start_date=dt(2022, 1, 1),
end_date=latest_date,
className="ms-2",
),
],
className="mt-3 mb-4 mt-md-0",
)
footer = html.Div(
[
html.A(
"Source",
href="https://github.com/bitkarrot/dca-calculator",
style={"textDecoration": "none"},
)
],
className="mb-4",
)
collapse = html.Div(
[
dbc.Collapse([
html.P(
"Find out how many Sats you can Stack with this Dollar Cost Average (DCA) calculator.",
className="text-white",
),
amount_input,
currency_type,
inline_radioitems,
date_range,
],
id="collapse",
is_open=True,
),
dbc.Button(
"Show/Hide Calculator",
id="collapse-button",
className="mb-1",
color="primary",
n_clicks=0,
),
], className="text-white",
)
app.layout = dbc.Container(
[
navbar,
dbc.Card(
[
dbc.Container(
[
html.Div(
[
html.H1(
"DCA Calculator", className="display-3 text-warning"
),
# html.P(
# "Find out how many Sats you can Stack with this Dollar Cost Average (DCA) calculator.",
# className="text-white",
# ),
],
className="mt-4 mb-4",
),
html.Div(
[
collapse,
],
className="text-white p-3 bg-primary bg-opacity-10",
),
html.Div(
[
html.Div(id="stacked", className="text-warning"),
dcc.Markdown(),
dcc.Graph(id="graph", config={"displayModeBar": False}),
],
className="p-3",
),
footer,
]
)
],
className="",
),
],
fluid=True,
className="dbc",
)
@app.callback(
Output("collapse", "is_open"),
[Input("collapse-button", "n_clicks")],
[State("collapse", "is_open")],
)
def toggle_collapse(n, is_open):
if n:
return not is_open
return is_open
@app.callback(
Output("graph", "figure"),
Output("stacked", "children"),
Input("amount", "value"),
Input("currency", "value"),
Input("freq", "value"),
Input("date-picker", "start_date"),
Input("date-picker", "end_date"),
)
def display_area(amount, currency, freq, start_date, end_date):
try:
# print(amount, currency, freq, start_date, end_date)
if amount is None:
raise Exception("Amount is none")
# date range
f_df = df[(df.index >= start_date) & (df.index <= end_date)]
# filter by frequency
if freq == "weekly":
f_df = f_df.resample("W").last()
elif freq == "bi-weekly":
f_df = f_df.resample("2W").last()
elif freq == "monthly":
f_df = f_df.resample("M").last()
# currency type
currency_col = "usdsat_rate"
if currency == "HKD":
currency_col = "sathkd_rate"
dfs = f_df[[currency_col]].copy()
dfs["Sats per freq"] = dfs[currency_col] * amount
dfs["Sats Stacked"] = dfs["Sats per freq"].cumsum()
total_value = dfs["Sats per freq"].sum()
btc_total = total_value / 100000000
fig = px.area(dfs, x=dfs.index, y="Sats Stacked", template=template_type)
# update the line color
# fig.update_traces(line_color="orange")
# content info
stacker_info = (
"### You stacked a total of "
+ str(format(total_value, ","))
+ " sats or "
+ str(btc_total)
+ " BTC \n\n"
)
stacker_info = (
stacker_info
+ "with "
+ str(amount)
+ " "
+ str(currency)
+ " "
+ str(freq)
+ " dollar cost averaging "
)
stacker_info = (
stacker_info
+ " from "
+ start_date.split("T")[0]
+ " to "
+ end_date.split("T")[0]
)
return [fig, dcc.Markdown(stacker_info)]
except Exception as e:
raise PreventUpdate
if __name__ == "__main__":
app.run_server(debug=True)