# 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?

Most of the returns are between -0.3% and 0.3%. 6 returns were high between 27% and 37%. The remaining returns had high distribution however did not reach the same high percentages. Meaning that most of the returns were not high yeild but they did happen more than once.