# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Visualize and compare skewness of your portfolio and its assets.

Choose your stocks.

from 2012-12-31 to 2017-12-31

1 Import stock prices

symbols <- c("TM", "SBUX", "AEO", "BBW")
prices <- tq_get(x = symbols, 
                 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 = "quarterly", 
                 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] "AEO"  "BBW"  "SBUX" "TM"
# weights
weights <- c(0.25, 0.25, 0.2, 0.3)
weights
## [1] 0.25 0.25 0.20 0.30
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AEO        0.25
## 2 BBW        0.25
## 3 SBUX       0.2 
## 4 TM         0.3

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: 20 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-03-28  0.108  
##  2 2013-06-28  0.103  
##  3 2013-09-30  0.0238 
##  4 2013-12-31  0.0187 
##  5 2014-03-31 -0.00730
##  6 2014-06-30  0.0916 
##  7 2014-09-30  0.0548 
##  8 2014-12-31  0.136  
##  9 2015-03-31  0.112  
## 10 2015-06-30 -0.0356 
## 11 2015-09-30 -0.00379
## 12 2015-12-31 -0.0806 
## 13 2016-03-31 -0.00559
## 14 2016-06-30 -0.0278 
## 15 2016-09-30  0.00508
## 16 2016-12-30  0.0425 
## 17 2017-03-31 -0.142  
## 18 2017-06-30 -0.00367
## 19 2017-09-29  0.0345 
## 20 2017-12-29  0.108

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.372

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

# Transform data: calculate rolling sharpe ratio
rolling_sr_tbl <- portfolio_returns_tbl %>%
    
    tq_mutate(returns, 
              mutate_fun = rollapply, 
              width = window,
              FUN = Calculate_rolling_SharpeRatio,
              col_rename = "rolling_sr") %>%
    
    select(-returns) %>% 
    na.omit()

rolling_sr_tbl
## # A tibble: 9 × 2
##   date       rolling_sr
##   <date>          <dbl>
## 1 2015-12-31   0.634   
## 2 2016-03-31   0.509   
## 3 2016-06-30   0.353   
## 4 2016-09-30   0.328   
## 5 2016-12-30   0.357   
## 6 2017-03-31   0.149   
## 7 2017-06-30   0.0527  
## 8 2017-09-29   0.0311  
## 9 2017-12-29   0.000217
rolling_sr_tbl %>% ggplot(aes(x = date, y = rolling_sr)) +
    geom_line(color = "cornflowerblue") +
    
    # Labeling
    labs(x = NULL, y = "Sharpe Ratio", title = "Rolling 12 Month Sharpe Ratio") +
    
    annotate(geom = "text",
             x = as.Date("2017-01-01"),
             y = 0.5, 
             label = "This portfolio has done poorly from 2016-2018.",
             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?

This portfolio did not perform well overtime. This decrease in sharpe ratio means that the risk is increasing but the return is not increasing with it. It could also mean returns are decreasing while the risks remain the same. There are no structural breaks over this period.