# 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("WMT", "AAPL", "NFLX", "GOOGL")

prices <- tq_get(x   = symbols,
                get  = "stock,prices",
                from = "2012-12-31",
                to   = "2017-12-31")
prices
## # A tibble: 5,040 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 WMT    2012-12-31  22.5  22.8  22.5  22.7 21037500     17.4
##  2 WMT    2013-01-02  23.0  23.1  22.8  23.1 31172400     17.7
##  3 WMT    2013-01-03  23.1  23.1  22.8  22.9 26730300     17.6
##  4 WMT    2013-01-04  22.9  23.1  22.8  23.0 19314000     17.7
##  5 WMT    2013-01-07  22.9  23.0  22.7  22.8 18604200     17.5
##  6 WMT    2013-01-08  22.8  23.0  22.7  22.9 17600700     17.5
##  7 WMT    2013-01-09  22.9  22.9  22.7  22.9 15165600     17.5
##  8 WMT    2013-01-10  22.9  23.0  22.6  22.8 34361400     17.5
##  9 WMT    2013-01-11  22.9  22.9  22.7  22.9 18673500     17.6
## 10 WMT    2013-01-14  22.8  22.9  22.7  22.8 16471200     17.5
## # ℹ 5,030 more rows

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: 80 × 3
##    asset date       returns
##    <chr> <date>       <dbl>
##  1 AAPL  2013-03-28 -0.178 
##  2 AAPL  2013-06-28 -0.103 
##  3 AAPL  2013-09-30  0.191 
##  4 AAPL  2013-12-31  0.169 
##  5 AAPL  2014-03-31 -0.0383
##  6 AAPL  2014-06-30  0.198 
##  7 AAPL  2014-09-30  0.0858
##  8 AAPL  2014-12-31  0.0956
##  9 AAPL  2015-03-31  0.124 
## 10 AAPL  2015-06-30  0.0122
## # ℹ 70 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL"  "GOOGL" "NFLX"  "WMT"
# weights
weights <- c(0.35, 0.30, 0.20, 0.15)
weights
## [1] 0.35 0.30 0.20 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AAPL       0.35
## 2 GOOGL      0.3 
## 3 NFLX       0.2 
## 4 WMT        0.15

4 Build a portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col   = asset,
                 returns_col  = returns,
                 weights      = w_tbl,
                 rebalance_on = "quarters")

portfolio_returns_tbl
## # A tibble: 20 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-03-28           0.130  
##  2 2013-06-28           0.0167 
##  3 2013-09-30           0.141  
##  4 2013-12-31           0.178  
##  5 2014-03-31          -0.0274 
##  6 2014-06-30           0.126  
##  7 2014-09-30           0.0404 
##  8 2014-12-31          -0.0349 
##  9 2015-03-31           0.0908 
## 10 2015-06-30           0.0660 
## 11 2015-09-30           0.0134 
## 12 2015-12-31           0.0578 
## 13 2016-03-31           0.00348
## 14 2016-06-30          -0.0796 
## 15 2016-09-30           0.115  
## 16 2016-12-30           0.0462 
## 17 2017-03-31           0.140  
## 18 2017-06-30           0.0404 
## 19 2017-09-29           0.0835 
## 20 2017-12-29           0.105

5 Plot: Portfolio Histogram and Density

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

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

In a typical quarter you should expect anywhere from about 4.5% to about 14.5%. The returns aren’t necessarily consistent with the chosen stocks in the portfolio, but based on the density line, it is at about its peak between these return percentages. There’s no real outlier for an “average” return but in the chosen dates, these returns in this range are most common