-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathapp.py
744 lines (643 loc) · 21.5 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
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
# -*- coding: utf-8 -*-
from dash import Dash, dcc, html, dash_table, Input, Output, State, callback_context
import dash_bootstrap_components as dbc
import plotly.graph_objects as go
import pandas as pd
import about
app_description = """
How does asset allocation affect portfolio performance? Select the percentage of stocks, bonds and cash
in a portfolio and see annual returns over any time period from 1928 to 2021.
"""
app_title = "Asset Allocation Visualizer"
app_image = "https://www.wealthdashboard.app/assets/app.png"
metas = [
{"name": "viewport", "content": "width=device-width, initial-scale=1"},
{"property": "twitter:card", "content": "summary_large_image"},
{"property": "twitter:url", "content": "https://www.wealthdashboard.app/"},
{"property": "twitter:title", "content": app_title},
{"property": "twitter:description", "content": app_description},
{"property": "twitter:image", "content": app_image},
{"property": "og:title", "content": app_title},
{"property": "og:type", "content": "website"},
{"property": "og:description", "content": app_description},
{"property": "og:image", "content": app_image},
]
app = Dash(
__name__,
external_stylesheets=[dbc.themes.SPACELAB, dbc.icons.FONT_AWESOME],
meta_tags=metas,
title=app_title,
)
# make dataframe from spreadsheet:
df = pd.read_csv("assets/historic.csv")
MAX_YR = df.Year.max()
MIN_YR = df.Year.min()
START_YR = 2007
# since data is as of year end, need to add start year
df = (
pd.concat([df, pd.DataFrame([{"Year": MIN_YR - 1}])], ignore_index=True)
.sort_values("Year", ignore_index=True)
.fillna(0)
)
COLORS = {
"cash": "#3cb521",
"bonds": "#fd7e14",
"stocks": "#446e9b",
"inflation": "#cd0200",
"background": "whitesmoke",
}
"""
==========================================================================
Markdown Text
"""
datasource_text = dcc.Markdown(
"""
[Data source:](http://pages.stern.nyu.edu/~adamodar/New_Home_Page/datafile/histretSP.html)
Historical Returns on Stocks, Bonds and Bills from NYU Stern School of
Business
"""
)
asset_allocation_text = dcc.Markdown(
"""
> **Asset allocation** is one of the main factors that drive portfolio risk and returns. Play with the app and see for yourself!
> Change the allocation to cash, bonds and stocks on the sliders and see how your portfolio performs over time in the graph.
Try entering different time periods and dollar amounts too.
"""
)
learn_text = dcc.Markdown(
"""
Past performance certainly does not determine future results, but you can still
learn a lot by reviewing how various asset classes have performed over time.
Use the sliders to change the asset allocation (how much you invest in cash vs
bonds vs stock) and see how this affects your returns.
Note that the results shown in "My Portfolio" assumes rebalancing was done at
the beginning of every year. Also, this information is based on the S&P 500 index
as a proxy for "stocks", the 10 year US Treasury Bond for "bonds" and the 3 month
US Treasury Bill for "cash." Your results of course, would be different based
on your actual holdings.
This is intended to help you determine your investment philosophy and understand
what sort of risks and returns you might see for each asset category.
The data is from [Aswath Damodaran](http://people.stern.nyu.edu/adamodar/New_Home_Page/home.htm)
who teaches corporate finance and valuation at the Stern School of Business
at New York University.
Check out his excellent on-line course in
[Investment Philosophies.](http://people.stern.nyu.edu/adamodar/New_Home_Page/webcastinvphil.htm)
"""
)
cagr_text = dcc.Markdown(
"""
(CAGR) is the compound annual growth rate. It measures the rate of return for an investment over a period of time,
such as 5 or 10 years. The CAGR is also called a "smoothed" rate of return because it measures the growth of
an investment as if it had grown at a steady rate on an annually compounded basis.
"""
)
footer = html.Div(
[
dcc.Markdown(
"""
This information is intended solely as general information for educational
and entertainment purposes only and is not a substitute for professional advice and
services from qualified financial services providers familiar with your financial
situation.
"""
),
],
className="p-2 mt-5 bg-primary text-white small",
)
"""
==========================================================================
Tables
"""
total_returns_table = dash_table.DataTable(
id="total_returns",
columns=[{"id": "Year", "name": "Year", "type": "text"}]
+ [
{"id": col, "name": col, "type": "numeric", "format": {"specifier": "$,.0f"}}
for col in ["Cash", "Bonds", "Stocks", "Total"]
],
page_size=15,
style_table={"overflowX": "scroll"},
)
annual_returns_pct_table = dash_table.DataTable(
id="annual_returns_pct",
columns=(
[{"id": "Year", "name": "Year", "type": "text"}]
+ [
{"id": col, "name": col, "type": "numeric", "format": {"specifier": ".1%"}}
for col in df.columns[1:]
]
),
data=df.to_dict("records"),
sort_action="native",
page_size=15,
style_table={"overflowX": "scroll"},
)
def make_summary_table(dff):
"""Make html table to show cagr and best and worst periods"""
table_class = "h5 text-body text-nowrap"
cash = html.Span(
[html.I(className="fa fa-money-bill-alt"), " Cash"], className=table_class
)
bonds = html.Span(
[html.I(className="fa fa-handshake"), " Bonds"], className=table_class
)
stocks = html.Span(
[html.I(className="fa fa-industry"), " Stocks"], className=table_class
)
inflation = html.Span(
[html.I(className="fa fa-ambulance"), " Inflation"], className=table_class
)
start_yr = dff["Year"].iat[0]
end_yr = dff["Year"].iat[-1]
df_table = pd.DataFrame(
{
"": [cash, bonds, stocks, inflation],
f"Rate of Return (CAGR) from {start_yr} to {end_yr}": [
cagr(dff["all_cash"]),
cagr(dff["all_bonds"]),
cagr(dff["all_stocks"]),
cagr(dff["inflation_only"]),
],
f"Worst 1 Year Return": [
worst(dff, "3-mon T.Bill"),
worst(dff, "10yr T.Bond"),
worst(dff, "S&P 500"),
"",
],
}
)
return dbc.Table.from_dataframe(df_table, bordered=True, hover=True)
"""
==========================================================================
Figures
"""
def make_pie(slider_input, title):
fig = go.Figure(
data=[
go.Pie(
labels=["Cash", "Bonds", "Stocks"],
values=slider_input,
textinfo="label+percent",
textposition="inside",
marker={"colors": [COLORS["cash"], COLORS["bonds"], COLORS["stocks"]]},
sort=False,
hoverinfo="none",
)
]
)
fig.update_layout(
title_text=title,
title_x=0.5,
margin=dict(b=25, t=75, l=35, r=25),
height=325,
paper_bgcolor=COLORS["background"],
)
return fig
def make_line_chart(dff):
start = dff.loc[1, "Year"]
yrs = dff["Year"].size - 1
dtick = 1 if yrs < 16 else 2 if yrs in range(16, 30) else 5
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=dff["Year"],
y=dff["all_cash"],
name="All Cash",
marker_color=COLORS["cash"],
)
)
fig.add_trace(
go.Scatter(
x=dff["Year"],
y=dff["all_bonds"],
name="All Bonds (10yr T.Bonds)",
marker_color=COLORS["bonds"],
)
)
fig.add_trace(
go.Scatter(
x=dff["Year"],
y=dff["all_stocks"],
name="All Stocks (S&P500)",
marker_color=COLORS["stocks"],
)
)
fig.add_trace(
go.Scatter(
x=dff["Year"],
y=dff["Total"],
name="My Portfolio",
marker_color="black",
line=dict(width=6, dash="dot"),
)
)
fig.add_trace(
go.Scatter(
x=dff["Year"],
y=dff["inflation_only"],
name="Inflation",
visible=True,
marker_color=COLORS["inflation"],
)
)
fig.update_layout(
title=f"Returns for {yrs} years starting {start}",
template="none",
showlegend=True,
legend=dict(x=0.01, y=0.99),
height=400,
margin=dict(l=40, r=10, t=60, b=55),
yaxis=dict(tickprefix="$", fixedrange=True),
xaxis=dict(title="Year Ended", fixedrange=True, dtick=dtick),
)
return fig
"""
==========================================================================
Make Tabs
"""
# =======Play tab components
asset_allocation_card = dbc.Card(asset_allocation_text, className="mt-2")
slider_card = dbc.Card(
[
html.H4("First set cash allocation %:", className="card-title"),
dcc.Slider(
id="cash",
marks={i: f"{i}%" for i in range(0, 101, 10)},
min=0,
max=100,
step=5,
value=10,
included=False,
),
html.H4(
"Then set stock allocation % ",
className="card-title mt-3",
),
html.Div("(The rest will be bonds)", className="card-title"),
dcc.Slider(
id="stock_bond",
marks={i: f"{i}%" for i in range(0, 91, 10)},
min=0,
max=90,
step=5,
value=50,
included=False,
),
],
body=True,
className="mt-4",
)
time_period_data = [
{
"label": f"2007-2008: Great Financial Crisis to {MAX_YR}",
"start_yr": 2007,
"planning_time": MAX_YR - START_YR + 1,
},
{
"label": "1999-2010: The decade including 2000 Dotcom Bubble peak",
"start_yr": 1999,
"planning_time": 10,
},
{
"label": "1969-1979: The 1970s Energy Crisis",
"start_yr": 1970,
"planning_time": 10,
},
{
"label": "1929-1948: The 20 years following the start of the Great Depression",
"start_yr": 1929,
"planning_time": 20,
},
{
"label": f"{MIN_YR}-{MAX_YR}",
"start_yr": "1928",
"planning_time": MAX_YR - MIN_YR + 1,
},
]
time_period_card = dbc.Card(
[
html.H4(
"Or select a time period:",
className="card-title",
),
dbc.RadioItems(
id="time_period",
options=[
{"label": period["label"], "value": i}
for i, period in enumerate(time_period_data)
],
value=0,
labelClassName="mb-2",
),
],
body=True,
className="mt-4",
)
# ======= InputGroup components
start_amount = dbc.InputGroup(
[
dbc.InputGroupText("Start Amount $"),
dbc.Input(
id="starting_amount",
placeholder="Min $10",
type="number",
min=10,
value=10000,
),
],
className="mb-3",
)
start_year = dbc.InputGroup(
[
dbc.InputGroupText("Start Year"),
dbc.Input(
id="start_yr",
placeholder=f"min {MIN_YR} max {MAX_YR}",
type="number",
min=MIN_YR,
max=MAX_YR,
value=START_YR,
),
],
className="mb-3",
)
number_of_years = dbc.InputGroup(
[
dbc.InputGroupText("Number of Years:"),
dbc.Input(
id="planning_time",
placeholder="# yrs",
type="number",
min=1,
value=MAX_YR - START_YR + 1,
),
],
className="mb-3",
)
end_amount = dbc.InputGroup(
[
dbc.InputGroupText("Ending Amount"),
dbc.Input(id="ending_amount", disabled=True, className="text-black"),
],
className="mb-3",
)
rate_of_return = dbc.InputGroup(
[
dbc.InputGroupText(
"Rate of Return(CAGR)",
id="tooltip_target",
className="text-decoration-underline",
),
dbc.Input(id="cagr", disabled=True, className="text-black"),
dbc.Tooltip(cagr_text, target="tooltip_target"),
],
className="mb-3",
)
input_groups = html.Div(
[start_amount, start_year, number_of_years, end_amount, rate_of_return],
className="mt-4 p-4",
)
# ===== Results Tab components
results_card = dbc.Card(
[
dbc.CardHeader("My Portfolio Returns - Rebalanced Annually"),
html.Div(total_returns_table),
],
className="mt-4",
)
data_source_card = dbc.Card(
[
dbc.CardHeader("Source Data: Annual Total Returns"),
html.Div(annual_returns_pct_table),
],
className="mt-4",
)
# ========= Learn Tab Components
learn_card = dbc.Card(
[
dbc.CardHeader("An Introduction to Asset Allocation"),
dbc.CardBody(learn_text),
],
className="mt-4",
)
# ========= Build tabs
tabs = dbc.Tabs(
[
dbc.Tab(learn_card, tab_id="tab1", label="Learn"),
dbc.Tab(
[asset_allocation_text, slider_card, input_groups, time_period_card],
tab_id="tab-2",
label="Play",
className="pb-4",
),
dbc.Tab([results_card, data_source_card], tab_id="tab-3", label="Results"),
],
id="tabs",
active_tab="tab-2",
className="mt-2",
)
"""
==========================================================================
Helper functions to calculate investment results, cagr and worst periods
"""
def backtest(stocks, cash, start_bal, nper, start_yr):
"""calculates the investment returns for user selected asset allocation,
rebalanced annually and returns a dataframe
"""
end_yr = start_yr + nper - 1
cash_allocation = cash / 100
stocks_allocation = stocks / 100
bonds_allocation = (100 - stocks - cash) / 100
# Select time period - since data is for year end, include year prior
# for start ie year[0]
dff = df[(df.Year >= start_yr - 1) & (df.Year <= end_yr)].set_index(
"Year", drop=False
)
dff["Year"] = dff["Year"].astype(int)
# add columns for My Portfolio returns
dff["Cash"] = cash_allocation * start_bal
dff["Bonds"] = bonds_allocation * start_bal
dff["Stocks"] = stocks_allocation * start_bal
dff["Total"] = start_bal
dff["Total"] = dff["Total"].astype(float)
dff["Rebalance"] = True
# calculate My Portfolio returns
for yr in dff.Year + 1:
if yr <= end_yr:
# Rebalance at the beginning of the period by reallocating
# last period's total ending balance
if dff.loc[yr, "Rebalance"]:
dff.loc[yr, "Cash"] = dff.loc[yr - 1, "Total"] * cash_allocation
dff.loc[yr, "Stocks"] = dff.loc[yr - 1, "Total"] * stocks_allocation
dff.loc[yr, "Bonds"] = dff.loc[yr - 1, "Total"] * bonds_allocation
# calculate this period's returns
dff.loc[yr, "Cash"] = dff.loc[yr, "Cash"] * (
1 + dff.loc[yr, "3-mon T.Bill"]
)
dff.loc[yr, "Stocks"] = dff.loc[yr, "Stocks"] * (1 + dff.loc[yr, "S&P 500"])
dff.loc[yr, "Bonds"] = dff.loc[yr, "Bonds"] * (
1 + dff.loc[yr, "10yr T.Bond"]
)
dff.loc[yr, "Total"] = dff.loc[yr, ["Cash", "Bonds", "Stocks"]].sum()
dff = dff.reset_index(drop=True)
columns = ["Cash", "Stocks", "Bonds", "Total"]
dff[columns] = dff[columns].round(0)
# create columns for when portfolio is all cash, all bonds or all stocks,
# include inflation too
#
# create new df that starts in yr 1 rather than yr 0
dff1 = (dff[(dff.Year >= start_yr) & (dff.Year <= end_yr)]).copy()
#
# calculate the returns in new df:
columns = ["all_cash", "all_bonds", "all_stocks", "inflation_only"]
annual_returns = ["3-mon T.Bill", "10yr T.Bond", "S&P 500", "Inflation"]
for col, return_pct in zip(columns, annual_returns):
dff1[col] = round(start_bal * (1 + (1 + dff1[return_pct]).cumprod() - 1), 0)
#
# select columns in the new df to merge with original
dff1 = dff1[["Year"] + columns]
dff = dff.merge(dff1, how="left")
# fill in the starting balance for year[0]
dff.loc[0, columns] = start_bal
return dff
def cagr(dff):
"""calculate Compound Annual Growth Rate for a series and returns a formated string"""
start_bal = dff.iat[0]
end_bal = dff.iat[-1]
planning_time = len(dff) - 1
cagr_result = ((end_bal / start_bal) ** (1 / planning_time)) - 1
return f"{cagr_result:.1%}"
def worst(dff, asset):
"""calculate worst returns for asset in selected period returns formated string"""
worst_yr_loss = min(dff[asset])
worst_yr = dff.loc[dff[asset] == worst_yr_loss, "Year"].iloc[0]
return f"{worst_yr_loss:.1%} in {worst_yr}"
"""
===========================================================================
Main Layout
"""
app.layout = dbc.Container(
[
dbc.Row(
dbc.Col(
html.H2(
"Asset Allocation Visualizer",
className="text-center bg-primary text-white p-2",
),
)
),
dbc.Row(
[
dbc.Col(tabs, width=12, lg=5, className="mt-4 border"),
dbc.Col(
[
dcc.Graph(id="allocation_pie_chart", className="mb-2"),
dcc.Graph(id="returns_chart", className="pb-4"),
html.Hr(),
html.Div(id="summary_table"),
html.H6(datasource_text, className="my-2"),
],
width=12,
lg=7,
className="pt-4",
),
],
className="ms-1",
),
dbc.Row(dbc.Col(footer)),
dbc.Row(dbc.Col(about.card, width="auto"), justify="center")
],
fluid=True,
)
"""
==========================================================================
Callbacks
"""
@app.callback(
Output("allocation_pie_chart", "figure"),
Input("stock_bond", "value"),
Input("cash", "value"),
)
def update_pie(stocks, cash):
bonds = 100 - stocks - cash
slider_input = [cash, bonds, stocks]
if stocks >= 70:
investment_style = "Aggressive"
elif stocks <= 30:
investment_style = "Conservative"
else:
investment_style = "Moderate"
figure = make_pie(slider_input, investment_style + " Asset Allocation")
return figure
@app.callback(
Output("stock_bond", "max"),
Output("stock_bond", "marks"),
Output("stock_bond", "value"),
Input("cash", "value"),
State("stock_bond", "value"),
)
def update_stock_slider(cash, initial_stock_value):
max_slider = 100 - int(cash)
stocks = min(max_slider, initial_stock_value)
# formats the slider scale
if max_slider > 50:
marks_slider = {i: f"{i}%" for i in range(0, max_slider + 1, 10)}
elif max_slider <= 15:
marks_slider = {i: f"{i}%" for i in range(0, max_slider + 1, 1)}
else:
marks_slider = {i: f"{i}%" for i in range(0, max_slider + 1, 5)}
return max_slider, marks_slider, stocks
@app.callback(
Output("planning_time", "value"),
Output("start_yr", "value"),
Output("time_period", "value"),
Input("planning_time", "value"),
Input("start_yr", "value"),
Input("time_period", "value"),
)
def update_time_period(planning_time, start_yr, period_number):
"""syncs inputs and selected time periods"""
ctx = callback_context
input_id = ctx.triggered[0]["prop_id"].split(".")[0]
if input_id == "time_period":
planning_time = time_period_data[period_number]["planning_time"]
start_yr = time_period_data[period_number]["start_yr"]
if input_id in ["planning_time", "start_yr"]:
period_number = None
return planning_time, start_yr, period_number
@app.callback(
Output("total_returns", "data"),
Output("returns_chart", "figure"),
Output("summary_table", "children"),
Output("ending_amount", "value"),
Output("cagr", "value"),
Input("stock_bond", "value"),
Input("cash", "value"),
Input("starting_amount", "value"),
Input("planning_time", "value"),
Input("start_yr", "value"),
)
def update_totals(stocks, cash, start_bal, planning_time, start_yr):
# set defaults for invalid inputs
start_bal = 10 if start_bal is None else start_bal
planning_time = 1 if planning_time is None else planning_time
start_yr = MIN_YR if start_yr is None else int(start_yr)
# calculate valid planning time start yr
max_time = MAX_YR + 1 - start_yr
planning_time = min(max_time, planning_time)
if start_yr + planning_time > MAX_YR:
start_yr = min(df.iloc[-planning_time, 0], MAX_YR) # 0 is Year column
# create investment returns dataframe
dff = backtest(stocks, cash, start_bal, planning_time, start_yr)
# create data for DataTable
data = dff.to_dict("records")
# create the line chart
fig = make_line_chart(dff)
summary_table = make_summary_table(dff)
# format ending balance
ending_amount = f"${dff['Total'].iloc[-1]:0,.0f}"
# calcluate cagr
ending_cagr = cagr(dff["Total"])
return data, fig, summary_table, ending_amount, ending_cagr
if __name__ == "__main__":
app.run_server(debug=True)