# 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

symbol <- c("GME", "MSFT", "INTC", "XXII", "TSLA")

prices <- tq_get(x = symbol,
                 get = "stock.prices",
                 from = "2012-12-31",
                 to = "2017-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 <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "GME"  "INTC" "MSFT" "TSLA" "XXII"
weight <- c(0.2,0.2,0.2,0.2,0.2)
weight
## [1] 0.2 0.2 0.2 0.2 0.2
w_tbl <- tibble(symbols, weight)

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: 60 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-01-31  0.0439 
##  2 2013-02-28 -0.0381 
##  3 2013-03-28  0.102  
##  4 2013-04-30  0.0861 
##  5 2013-05-31  0.123  
##  6 2013-06-28  0.101  
##  7 2013-07-31  0.212  
##  8 2013-08-30 -0.00238
##  9 2013-09-30  0.0468 
## 10 2013-10-31 -0.0139 
## # ℹ 50 more rows

5 Compute Sharpe Ratio

 rfr <- 0.0003

portfolio_SharpRatio <- portfolio_returns_tbl %>%
    
    tq_performance(Ra              = returns, 
                   performance_fun = SharpeRatio,
                   Rf              = rfr,
                   FUN             = "StdDev")

portfolio_SharpRatio
## # A tibble: 1 × 1
##   `StdDevSharpe(Rf=0%,p=95%)`
##                         <dbl>
## 1                       0.266

6 Plot: Rolling Sharpe Ratio

Calculate_rolling_SharpRatio <- function(data) {
    
    
    rolling_SR <- SharpeRatio(R   = data, 
                              Rf  = rfr, 
                              FUN = "StdDev")
    return(rolling_SR)
}

# Define Window
window <- 24


# Transform Data: calculate Rolling Sharp Ratio

rolling_sr_tbl <- portfolio_returns_tbl %>%
    
    tq_mutate(select     = returns,
              mutate_fun = rollapply, 
              width      = window, 
              FUN        = Calculate_rolling_SharpRatio,
              col_rename = "rolling_sr") %>%
    select(-returns) %>%
    na.omit()



rolling_sr_tbl %>%
    
    ggplot(aes(x = date,
               y = rolling_sr)) +
    geom_line(color = "cornflowerblue") +
    
    # Labeling
    labs(x = NULL, y = "Rolling Sharp Ratio") +
    
    annotate(geom  = "text",
             x     = as.Date("2016-06-01"),
             y     = 0.5,
             label = "This portfolio plummeted during 2015 into 2016 
             and began to rise at the end of 2017",
             color = "red",
             size  = 5)

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?
The Portfolio has not performed great, in 2015 through 2016 it sustained a significant downhill slope leading to losses. It maintained an average of -0.15 rolling Sharpe ratio throughout 2016 and bbegain to rise to a peak of 0.25 rolling sharp at the end of 2017.