# 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     = "monthly",
                 type       = "log") %>%
    slice(-1) %>%
    ungroup() %>%
    rename(asset = symbol)   

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

tickers <- c("SPY", "EFA", "IJS", "EEM", "AGG")  
w <- c(0.25, 0.25, 0.20, 0.20, 0.10)

w_tbl <- tibble(
  asset   = tickers,
  weights = w
)

w_tbl
## # A tibble: 5 × 2
##   asset weights
##   <chr>   <dbl>
## 1 SPY      0.25
## 2 EFA      0.25
## 3 IJS      0.2 
## 4 EEM      0.2 
## 5 AGG      0.1

4 Build a portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
  tq_portfolio(
    assets_col   = asset,
    returns_col  = monthly.returns,
    weights      = w_tbl,
    col_rename   = "monthly_return",
    rebalance_on = "months"
  )

5 Plot: Portfolio Histogram and Density

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

Based on the portfolio of SPY, EFA, IJS, EEM, and AGG (with weights 25%, 25%, 20%, 20%, and 10%), the expected quarterly return is approximately X%. This value represents the mean of the quarterly return distribution.