# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize and examine changes in the underlying trend in the performance of your portfolio in terms of Sharpe Ratio.
Choose your stocks.
from 2012-12-31 to present
symbols <- c("WMT", "AAPL", "NFLX", "GOOGL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31")
prices
## # A tibble: 13,932 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 WMT 2012-12-31 22.5 22.8 22.5 22.7 21037500 17.4
## 2 WMT 2013-01-02 23.0 23.1 22.8 23.1 31172400 17.7
## 3 WMT 2013-01-03 23.1 23.1 22.8 22.9 26730300 17.6
## 4 WMT 2013-01-04 22.9 23.1 22.8 23.0 19314000 17.7
## 5 WMT 2013-01-07 22.9 23.0 22.7 22.8 18604200 17.5
## 6 WMT 2013-01-08 22.8 23.0 22.7 22.9 17600700 17.5
## 7 WMT 2013-01-09 22.9 22.9 22.7 22.9 15165600 17.5
## 8 WMT 2013-01-10 22.9 23.0 22.6 22.8 34361400 17.5
## 9 WMT 2013-01-11 22.9 22.9 22.7 22.9 18673500 17.6
## 10 WMT 2013-01-14 22.8 22.9 22.7 22.8 16471200 17.5
## # ℹ 13,922 more rows
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
asset_returns_tbl
## # A tibble: 668 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AAPL 2013-01-31 -0.156
## 2 AAPL 2013-02-28 -0.0256
## 3 AAPL 2013-03-28 0.00285
## 4 AAPL 2013-04-30 0.000271
## 5 AAPL 2013-05-31 0.0222
## 6 AAPL 2013-06-28 -0.126
## 7 AAPL 2013-07-31 0.132
## 8 AAPL 2013-08-30 0.0804
## 9 AAPL 2013-09-30 -0.0217
## 10 AAPL 2013-10-31 0.0920
## # ℹ 658 more rows
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "GOOGL" "NFLX" "WMT"
# weights
weights <- c(0.35, 0.30, 0.20, 0.15)
weights
## [1] 0.35 0.30 0.20 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.35
## 2 GOOGL 0.3
## 3 NFLX 0.2
## 4 WMT 0.15
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months",
col_rename = "returns")
portfolio_returns_tbl
## # A tibble: 167 × 2
## date returns
## <date> <dbl>
## 1 2013-01-31 0.0849
## 2 2013-02-28 0.0362
## 3 2013-03-28 0.00894
## 4 2013-04-30 0.0435
## 5 2013-05-31 0.0287
## 6 2013-06-28 -0.0555
## 7 2013-07-31 0.0849
## 8 2013-08-30 0.0350
## 9 2013-09-30 0.0216
## 10 2013-10-31 0.0949
## # ℹ 157 more rows
# Define Risk Free Rate
rfr <- .0003
portfolio_SharpeRatio_tbl <- portfolio_returns_tbl %>%
tq_performance(Ra = returns,
performance_fun = SharpeRatio,
Rf = rfr,
FUN = "StdDev")
portfolio_SharpeRatio_tbl
## # A tibble: 1 × 1
## `SharpeRatio(Rf=0%,p=95%)`
## <dbl>
## 1 0.311
# Create a custom function to calculate rolling SR
Calculate_rolling_SharpeRatio <- function(data) {
rolling_SR <- SharpeRatio(R = data,
Rf = rfr,
FUN = "StdDev")
return(rolling_SR)
}
# Define window
window <- 24
# Transform data: Calculate Rolling Sharpe Ratio
rolling_sr_tbl <- portfolio_returns_tbl %>%
tq_mutate(select = returns,
mutate_fun = rollapply,
width = window,
FUN = Calculate_rolling_SharpeRatio,
col_rename = "rolling_sr") %>%
select(-returns) %>%
na.omit()
rolling_sr_tbl
## # A tibble: 144 × 2
## date rolling_sr
## <date> <dbl>
## 1 2014-12-31 0.534
## 2 2015-01-30 0.531
## 3 2015-02-27 0.547
## 4 2015-03-31 0.474
## 5 2015-04-30 0.478
## 6 2015-05-29 0.479
## 7 2015-06-30 0.546
## 8 2015-07-31 0.546
## 9 2015-08-31 0.453
## 10 2015-09-30 0.392
## # ℹ 134 more rows
rolling_sr_tbl %>%
ggplot(aes(x = date, y = rolling_sr)) +
geom_line(color = "cornflowerblue") +
# Labeling
labs(x = NULL, y = "Rolling Sharpe Ratio", title = "Rolling 24-Month Sharpe Ratio")
How has your portfolio performed over time? Provide dates of the structural breaks, if any. The Code Along Assignment 9 had one structural break in November 2016. What do you think the reason is?
Over time, the sharpe calculations for this portfolio are nothing short of all over the place. There are constant fluctuations both increasing and decreasing sharply. According to the rolling_sr_tbl, the major structural breaks occur at 6/30/2016, 6/29/2018, 3/31/2020, 8/31/2021, and 12/29/2023 to where to spiked for a final time to about 0.7 ratio and it has sharply declined downhill until today.
As stated, the fluctuations in my portfolio are quite wild. it ranges from as high as 0.7 and as low as what looks like -0.5 covering all values in between. The biggest structural break, occurring in March of 2020, can be attributed to the Covid pandemic. During this time, the stock market encountered a complete crash on all levels. Volatility during this time likely spiked causing the performance of this portfolio to drop further and further until risk free rate bonds ended up outperforming the stocks in this portfolio which is bad. However, in August of 2021 when the Sharpe ratio spiked again, this is due to the post pandemic recovery in the market. For example, both Apple and Walmart heavily benefited from strong consumer demand and this pushed the performance of the portfolio higher and higher. There are other structural breaks to be discussed, but overall the Sharpe ratio in terms of overall performance above risk, is nothing special. Reaching a peak of 0.7 simply means that this portfolio produced an additional 0.7 units of returns above its standard deviation. For a sharpe ratio to be considered “good” it should maintain a rating of at least 1.0 for some time but it never even reaches that here.