# 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("CRWD", "AMZN", "SHOP","TTD", "NVDA")

prices <- tq_get(x = symbols, 
                 get  = "stock.prices", 
                 from = "2021-01-01")

2 Convert prices to returns

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMZN" "CRWD" "NVDA" "SHOP" "TTD"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)

w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMZN       0.25
## 2 CRWD       0.25
## 3 NVDA       0.2 
## 4 SHOP       0.2 
## 5 TTD        0.1

4 Build a portfolio

# ?tq_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: 45 × 2
##    date         returns
##    <date>         <dbl>
##  1 2021-02-26  0.0378  
##  2 2021-03-31 -0.0978  
##  3 2021-04-30  0.110   
##  4 2021-05-28  0.00183 
##  5 2021-06-30  0.149   
##  6 2021-07-30 -0.000161
##  7 2021-08-31  0.0648  
##  8 2021-09-30 -0.0992  
##  9 2021-10-29  0.105   
## 10 2021-11-30  0.0333  
## # ℹ 35 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")

6 Plot: Rolling Sharpe Ratio

# Create a custom function to calculate rolling sharpe ratio
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: 22 × 2
##    date       rolling_sr
##    <date>          <dbl>
##  1 2023-01-31    -0.148 
##  2 2023-02-28    -0.155 
##  3 2023-03-31    -0.0721
##  4 2023-04-28    -0.120 
##  5 2023-05-31    -0.0432
##  6 2023-06-30    -0.0768
##  7 2023-07-31    -0.0502
##  8 2023-08-31    -0.0711
##  9 2023-09-29    -0.0652
## 10 2023-10-31    -0.112 
## # ℹ 12 more rows
rolling_sr_tbl %>%
    
    ggplot(aes(x = date, y = rolling_sr)) +
    geom_line(color = "cornflowerblue") +
    
    # 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?

Since 2023 my portfolio has done very well, from around October of 2023, to July 2024 it was steadily increasing. There was one break right after July of 2024. I think a big part of this is CRWD causing the major cybersecurity outage around that time. At that point the stock fell tremendously, which is when I decided to buy the stock in the first place.