# 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("FDX", "UPS", "MSFT")
prices <- tq_get(x = symbols,
                 get = "stock.prices", 
                 from = "2012-12-31",
                 to = "2017-12-31")
prices
## # A tibble: 3,780 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 FDX    2012-12-31  90.3  91.8  90.1  91.7 1451700     82.0
##  2 FDX    2013-01-02  93.5  95.2  93.4  94.2 2020100     84.3
##  3 FDX    2013-01-03  94.1  95.2  94.1  94.6 1723800     84.6
##  4 FDX    2013-01-04  94.8  95.1  94.4  94.9 1500600     84.9
##  5 FDX    2013-01-07  94.4  94.8  94.2  94.7 1148600     84.7
##  6 FDX    2013-01-08  94.4  95.5  94.2  95.1 2177600     85.0
##  7 FDX    2013-01-09  95.0  96.9  94.8  96.8 2345100     86.5
##  8 FDX    2013-01-10  97.3  97.7  96.7  97.7 2009700     87.3
##  9 FDX    2013-01-11  97.6  97.8  97.1  97.4 1272400     87.1
## 10 FDX    2013-01-14  97.4  98.7  97.2  98.4 2253600     88.0
## # … with 3,770 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      date   returns 
##   "asset"    "date" "returns"
asset_returns_tbl
## # A tibble: 60 × 3
##    symbol date       quarterly.returns
##    <chr>  <date>                 <dbl>
##  1 FDX    2013-03-28           0.0696 
##  2 FDX    2013-06-28           0.00540
##  3 FDX    2013-09-30           0.148  
##  4 FDX    2013-12-31           0.232  
##  5 FDX    2014-03-31          -0.0801 
##  6 FDX    2014-06-30           0.134  
##  7 FDX    2014-09-30           0.0657 
##  8 FDX    2014-12-31           0.0740 
##  9 FDX    2015-03-31          -0.0473 
## 10 FDX    2015-06-30           0.0308 
## # … with 50 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(symbol) %>% pull()
    

# weights
weights <- c(0.50, 0.32, 0.18)
weights
## [1] 0.50 0.32 0.18
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 FDX        0.5 
## 2 MSFT       0.32
## 3 UPS        0.18

4 Build a portfolio

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

portfolio_returns_tbl
## # A tibble: 20 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-03-28            0.0882
##  2 2013-06-28            0.0677
##  3 2013-09-30            0.0754
##  4 2013-12-31            0.182 
##  5 2014-03-31           -0.0208
##  6 2014-06-30            0.0855
##  7 2014-09-30            0.0622
##  8 2014-12-31            0.0629
##  9 2015-03-31           -0.0873
## 10 2015-06-30            0.0451
## 11 2015-09-30           -0.0759
## 12 2015-12-31            0.0891
## 13 2016-03-31            0.0637
## 14 2016-06-30           -0.0506
## 15 2016-09-30            0.115 
## 16 2016-12-30            0.0691
## 17 2017-03-31            0.0345
## 18 2017-06-30            0.0783
## 19 2017-09-29            0.0625
## 20 2017-12-29            0.0973

5 Plot: Portfolio Histogram and Density

portfolio_returns_tbl %>% 
    ggplot(mapping = aes(x = portfolio.returns)) +
    geom_histogram(fill = "cornflowerblue", binwidth = 0.01) + geom_density()+
    
    #formating
    scale_x_continuous(labels = scales::percent_format())

    labs(x = "returns",
         y = "distribution",
         title = "Portfolio Histogram & Density")
## $x
## [1] "returns"
## 
## $y
## [1] "distribution"
## 
## $title
## [1] "Portfolio Histogram & Density"
## 
## attr(,"class")
## [1] "labels"

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

A typical return for this portfolio is around 6 or 7% per quarter. This is because it is the highest bar on the histogram and density chart. There were a few outliars in the portfolio that resulted in the return being negative or over 10% return.