# 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("WMT", "TGT", "COST")

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 <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "COST" "TGT"  "WMT"
weights <- c(0.35, 0.3, 0.25)
weights
## [1] 0.35 0.30 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 COST       0.35
## 2 TGT        0.3 
## 3 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  = "quarter" )

portfolio_returns_tbl
## # A tibble: 20 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-03-28          0.0963  
##  2 2013-06-28          0.0191  
##  3 2013-09-30         -0.00526 
##  4 2013-12-31          0.0279  
##  5 2014-03-31         -0.0381  
##  6 2014-06-30         -0.00190 
##  7 2014-09-30          0.0631  
##  8 2014-12-31          0.134   
##  9 2015-03-31          0.0517  
## 10 2015-06-30         -0.0743  
## 11 2015-09-30         -0.00496 
## 12 2015-12-31          0.00605 
## 13 2016-03-31          0.0619  
## 14 2016-06-30         -0.0293  
## 15 2016-09-30         -0.0133  
## 16 2016-12-30          0.0269  
## 17 2017-03-31         -0.0485  
## 18 2017-06-30         -0.000799
## 19 2017-09-29          0.0597  
## 20 2017-12-29          0.138

5 Plot: Portfolio Histogram and Density

portfolio_returns_tbl %>%
    ggplot(mapping = aes(x = portfolio.returns)) +
    geom_histogram(fill = "cornflowerblue", binwidth = 0.01)+
    geom_density() +
    scale_x_continuous(labels = scales::percent_format()) +
    labs(x = "returns",
         y = "distributions",
         title = "Portfolio Histogram & Density")

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

From the portfolio I would expect around a 5% return in a typical quarter. Looking at the graph, majority of the returns is within the 0-5 percent range but I also wanted to factor in both the highs and the lows. So looking at the high of over 14% and a low of around -7%, even if it is heavy on the 0% side, I feel as though the 14% will bump the return rate up to that 5%. With the -7%, I know it will make an impact but I feel as though it will be minor considering how much of the data is between 0-5% and the high being double the low. Therefore, I feel it will be around a 5% return from this portfolio.