# 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

stocks <- c("MSFT", "JPM", "WMT", "DIS", "NKE") |>
  tq_get(get = "stock.prices",
         from = "2012-12-31",
         to = "2017-12-31")

stocks
## # A tibble: 6,300 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 MSFT   2012-12-31  26.6  26.8  26.4  26.7 42749500     21.4
##  2 MSFT   2013-01-02  27.2  27.7  27.1  27.6 52899300     22.1
##  3 MSFT   2013-01-03  27.6  27.6  27.2  27.2 48294400     21.8
##  4 MSFT   2013-01-04  27.3  27.3  26.7  26.7 52521100     21.4
##  5 MSFT   2013-01-07  26.8  26.9  26.6  26.7 37110400     21.4
##  6 MSFT   2013-01-08  26.8  26.8  26.5  26.5 44703100     21.2
##  7 MSFT   2013-01-09  26.7  26.8  26.6  26.7 49047900     21.4
##  8 MSFT   2013-01-10  26.6  27.0  26.3  26.5 71431300     21.2
##  9 MSFT   2013-01-11  26.5  26.9  26.3  26.8 55512100     21.5
## 10 MSFT   2013-01-14  26.9  27.1  26.8  26.9 48324400     21.5
## # ℹ 6,290 more rows

2 Convert prices to returns (quarterly)

returns <- stocks |>
  group_by(symbol) |>
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = "quarterly",
               col_rename = "Ra")

returns
## # A tibble: 105 × 3
## # Groups:   symbol [5]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 MSFT   2012-12-31  0      
##  2 MSFT   2013-03-28  0.0800 
##  3 MSFT   2013-06-28  0.216  
##  4 MSFT   2013-09-30 -0.0297 
##  5 MSFT   2013-12-31  0.133  
##  6 MSFT   2014-03-31  0.104  
##  7 MSFT   2014-06-30  0.0245 
##  8 MSFT   2014-09-30  0.119  
##  9 MSFT   2014-12-31  0.00826
## 10 MSFT   2015-03-31 -0.118  
## # ℹ 95 more rows

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

weights <- c(0.30, 0.25, 0.20, 0.15, 0.10)

weights
## [1] 0.30 0.25 0.20 0.15 0.10

4 Build a portfolio

portfolio <- returns |>
  tq_portfolio(assets_col = symbol,
               returns_col = Ra,
               weights = weights,
               col_rename = "Rp")

portfolio
## # A tibble: 21 × 2
##    date             Rp
##    <date>        <dbl>
##  1 2012-12-31  0      
##  2 2013-03-28  0.102  
##  3 2013-06-28  0.119  
##  4 2013-09-30  0.00478
##  5 2013-12-31  0.128  
##  6 2014-03-31  0.0410 
##  7 2014-06-30  0.0124 
##  8 2014-09-30  0.0784 
##  9 2014-12-31  0.0535 
## 10 2015-03-31 -0.0255 
## # ℹ 11 more rows

5 Plot: Portfolio Histogram and Density

portfolio |>
  ggplot(aes(x = Rp)) +
  geom_histogram(aes(y = after_stat(density)),
                 binwidth = 0.01,
                 fill = "steelblue",
                 color = "white") +
  geom_density(color = "darkblue",
               linewidth = 1) +
  labs(title = "Portfolio Quarterly Returns",
       x = "Quarterly Return",
       y = "Density")

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

portfolio |>
  summarise(typical_return = mean(Rp))
## # A tibble: 1 × 1
##   typical_return
##            <dbl>
## 1         0.0503

Based on the historical data, the portfolio had an average quarterly return of approximately 5.03%. Therefore, I would expect the portfolio to return around 5% in a typical quarter.