# 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

symbol <- c("AMZN", "BIG", "TSLA", "WM", "PLUG")

prices <- tq_get(x = symbol,
                 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()

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

symbols <- asset_returns_tbl %>% distinct(symbol) %>% pull()
symbols
## [1] "AMZN" "BIG"  "PLUG" "TSLA" "WM"
weight <- c(0.2,0.2,0.2,0.2,0.2)
weight
## [1] 0.2 0.2 0.2 0.2 0.2
w_tbl <- tibble(symbols, weight)

4 Build a portfolio

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

5 Plot: Portfolio Histogram and Density

portfolio_returns_tbl %>%
ggplot(mapping = aes(x = portfolio.returns)) +
    geom_histogram(fill = "violetred1", binwidth = .05) +
    geom_density() +
    scale_x_continuous(labels = scales::percent_format()) +
    labs(x = "Returns", y = "distribution", title = "Histogram & Destiny")

What return should you expect from the portfolio in a typical quarter? Following the destiny line you can assume the most typical return will fall between 0% and 5%, while the overall average return is roughly 7%