# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Collect individual returns into a portfolio by assigning a weight to each stock

five stocks: “SPY”, “EFA”, “IJS”, “EEM”, “AGG”

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

1 Import stock prices

symbols <- c("LULU", "NFLX", "TSLA")

prices <- tq_get(x = symbols, 
                 get = "stock.prices",
                 from = "2012-12-31", 
                 to = "2017-12-31")

2 Convert prices to returns

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

# symbols
symbols <- asset_returns_tb1 %>% distinct(asset) %>% pull()
symbols
## [1] "LULU" "NFLX" "TSLA"
#weights
weights <- c(0.30, 0.35, 0.35)
weights
## [1] 0.30 0.35 0.35
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 LULU       0.3 
## 2 NFLX       0.35
## 3 TSLA       0.35

4 Build a portfolio

portfolio_returns_tb1 <- asset_returns_tb1 %>% 
    
    tq_portfolio(assets_col = asset,
                 returns_col = returns,
                 weights = w_tbl, 
                 rebalance_on = "months")

portfolio_returns_tb1
## # A tibble: 20 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-03-28            0.229 
##  2 2013-06-28            0.417 
##  3 2013-09-30            0.373 
##  4 2013-12-31           -0.0911
##  5 2014-03-31            0.0638
##  6 2014-06-30            0.0494
##  7 2014-09-30            0.0232
##  8 2014-12-31           -0.0428
##  9 2015-03-31            0.0534
## 10 2015-06-30            0.288 
## 11 2015-09-30           -0.0697
## 12 2015-12-31            0.0344
## 13 2016-03-31            0.0219
## 14 2016-06-30           -0.0405
## 15 2016-09-30           -0.0453
## 16 2016-12-30            0.115 
## 17 2017-03-31            0.0869
## 18 2017-06-30            0.137 
## 19 2017-09-29            0.0601
## 20 2017-12-29            0.0579

5 Calculate Standard Deviation

portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_tb1 %>%
    
    tq_performance(Ra = portfolio.returns, 
                   performance_fun = table.Stats) %>%
    
    select(Stdev) %>%
    mutate(tq_sd = round(Stdev, 3))

portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
##   Stdev tq_sd
##   <dbl> <dbl>
## 1 0.141 0.141
# mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tb1$portfolio.returns)

portfolio_mean_tidyquant_builtin_percent
## [1] 0.08609191

6 Plot

Expected Returns vs Risk

#Expected Returns vs Risk
sd_mean_tbl <- asset_returns_tb1 %>%
    
    group_by(asset)%>%
    tq_performance(Ra = returns,
                   performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
    ungroup() %>%
    mutate(Stdev = Stdev * 100, 
           Mean  = Mean * 100) %>%

    #add portfolio sd
add_row(tibble(asset = "Portfolio", 
               Mean = portfolio_mean_tidyquant_builtin_percent * 100 ,
               Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))

sd_mean_tbl
## # A tibble: 4 × 3
##   asset      Mean  Stdev
##   <chr>     <dbl>  <dbl>
## 1 LULU       0.15 17.6  
## 2 NFLX      13.4  21.7  
## 3 TSLA      11.1  30.0  
## 4 Portfolio  8.61  0.141
sd_mean_tbl %>%
    
    ggplot(aes(x = Stdev, y = Mean, color = asset)) +
    geom_point() +
    ggrepel::geom_label_repel(aes(label = asset))

24 Months rolling Volatillity

rolling_sd_tbl <- portfolio_returns_tb1 %>%
    
    tq_mutate(select = portfolio.returns, 
              mutate_fun = rollapply,
              width = 20, 
              FUN = sd, 
              col_rename = "rolling_sd") %>%
    
    na.omit() %>%
    select(date, rolling_sd)
rolling_sd_tbl %>%
    
    ggplot(aes(x= date, y = rolling_sd)) +
geom_line(color = "cornflowerblue")

# Formatting
scale_y_continuous(labels = scales::percent_format())
## <ScaleContinuousPosition>
##  Range:  
##  Limits:    0 --    1
#labeling
labs(x = NULL,
     y = NULL, 
     title = "24-Month Rolling Volatillity") +
    theme(plot.title = element_text(hjust = 0.5))
## NULL