# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

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

Choose your stocks.

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

1 Import stock prices

symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2012-12-31",
                 to = "2017-12-31")

2 Convert prices to returns (quarterly)

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"))

asset_returns_tbl
## # A tibble: 100 × 3
##    asset date         returns
##    <chr> <date>         <dbl>
##  1 AGG   2013-03-28  0.000645
##  2 AGG   2013-06-28 -0.0264  
##  3 AGG   2013-09-30  0.00553 
##  4 AGG   2013-12-31  0.000199
##  5 AGG   2014-03-31  0.0176  
##  6 AGG   2014-06-30  0.0193  
##  7 AGG   2014-09-30  0.00275 
##  8 AGG   2014-12-31  0.0186  
##  9 AGG   2015-03-31  0.0151  
## 10 AGG   2015-06-30 -0.0184  
## # … with 90 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AGG" "EEM" "EFA" "IJS" "SPY"
# weights
weights <- c(0.2, 0.25, 0.5, 0.2, 0.7)
weights
## [1] 0.20 0.25 0.50 0.20 0.70
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AGG        0.2 
## 2 EEM        0.25
## 3 EFA        0.5 
## 4 IJS        0.2 
## 5 SPY        0.7

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")

portfolio_returns_tbl
## # A tibble: 20 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-03-28           0.101  
##  2 2013-06-28          -0.00411
##  3 2013-09-30           0.123  
##  4 2013-12-31           0.126  
##  5 2014-03-31           0.0159 
##  6 2014-06-30           0.0791 
##  7 2014-09-30          -0.0474 
##  8 2014-12-31           0.0251 
##  9 2015-03-31           0.0436 
## 10 2015-06-30          -0.00191
## 11 2015-09-30          -0.165  
## 12 2015-12-31           0.0694 
## 13 2016-03-31           0.0273 
## 14 2016-06-30           0.0292 
## 15 2016-09-30           0.0909 
## 16 2016-12-30           0.0237 
## 17 2017-03-31           0.108  
## 18 2017-06-30           0.0710 
## 19 2017-09-29           0.0885 
## 20 2017-12-29           0.0893

5 Plot: Portfolio Histogram and Density

portfolio_returns_tbl %>%
    
    ggplot(mapping = aes(x = portfolio.returns)) +
    geom_histogram(fill = "cornflowerblue", binwidth = 0.01) +
    geom_density() +
    
    # Formatting 
    scale_x_continuous(labels = scales :: percent_format()) +
    
    labs(x = "returns",
         y = "dsitribution",
         title = "Portfolio Histogram & Density")

What return should you expect from the portfolio in a typical quarter?

It would be safe to assume about a 1% return per quater