-
Notifications
You must be signed in to change notification settings - Fork 0
/
volatility_decay_app.py
546 lines (508 loc) · 21.2 KB
/
volatility_decay_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
"""
This module contains the code for the volatility decay Streamlit App.
Based on a [reddit](https://www.reddit.com/r/HFEA/comments/tue7n6/the_volatility_decay_equation_with_verification/)
post, make an interactive visualization to show the effect of the volatility decay.
The results show the (somewhat) quadratic (/ logarithmic) volatility drag along the volatility axis, together with
(somewhat) quadratic (/ logarithmic) scaling profit region decrease with increased leverage factor. Further sources
describing the quadratic behaviour:
- [Blogpost](https://www.afrugaldoctor.com/home/leveraged-etfs-and-volatility-decay-part-2)
- [(Detailed) Journal Article, also mentioned in the Blogpost](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1664823)
requirements.txt:
ipywidgets>=7.0.0
numpy
plotly
prophet
streamlit
"""
import streamlit as st
from vol_decay import constants
from vol_decay.investments import (
get_derivatives_data,
update_derivates_calibration_plot,
update_derivatives_performance_plot,
update_ticker_plot,
)
from vol_decay.kelly_theory import update_kelly_plot, update_plot, update_result
from vol_decay.stock_screener import (
compute_adr,
kelly_selection,
positive_return_selection,
volatility_selection,
)
from vol_decay.utils.data_and_forecast import download_universe
if __name__ == "__main__":
# define the layout
st.set_page_config(layout="wide")
st.markdown(
"""
<style>
.header {
background: linear-gradient(to right, #67001f, #053061);
padding: 2em;
color: white !important;
max-width: 100%;
text-align: center;
}
.header h1, .header h3 {
color: white !important;
}
</style>
<div class="header">
<h1>Volatility Decay & Kelly Criterion</h1>
<h3>Volatility decay, the phenomenon of underperforming the market
despite having a larger position, is a central concept in the realm
of financial markets and risk management. Navigating this environment
requires strategic and rational decision-making when it comes to
position sizing, and the Kelly Criterion, developed by John L. Kelly Jr,
is proving to be a valuable tool. This formula allows investors to optimize
the size of their positions while balancing long-term growth expectations with
risk mitigation. By incorporating probabilities of success and risk/return
ratios, the Kelly Criterion provides a smart strategy for investors looking
to make informed decisions in the stock market.</h3>
</div>
""",
unsafe_allow_html=True,
)
tab1, tab2, tab3 = st.tabs(
["Kelly Theory", "Investments", "Stock Universe Screener (WIP)"]
)
with tab1:
# Header for the Kelly Criterion
st.markdown("", unsafe_allow_html=True)
st.write(
"## [Kelly Criterion](https://rhsfinancial.com/2017/06/20/line-aggressive-crazy-leverage/) Calculator (Annualized)"
)
# Text input for yearly expected return
input_er = st.number_input(
"Expected Yearly Return [%]", value=constants.ann_return
)
# Text input for yearly risk free rate
input_risk_free = st.number_input(
"Risk Free Yearly Return [%] (Costs)", value=constants.ann_risk_free
)
# Text input for yearly return volatility
input_volatility = st.number_input(
"Annualized Volatility of the Underlying [%]", value=constants.ann_vol
)
# Display the result
result = update_result(input_er, input_risk_free, input_volatility)
st.write(result)
# Header for the plot
st.markdown("", unsafe_allow_html=True)
st.write("## Gain of the LETF over the Unleveraged ETF")
st.markdown(
"""
The difference in profit of the hypothetical leveraged ETF is
compared to the unleveraged ETF below. It becomes clear that
the profit does not increase linearly with leverage and the
margin of error becomes smaller and smaller, especially with
higher leverage.
"""
)
# Dropdown for the plot style
data_source = st.selectbox("Plot Style", ["Heatmap", "Contour"], index=1)
# Slider for leverage
leverage = st.slider(
"Exposure to the Market (Leverage) [%]",
min_value=0.0,
max_value=1000.0,
value=constants.lev_r * 100,
step=10.0,
)
# Slider for TER
ter = st.slider(
"TER of the ETF [%]",
min_value=0.0,
max_value=2.0,
value=constants.exp_r,
step=0.05,
)
# Slider for LIBOR
libor = st.slider(
"LIBOR rate [%]",
min_value=0.0,
max_value=4.0,
value=constants.libor,
step=0.1,
)
# Placeholder for the graph
st.plotly_chart(
update_plot(data_source, leverage, ter, libor), use_container_width=True
)
# Header for the Kelly Criterion plot
st.markdown("", unsafe_allow_html=True)
st.write("## Ideal Market Exposure (Leverage) According to the Kelly Criterion")
st.markdown(
"""
In contrast to the previous figure, the ideal leverage factor, which
is determined by the Kelly criterion, shows an even stronger dependence
on volatility and thus an even smaller margin of error. This is due
to the fact that the Kelly criterion maximizes the expected geometric
growth rate, thus avoiding the
['Just One More Paradox'](https://www.youtube.com/watch?v=_FuuYSM7yOo&),
which describes the phenomenon of a median loss even though the expected
value of every bet and thus the overall expected value (mean) is positive.
""",
unsafe_allow_html=True,
)
# Dropdown for the plot style
data_source_kelly = st.selectbox(
"Plot Style", ["Heatmap", "Contour"], index=1, key="kelly"
)
# Slider for the risk free rate
risk_free_rate = st.slider(
"Risk Free Yearly Return [%] (Costs)",
min_value=0.0,
max_value=8.0,
value=3.0,
step=0.25,
)
# Placeholder for the graph
st.plotly_chart(
update_kelly_plot(data_source_kelly, risk_free_rate),
use_container_width=True,
)
with tab2:
# Header for the ticker price data plot
st.markdown("", unsafe_allow_html=True)
st.write("## What Does All This Imply for My Investments?")
st.markdown(
"""
Depending on whether you want to pursue a maximally aggressive Kelly strategy
or whether you are aiming for an annualized volatility of 20%, for example,
you can find suggestions below in view of current stock market prices as well
as forecasts for the percentage-at-risk (PaR) at empirical 1% and 5% levels
based on the data of the past 2 years (if available) in order to put the Kelly
suggestions into perspective.\n
Keep in mind that an overestimation of returns and and an underestimation of
volatility can lead to an inflated allocation fraction and thus a significant
loss of capital <a href="#footnote-1">[1]</a>. The Kelly Criterion is a
powerful tool, but it is not a guarantee for success. It is important to use
it in conjunction with other risk management strategies and to be aware of the
limitations of the model and its assumptions.\n
Further Reading:
<p id="footnote-1">[1] E. Thorp, <a href=
"https://www.eecs.harvard.edu/cs286r/courses/fall12/papers/Thorpe_KellyCriterion2007.pdf">
THE KELLY CRITERION IN BLACKJACK SPORTS BETTING, AND THE STOCK MARKET</a>,
Chapter 9, Handbook of Asset and Liability Management.</p>
For the practical part, here are some common ticker symbols:<br>
- ^GSPC: S&P 500 Index<br>
- ^NDX: Nasdaq 100 Index<br>
- AAPL: Apple Inc.<br>
- MSFT: Microsoft Corporation<br>
- NVDA: NVIDIA Corporation<br>
- ^GDAXI: DAX40 Index<br>
- ^STOXX50E: EURO STOXX 50 Index<br>
- ^STOXX: STOXX Europe 600 Index<br>
- ^N225: Nikkei 225 Index<br>
- SPY: SPDR S&P 500 ETF<br>
- SJIM: Inverse Cramer ETF\n
For a list of all available ticker symbols, please visit
https://finance.yahoo.com/lookup or https://finance.yahoo.com/most-active/
""",
unsafe_allow_html=True,
)
# Ticker string input
ticker_symbol = st.text_input("Ticker Symbol", value="^GSPC")
# Slider for the risk free rate
risk_free_rate_for_ticker = st.slider(
"Risk Free Yearly Return [%] (Costs)",
min_value=0.0,
max_value=8.0,
value=3.0,
step=0.25,
key="ticker",
)
st.plotly_chart(
update_ticker_plot(ticker_symbol, risk_free_rate_for_ticker),
use_container_width=True,
)
# Checkbox for tax
include_tax = st.checkbox(
"Include Taxes on Capital Gains (incl. 'Solidaritätszuschlag')", value=True
)
# Slider for the expenses of the derivatives
derivative_expenses = st.slider(
"Yearly Expense Ratio of the Derivatives [%]",
min_value=1.0,
max_value=25.0,
value=3.0,
step=1.0,
)
# Slider for the transaction costs of the derivatives
rel_transact_costs = st.slider(
"Transaction Costs for Buying and Selling (Separately) [%]",
min_value=0.0,
max_value=5.0,
value=3.0,
step=0.5,
)
# Slider for the Kelly leverage time window
look_back_window = st.slider(
"Kelly Leverage Look-Back Window [Trading Days]",
min_value=10,
max_value=100,
value=60,
step=10,
format="%d",
)
# Slider for the holding period of the derivatives
holding_period = st.slider(
"Holding Period of the Derivatives [Trading Days]",
min_value=5,
max_value=150,
value=15,
step=5,
format="%d",
)
# Slider for the Kelly leverage signal
leverage_signal = st.slider(
"Kelly Leverage Signal",
min_value=-5.0,
max_value=10.0,
value=5.0,
step=0.5,
)
# Slider for the leverage of the derivatives
derivative_leverage = st.slider(
"Leverage of the Derivatives",
min_value=0.0,
max_value=10.0,
value=3.0,
step=0.25,
)
# Calculate the performance of the derivatives
data_dict = get_derivatives_data(
ticker_symbol,
risk_free_rate_for_ticker,
derivative_leverage,
derivative_expenses,
rel_transact_costs,
look_back_window,
holding_period,
leverage_signal,
include_tax,
)
# Display aggregated statistics of the derivatives
win_ratio_ko = data_dict["win_ratio_ko"]
win_ratio_f = data_dict["win_ratio_f"]
reward_ko = data_dict["reward_ko"]
reward_f = data_dict["reward_f"]
# Update the derivatives performance plot
derivates_fig = update_derivatives_performance_plot(
data_dict,
derivative_leverage,
holding_period,
leverage_signal,
)
st.write(
f"Win ratio over the past {constants.five_years} trading days (KO / Factor):"
+ f" {win_ratio_ko:.1f}%/ {win_ratio_f:.1f}%. Average risk (loss)"
+ f" vs. average reward (win) per trade over the past {constants.five_years}"
+ f" trading days (KO/ Factor): 1:{reward_ko:.2f}/ 1:{reward_f:.2f}"
+ r" (take the inverse of this ratio as an estimate for $b$ in the Kelly formula)."
)
st.plotly_chart(
derivates_fig,
use_container_width=True,
)
# Add a calibration plot for the derivatives performance
calibration_fig = update_derivates_calibration_plot(
data_dict, derivative_leverage, holding_period
)
st.plotly_chart(
calibration_fig,
use_container_width=True,
)
with tab3:
# Header for the Stock Screener
st.markdown("", unsafe_allow_html=True)
st.write("## Stock Universe Screener (Work in Progress)")
# TODO: Display the failed download log
with st.spinner("Fetching data (might take up to 5 minutes)..."):
data = download_universe()
# Display the output in Streamlit
st.success("Data fetched successfully!")
# Filter data based on the "Volume" column
filter_by_volume = st.checkbox(
"Keep only Stocks with High Trading Volume", value=True
)
if filter_by_volume:
# Filter data based on the "Volume" column
sufficient_volume = (data["Volume"] > 100000).all(axis=0)
# Remove columns with insufficient volume
filtered_cols = [
col
for i, col in enumerate(data["Volume"].columns)
if sufficient_volume.iloc[i] == True
]
else:
# Keep all columns
filtered_cols = data["Volume"].columns
# Display the past 60 days of the adjusted close data
n_days_60 = 60
adj_close_data = data.xs("Adj Close", level=0, axis=1).iloc[-(n_days_60 + 1) :]
adj_close_data = adj_close_data[filtered_cols]
n_assets = adj_close_data.shape[1]
st.markdown("", unsafe_allow_html=True)
st.write(
f"### Past {n_days_60} Days of Adjusted Close Data for {n_assets} Assets"
)
st.dataframe(data=adj_close_data.tail(10))
# Header for the stock universe indicators
st.markdown("", unsafe_allow_html=True)
st.write("## Indicators for the Stock Universe")
st.markdown(
"""
### Kelly Criterion \n
Filter for stocks with a low volatility compared to their past 60 day
(30 day) returns and display the average daily range (ADR) of the
corresponding stocks.
"""
)
# Slider for the risk free rate
risk_free_rate_u = st.slider(
"Risk Free Yearly Return [%] (Costs)",
min_value=0.0,
max_value=8.0,
value=3.0,
step=0.25,
key="stock_universe",
)
# Calculate the Kelly leverage factor for the stock universe for the past n_days
lev_60, lev_gt_10_60, largest_20_60 = kelly_selection(
adj_close_data, risk_free_rate_u, n_days_60
)
# Repeat for a different time window
n_days_30 = 30
lev_30, lev_gt_10_30, largest_20_30 = kelly_selection(
adj_close_data, risk_free_rate_u, n_days_30
)
# Calculate the average daily range for the stock universe
high_data = data.xs("High", level=0, axis=1).iloc[-n_days_60:]
high_data = high_data[filtered_cols]
low_data = data.xs("Low", level=0, axis=1).iloc[-n_days_60:]
low_data = low_data[filtered_cols]
# Calculate the average daily range for the past n_days
dr_60, adr_60 = compute_adr(high_data, low_data, n_days_60)
dr_30, adr_30 = compute_adr(
high_data.iloc[-n_days_30:], low_data.iloc[-n_days_30:], n_days_30
)
# Create columns for the indicators
col1, col2, col3, col4 = st.columns(4)
# Use the columns for display
col1.write(
f"Kelly leverage factor > 10 in the past {n_days_60} days:"
f" {len(lev_gt_10_60)} Stocks. Top 20:"
)
col1.dataframe(data=largest_20_60)
col2.write(
f"Kelly leverage factor > 10 in the past {n_days_30} days:"
f" {len(lev_gt_10_30)} Stocks. Top 20:"
)
col2.dataframe(data=largest_20_30)
col3.write(f"Average daily range [%] in the past {n_days_60} days:")
col3.dataframe(data=adr_60[largest_20_60.index])
col4.write(f"Average daily range [%] in the past {n_days_30} days:")
col4.dataframe(data=adr_30[largest_20_30.index])
st.markdown(
"""
### Average Daily Range \n
Filter for stocks with a high average daily range (ADR) in the past
60 days (30 days) and a positive trend.
"""
)
# Filter for a positive trend in the past n_days
ret_60, ret_10_60 = positive_return_selection(adj_close_data, n_days_60)
top_adr_60 = adr_60[ret_10_60].nlargest(20).round(2)
top_adr_60.name = "ADR [%]"
ret_30, ret_10_30 = positive_return_selection(adj_close_data, n_days_30)
top_adr_30 = adr_30[ret_10_30].nlargest(20).round(2)
top_adr_30.name = "ADR [%]"
# Create columns for the indicators
col1, col2, col3, col4 = st.columns(4)
# Use the columns for display
col1.write(f"Top 20 ADR stocks in the past {n_days_60} days:")
col1.dataframe(data=top_adr_60)
col2.write(f"Top 20 ADR stocks in the past {n_days_30} days:")
col2.dataframe(data=top_adr_30)
col3.write(f"Return [%] past {n_days_60} days:")
col3.dataframe(data=ret_60[top_adr_60.index])
col4.write(f"Return [%] in the past {n_days_30} days:")
col4.dataframe(data=ret_30[top_adr_30.index])
st.markdown(
"""
### Custom Filters \n
Filter stocks for custom Kelly leverage, average daily range (ADR),
and volatility in the past 60 days (30 days).
"""
)
# Slider for the Kelly filter
kelly_filter = st.slider(
"Min. Kelly Leverage [%]",
min_value=2.5,
max_value=30.0,
value=5.0,
step=2.5,
)
# Slider for the ADR filter
adr_filter = st.slider(
"Min. Average Daily Range (ADR) [%]",
min_value=0.0,
max_value=10.0,
value=1.5,
step=0.5,
)
# Slider for the volatility filter
volatility_filter = st.slider(
"Max. Annualized Volatility [%]",
min_value=5.0,
max_value=100.0,
value=15.0,
step=5.0,
)
# Apply filters to the leverage factor
filter_lev_60 = lev_60 > kelly_filter
filter_lev_30 = lev_30 > kelly_filter
# Apply filters to the average daily range
filter_adr_60 = adr_60 > adr_filter
filter_adr_30 = adr_30 > adr_filter
# Apply filters to the volatility
vol_60 = volatility_selection(adj_close_data, n_days_60)
filter_vol_60 = vol_60 < volatility_filter
vol_30 = volatility_selection(adj_close_data, n_days_30)
filter_vol_30 = vol_30 < volatility_filter
# Combine the filters
combined_60 = filter_lev_60 & filter_adr_60 & filter_vol_60
combined_30 = filter_lev_30 & filter_adr_30 & filter_vol_30
# Create new dataframes for the filtered stocks
filtered_data_60 = adj_close_data.iloc[-1].T.round(2).to_frame()[combined_60]
filtered_data_60.columns = ["Adj. Close"]
filtered_data_60["Adj. Close"] = filtered_data_60["Adj. Close"].astype(
"float64"
)
filtered_data_60["Kelly Leverage"] = lev_60.round(2)[combined_60]
filtered_data_60["ADR [%]"] = adr_60[combined_60]
filtered_data_60["Volatility [%]"] = vol_60[combined_60]
filtered_data_30 = adj_close_data.iloc[-1].T.round(2).to_frame()[combined_30]
filtered_data_30.columns = ["Adj. Close"]
filtered_data_30["Adj. Close"] = filtered_data_30["Adj. Close"].astype(
"float64"
)
filtered_data_30["Kelly Leverage"] = lev_30.round(2)[combined_30]
filtered_data_30["ADR [%]"] = adr_30[combined_30]
filtered_data_30["Volatility [%]"] = vol_30[combined_30]
# Sort the dataframes by the Kelly leverage
filtered_data_60 = filtered_data_60.sort_values(
by="Kelly Leverage", ascending=False
)
filtered_data_30 = filtered_data_30.sort_values(
by="Kelly Leverage", ascending=False
)
# Create columns for the indicators
col1, col2 = st.columns(2)
# Use the columns for display
col1.write(f"Filtered stocks ({n_days_60} day window): {len(filtered_data_60)}")
col1.dataframe(data=filtered_data_60)
col2.write(f"Filtered stocks ({n_days_30} day window): {len(filtered_data_30)}")
col2.dataframe(data=filtered_data_30)