# 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 2012-12-31 to present

1 Import stock prices

symbols <- c("COST", "LOW", "HD", "KR")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",
                 from = "2012-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"))

asset_returns_tbl
## # A tibble: 668 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 COST  2013-01-31  0.0359 
##  2 COST  2013-02-28 -0.00765
##  3 COST  2013-03-28  0.0465 
##  4 COST  2013-04-30  0.0216 
##  5 COST  2013-05-31  0.0138 
##  6 COST  2013-06-28  0.00854
##  7 COST  2013-07-31  0.0601 
##  8 COST  2013-08-30 -0.0458 
##  9 COST  2013-09-30  0.0291 
## 10 COST  2013-10-31  0.0243 
## # ℹ 658 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "COST" "HD"   "KR"   "LOW"
# weights
weights <- c(0.35, 0.25, 0.25, 0.15)
weights
## [1] 0.35 0.25 0.25 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 COST       0.35
## 2 HD         0.25
## 3 KR         0.25
## 4 LOW        0.15

4 Build a portfolio

portfolio_retuns_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col   = asset,
                 returns_col  = returns,
                 weights      = w_tbl,
                 rebalance_on = "months",
                 col_rename = "returns")

portfolio_retuns_tbl
## # A tibble: 167 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-01-31  0.0594 
##  2 2013-02-28  0.0176 
##  3 2013-03-28  0.0529 
##  4 2013-04-30  0.0318 
##  5 2013-05-31  0.0319 
##  6 2013-06-28  0.00242
##  7 2013-07-31  0.0716 
##  8 2013-08-30 -0.0434 
##  9 2013-09-30  0.0461 
## 10 2013-10-31  0.0374 
## # ℹ 157 more rows

5 Compute Sharpe Ratio

# Define risk free rate
rfr <- 0.0003


portfolio_SharpeRatio_tbl <- portfolio_retuns_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.268

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 <- 24

# Transform data: calculate rolling sharpe ratio
rolling_sr_tbl <- portfolio_retuns_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.676
##  2 2015-01-30      0.641
##  3 2015-02-27      0.679
##  4 2015-03-31      0.660
##  5 2015-04-30      0.481
##  6 2015-05-29      0.476
##  7 2015-06-30      0.439
##  8 2015-07-31      0.435
##  9 2015-08-31      0.432
## 10 2015-09-30      0.412
## # ℹ 134 more rows
# Plot
rolling_sr_tbl %>%
    
    ggplot(aes(x = date, y = rolling_sr)) +
    geom_line(color = "cornflowerblue") +
    
    scale_x_date(date_breaks = "1 year",
                 date_labels = "%Y") +

    
    # Labeling
    labs(x = NULL, y = "Rolling 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?

My portfolio is quite volatile, having four structural breaks since 2015. There is a downward trend from 2015 to late 2017, this might be caused by the devaluation of the Chinese Yuan and the Fed hiking the interest rate for the first time in nine years. Next, it has an upward trend from 2018 to mid 2022 this is probably due to the Fed cutting the interest rates three times during 2019. There is another dip from 2022 to late 2023 witch might be due to inflation hitting a 40 year high and interest rates rising at the fastest pace in decades. Another possible cause for 2022 is the start of the Russia-Ukraine War. It performs better in 2024 which is due to the AI boom. The final segment is a large dip below 0.0 Sharpe Ratio in 2025 and 2026.