# 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("NKE", "ADDYY", "SKX", "UAA")

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

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "ADDYY" "NKE"   "SKX"   "UAA"
# weights
weights <- c(0.4, 0.2, 0.25, 0.15)
weights
## [1] 0.40 0.20 0.25 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 ADDYY      0.4 
## 2 NKE        0.2 
## 3 SKX        0.25
## 4 UAA        0.15

4 Build a portfolio

5 Plot: Portfolio Histogram and Density

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