# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Visualize and examine changes in the underlying trend in the performance of your portfolio in terms of Sharpe Ratio.

Choose your stocks.

from 2017-12-31 to present

1 Import stock prices

# Choose stocks
symbols <- c("MSFT", "JPM", "GM", "TMUS", "IRVRF")

prices <- tq_get(x    = symbols, 
                get  = "stock.prices",
                from = "2017-12-31", 
                to   = "2022-12-31")

2 Convert prices to returns (monthly)

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"))

3 Assign a weight to each asset (change the weigting scheme)

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "GM"    "IRVRF" "JPM"   "MSFT"  "TMUS"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
weights
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 GM         0.25
## 2 IRVRF      0.25
## 3 JPM        0.2 
## 4 MSFT       0.2 
## 5 TMUS       0.1

4 Build a portfolio

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: 59 × 2
##    date        returns
##    <date>        <dbl>
##  1 2018-02-28  0.0296 
##  2 2018-03-29 -0.0283 
##  3 2018-04-30  0.0459 
##  4 2018-05-31  0.00510
##  5 2018-06-29 -0.0272 
##  6 2018-07-31 -0.00462
##  7 2018-08-31  0.0213 
##  8 2018-09-28 -0.0278 
##  9 2018-10-31  0.0614 
## 10 2018-11-30  0.0806 
## # … with 49 more rows

5 Compute Sharpe Ratio

# Define risk free rate
rfr <- 0.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
##   `StdDevSharpe(Rf=0%,p=95%)`
##                         <dbl>
## 1                      0.0641

6 Plot: Rolling Sharpe Ratio

# 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 <- 10

# 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: 50 × 2
##    date       rolling_sr
##    <date>          <dbl>
##  1 2018-11-30      0.393
##  2 2018-12-31      0.134
##  3 2019-01-31      0.327
##  4 2019-02-28      0.302
##  5 2019-03-29      0.253
##  6 2019-04-30      0.477
##  7 2019-05-31      0.462
##  8 2019-06-28      0.553
##  9 2019-07-31      0.683
## 10 2019-08-30      0.412
## # … with 40 more rows
# Plot
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") +
    theme(plot.title = element_text(hjust = 0.5)) +
    
    annotate(geom = "text", x = as.Date("2020-06-01"), y = -0.18,
             label = "Large dip at the start of the pandemic.", color = "red", size = 3) +
    annotate(geom = "text", x = as.Date("2022-06-01"), y = 0.25,
             label = "Large dip as Fed raisies interest rates.", color = "red", size = 3)

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?

When interpreting the portfolio’s risk adjusted returns for the last five years, it is clear that the portfolio performance does not generate superior returns for holding riskier assets. In the Sharpe Ratio calculation we have set the risk free rate to be 3%, however the actual 10 year treasury yields have been shrinking due to inflation premiums. In theory this would reduce the portfolio’s overall Sharpe, which hardly exceeds half a percent, as seen in the 24-month rolling Sharpe plot. There are two notable structural breaks, one in the early months of 2020, as well as one starting near the beginning of 2021 and lasting mid-way through 2022. Much of this movement can be attributed to the excess volatility endured during a period of uncertainty, with recessionary concerns stemming from across the U.S economy. The central bank has been on a crusade of raising interest rates, dampening the amount of money flowing into the economy and slowing the growth of large-cap companies that make up the portfolio. Generally speaking, a Sharpe as low as what is seen with this portfolio would be unattractive to the average investor. With that in mind, it may be worthwhile to build a portfolio that counteracts inflationary pressures by including a larger allocation in bonds over equities. Diversification would raise the Portfolio’s Sharpe and could adequately compensate the investor for holding assets with slightly higher units of risk.