# 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
symbols <- c("COST", "LMT", "ADBE", "LLY", "ADP")
prices <- tq_get(x = symbols,
get = "stock.prices",
fro = "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() %>%
set_names(c("asset", "date", "returns"))
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "ADBE" "ADP" "COST" "LLY" "LMT"
weights <- c(0.4, 0.25, 0.2, 0.1, 0.05)
weights
## [1] 0.40 0.25 0.20 0.10 0.05
w_tbl <- tibble(symbols, weights)
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarters")
portfolio_returns_tbl %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(fill = "green", binwidth = 0.01) +
geom_density() +
# Formatting
scale_x_continuous(labels = scales::percent_format()) +
labs(x = "Quarterly Returns",
y = "Frequency",
title = "Portfolio Histogram & Density")
What return should you expect from the portfolio in a typical quarter?
In this portfolio, investors should expect a quarterly return of just under 4%. This is shown in the histogram and density plot above, as we see on four occasions it has returned a percentage just below 5%. The average return, found by the summarise function is 5.74%. If each quarter were to return a percentage of that nature, they would significantly beat historical annual returns of the S$P 500. On a few occasions a quarterly return has exceeded 10%, but there is not enough evidence to show that this result has a high chance of happening.