# 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("NXPI", "IFX.DE", "AMD", "ON")

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

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
  group_by(symbol) %>%
    
  tq_transmute(
    select       = adjusted,
    mutate_fun   = periodReturn,
    period       = "monthly",
    type         = "arithmetic",
    col_rename   = "returns") %>%
    
  slice(-1) %>%
    
  ungroup() %>%
    
  rename(asset = symbol)

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull(asset)
symbols
## [1] "AMD"    "IFX.DE" "NXPI"   "ON"
# weights
weights <- c(0.25, 0.25, 0.25, 0.25)
weights
## [1] 0.25 0.25 0.25 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMD        0.25
## 2 IFX.DE     0.25
## 3 NXPI       0.25
## 4 ON         0.25

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: 63 × 2
##    date       returns
##    <date>       <dbl>
##  1 2020-02-28 -0.0895
##  2 2020-03-31 -0.222 
##  3 2020-04-30  0.226 
##  4 2020-05-29  0.0330
##  5 2020-06-30  0.119 
##  6 2020-07-31  0.138 
##  7 2020-08-31  0.0941
##  8 2020-09-30 -0.0114
##  9 2020-10-30  0.0372
## 10 2020-11-30  0.196 
## # ℹ 53 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.182

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(as.numeric(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: 40 × 2
##    date       rolling_sr
##    <date>          <dbl>
##  1 2021-11-30      0.464
##  2 2021-12-30      0.528
##  3 2021-12-31      0.809
##  4 2022-01-31      0.587
##  5 2022-02-28      0.548
##  6 2022-03-31      0.466
##  7 2022-04-29      0.285
##  8 2022-05-31      0.297
##  9 2022-06-30      0.167
## 10 2022-07-29      0.231
## # ℹ 30 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?

My portfolio performed best in January 2022, then declined throughout the year. It started recovering around November 2023 and improved during 2024. January 2022 and November 2023 look like possible structural breaks because the trend changed direction around those dates

I think the November 2016 break was related to Trump winning the election. Investors expected tax cuts and fewer regulations, which helped stocks rise and likely contributed to the portfolio’s improving Sharpe ratio