# 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("AMZN", "MSFT", "HD", "WMT")

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 AMZN   2012-12-31  12.2  12.6  12.1  12.5 68380000     12.5
##  2 AMZN   2013-01-02  12.8  12.9  12.7  12.9 65420000     12.9
##  3 AMZN   2013-01-03  12.9  13.0  12.8  12.9 55018000     12.9
##  4 AMZN   2013-01-04  12.9  13.0  12.8  13.0 37484000     13.0
##  5 AMZN   2013-01-07  13.1  13.5  13.1  13.4 98200000     13.4
##  6 AMZN   2013-01-08  13.4  13.4  13.2  13.3 60214000     13.3
##  7 AMZN   2013-01-09  13.4  13.5  13.3  13.3 45312000     13.3
##  8 AMZN   2013-01-10  13.4  13.4  13.1  13.3 57268000     13.3
##  9 AMZN   2013-01-11  13.3  13.4  13.2  13.4 48266000     13.4
## 10 AMZN   2013-01-14  13.4  13.7  13.4  13.6 85500000     13.6
## # … with 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 AMZN  2013-03-28  0.0604 
##  2 AMZN  2013-06-28  0.0412 
##  3 AMZN  2013-09-30  0.119  
##  4 AMZN  2013-12-31  0.243  
##  5 AMZN  2014-03-31 -0.170  
##  6 AMZN  2014-06-30 -0.0351 
##  7 AMZN  2014-09-30 -0.00723
##  8 AMZN  2014-12-31 -0.0382 
##  9 AMZN  2015-03-31  0.181  
## 10 AMZN  2015-06-30  0.154  
## # … with 70 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull() 
symbols
## [1] "AMZN" "HD"   "MSFT" "WMT"
# weights
weights <- c(0.30, 0.30, 0.15, 0.25)
weights
## [1] 0.30 0.30 0.15 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMZN       0.3 
## 2 HD         0.3 
## 3 MSFT       0.15
## 4 WMT        0.25

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.0922 
##  2 2013-06-28           0.0749 
##  3 2013-09-30           0.0260 
##  4 2013-12-31           0.135  
##  5 2014-03-31          -0.0521 
##  6 2014-06-30          -0.00123
##  7 2014-09-30           0.0599 
##  8 2014-12-31           0.0620 
##  9 2015-03-31           0.0515 
## 10 2015-06-30           0.0191 
## 11 2015-09-30           0.0431 
## 12 2015-12-31           0.148  
## 13 2016-03-31          -0.00465
## 14 2016-06-30           0.0518 
## 15 2016-09-30           0.0683 
## 16 2016-12-30          -0.0156 
## 17 2017-03-31           0.101  
## 18 2017-06-30           0.0628 
## 19 2017-09-29           0.0409 
## 20 2017-12-29           0.186

5 Plot: Portfolio Histogram and Density

portfolio_returns_tbl %>%
    
    ggplot(mapping = aes(x = portfolio.returns)) +
    geom_histogram(fill = "violet", binwidth = 0.01) + 
    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?

You should expect a return between 1-7.5% from this portfolio in a typical quarter, but you may end up with returns higher or lower than that. It is most common to get a 5% return from this portfolio.