-
Notifications
You must be signed in to change notification settings - Fork 3
/
02-app.py
273 lines (209 loc) · 7.36 KB
/
02-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
# import everything in helpers.py
from helpers import *
### Global data
# We read this file once, when the app starts
# Read Stock prices data
close_adj = pd.read_parquet("spy_close_adjusted.parquet")
### Front-End Code
app_ui = ui.page_navbar(
ui.nav("Performance",
ui.row(
ui.column(4,
ui.input_date_range(
"daterange", "Date Range",
start="2006-01-01",
end="2010-01-01")
),
ui.column(4,
ui.input_select("stocks", "Select Stocks",
choices=list(close_adj.columns),
selected=["AAPL", "MSFT", "AMZN",
"ACN", "ADBE"],
multiple=True,
selectize=True
)
),
),
ui.row(
ui.column(6,
ui.h4("Risk & Return"),
output_widget("scatter_plot")),
ui.column(6,
ui.h4("Cumulative Return"),
output_widget("cumulative_return"))
),
ui.row(
ui.column(12,
ui.h4("Performance Metrics"),
output_widget("performance_report"))
)
),
ui.nav("Single Stock Analysis",
ui.row(
ui.input_select("stock_1d", "Select Stock",
choices=list(close_adj.columns),
selected=["MSFT"],
multiple=False,
selectize=True
),
ui.input_select("benchmarks", "Select Benchmarks",
choices=["QQQ", "SPY", "TLT", "LQD"],
selected=["SPY", "TLT", "QQQ"],
multiple=True,
selectize=True
),
),
ui.row(
ui.column(6,
ui.h4("Monthly Return Distributions"),
output_widget("boxplot_benchmarks")),
ui.column(6,
ui.h4("Cumulative Return"),
output_widget("cumulative_returns_benchmarks"))
)
),
title='Portfolio Analytics',
bg="#0062cc",
inverse=True,
id="navbar_id",
footer=ui.div(
{"style": "width:80%;margin: 0 auto"},
ui.tags.style(
"""
h4 {
margin-top: 3em;
}
"""
))
)
### Server: Back-End Code
def server(input, output, session):
@reactive.Calc
def filter_prices():
# Date Range input dates
start_date = input.daterange()[0]
end_date = input.daterange()[1]
# Filter start and end dates
df_selection = close_adj[start_date:end_date]
return df_selection
@reactive.Calc
def daily_returns():
# get stocks list
stocks = list(input.stocks())
# Filtered close prices
df = filter_prices()
# Compute Daily Returns
df = (
df[stocks] # filter stocks
.dropna()
.pct_change() # Compute daily returns
)
return df
@reactive.Calc
def monthly_returns():
# Call reactive function
df = daily_returns()
# 3. Resample returns to be monthly
df = (
df
.resample('M')
.agg(lambda x: (1 + x).prod() - 1)
)
return df
@reactive.Calc
def performance_long():
df = monthly_returns()
perf_summary = performance(df)
perf_equal_weight = (
df
.mean(axis=1)
.to_frame()
.pipe(performance)
)
perf_equal_weight.index = ['Portfolio']
perf_long = pd.concat([perf_summary, perf_equal_weight])
return perf_long
@reactive.Calc
def returns_benchmark():
# Date Range input dates
start_date = input.daterange()[0]
end_date = input.daterange()[1]
# Filter start and end dates
df = close_adj[start_date:end_date]
select_tickers = [input.stock_1d()] + list(input.benchmarks())
df = (
df[select_tickers] # filter stocks
.dropna()
.pct_change() # Compute daily returns
.resample('M')
.agg(lambda x: (1 + x).prod() - 1)
)
return df
############### Portfolio Performance ##############
@output
@render_widget
def performance_report():
perf_long = performance_long()
perf_long = perf_long.round(4)
df = (perf_long
.T.reset_index()
.rename(columns={"index": "Metric"})
)
df['Metric'] = ['Return', 'Risk', "Max DD", "Sharpe"]
cols = list(df.columns[1:])
for col in cols:
df[col] = (df[col] * 100).round(2).astype(str) + "%"
fig = ff.create_table(df)
return go.FigureWidget(fig)
@output
@render_widget
def scatter_plot():
perf_long = performance_long()
perf_long = (perf_long
.reset_index()
.rename(columns={"index":"ticker"}))
fig = px.scatter(perf_long,
x="Annualized Returns",
y="Annualized Risk",
text="ticker")
fig.update_traces(textposition='top center')
fig.update_layout(
xaxis=dict(title='Annualized Returns',
tickformat=".0%"),
yaxis=dict(title='Risk', tickformat=".0%"),
title_text=''
)
return go.FigureWidget(fig)
@output
@render_widget
def cumulative_return():
monthly_ret = monthly_returns()
monthly_ret['Portfolio'] = monthly_ret.mean(axis=1)
ret_cumulative = (1 + monthly_ret).cumprod() - 1
ret_cumulative = (ret_cumulative * 100).round(2)
fig = px.line(ret_cumulative)
fig.update_traces(hovertemplate=None)
fig.update_layout(hovermode="x", yaxis_ticksuffix = "%")
return go.FigureWidget(fig)
############### Benchmarks ##############
@output
@render_widget
def boxplot_benchmarks():
monthly_ret = returns_benchmark()
mdf = (monthly_ret
.reset_index()
.melt(id_vars='Date'))
mdf.columns = ['Date', 'Ticker', "Return"]
fig = px.box(mdf, x='Ticker', y='Return', color='Ticker')
return go.FigureWidget(fig)
@output
@render_widget
def cumulative_returns_benchmarks():
monthly_ret = returns_benchmark()
ret_cumulative = (1 + monthly_ret).cumprod() - 1
ret_cumulative = (ret_cumulative * 100).round(2)
fig = px.line(ret_cumulative)
fig.update_traces(hovertemplate=None)
fig.update_layout(hovermode="x", yaxis_ticksuffix = "%")
return go.FigureWidget(fig)
app = App(app_ui, server, debug=False)