# 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("TSLA", "GM", "F", "VWAGY", "HMC")
prices <- tq_get(x = symbols, 
                    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
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "F"     "GM"    "HMC"   "TSLA"  "VWAGY"
# weights
weight <- c(0.15, 0.25, 0.2, 0.3, 0.1)
weight
## [1] 0.15 0.25 0.20 0.30 0.10
w_tbl <- tibble(symbols, weight)
w_tbl
## # A tibble: 5 × 2
##   symbols weight
##   <chr>    <dbl>
## 1 F         0.15
## 2 GM        0.25
## 3 HMC       0.2 
## 4 TSLA      0.3 
## 5 VWAGY     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: 60 × 2
##    date       returns
##    <date>       <dbl>
##  1 2013-01-31  0.0357
##  2 2013-02-28 -0.0476
##  3 2013-03-28  0.0340
##  4 2013-04-30  0.150 
##  5 2013-05-31  0.220 
##  6 2013-06-28  0.0120
##  7 2013-07-31  0.115 
##  8 2013-08-30  0.0404
##  9 2013-09-30  0.0750
## 10 2013-10-31 -0.0302
## # … with 50 more rows

5 Compute Sharpe Ratio

# Define risk free rate
rfr <- 0.0003

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

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

6 Plot: Rolling Sharpe Ratio

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_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: 37 × 2
##    date       rolling_sr
##    <date>          <dbl>
##  1 2014-12-31     0.357 
##  2 2015-01-30     0.306 
##  3 2015-02-27     0.383 
##  4 2015-03-31     0.349 
##  5 2015-04-30     0.306 
##  6 2015-05-29     0.242 
##  7 2015-06-30     0.221 
##  8 2015-07-31     0.124 
##  9 2015-08-31     0.0284
## 10 2015-09-30    -0.0763
## # … with 27 more rows
    rolling_sr_tbl %>%
        
    ggplot(aes(x = date, y = rolling_sr)) +
    geom_line(color = "cornflowerblue") + 
        
    # Labeling
    labs(x = NULL, y = "Rolling Sharpe Ratio") +
        
    annotate(geom = "text",
             x = as.Date("2016-06-01"), y = 0.5,
             label = "This portfolio has not been doing well.",
             color = "purple", 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 early 2016. What do you think the reason is?

A structural break for my portfolio also occurred in early 2016 and another in the middle of 2016. It took a steep decline through 2015, and bounced between -1 and -2 throughout 2016. Where it started increasing again in late 2016. But investors like to see the sharpe ratio above 1, with 1 signaling a good investment and so on. With the portfolio hovering alot around 0, it is not a good signal to investors. The portfolio did go above 0 in the middle of 2017 and increased until late 2017 where it dropped a little to a rolling sharpe ratio of 1.5 which is good. The upward shift shows a little potential for this portfolio.  
Looking into why there may have been structural breaks in early 2016, a couple sources had pointed out that global stock markets were falling due to poor reports regarding China's slowing economy. When China's stock market fell, everyone worried of deflation and depression and began withdrawing their moeny.