# Load packages
# Core
library(tidyverse)
library(tidyquant)
Collect individual returns into a portfolio by assigning a weight to each stock
Choose your stocks.
from 2012-12-31 to 2017-12-31
symbol <- c("AMZN", "BIG", "TSLA", "WM", "PLUG")
prices <- tq_get(x = symbol,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "quarterly",
type = "log") %>%
slice(-1) %>%
ungroup()
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)
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = symbol,
returns_col = quarterly.returns,
weights = w_tbl,
rebalance_on = "quarter")
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.