# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(plotly)
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("FNV", "WPM", "NEM", "CCJ", "BWXT", "NVDA", "TSM", "ASML", "CAT", "ITW", "HON", "PLD", "EQIX", "PSA", "TPL", "XOM", "CVX")
prices <- tq_get(x = symbols,
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() %>%
set_names(c("asset", "date", "returns"))
asset_returns_tbl
## # A tibble: 340 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 ASML 2013-03-28 0.0544
## 2 ASML 2013-06-28 0.161
## 3 ASML 2013-09-30 0.222
## 4 ASML 2013-12-31 -0.0526
## 5 ASML 2014-03-31 -0.00364
## 6 ASML 2014-06-30 0.00901
## 7 ASML 2014-09-30 0.0578
## 8 ASML 2014-12-31 0.0873
## 9 ASML 2015-03-31 -0.0651
## 10 ASML 2015-06-30 0.0377
## # ℹ 330 more rows
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "ASML" "BWXT" "CAT" "CCJ" "CVX" "EQIX" "FNV" "HON" "ITW" "NEM"
## [11] "NVDA" "PLD" "PSA" "TPL" "TSM" "WPM" "XOM"
weights <- c(0.10, 0.10, 0.10, 0.05, 0.05, 0.04, 0.03, 0.03, 0.04, 0.03, 0.03, 0.07, 0.07, 0.06, 0.07, 0.07, 0.06)
weights
## [1] 0.10 0.10 0.10 0.05 0.05 0.04 0.03 0.03 0.04 0.03 0.03 0.07 0.07 0.06 0.07
## [16] 0.07 0.06
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 17 × 2
## symbols weights
## <chr> <dbl>
## 1 ASML 0.1
## 2 BWXT 0.1
## 3 CAT 0.1
## 4 CCJ 0.05
## 5 CVX 0.05
## 6 EQIX 0.04
## 7 FNV 0.03
## 8 HON 0.03
## 9 ITW 0.04
## 10 NEM 0.03
## 11 NVDA 0.03
## 12 PLD 0.07
## 13 PSA 0.07
## 14 TPL 0.06
## 15 TSM 0.07
## 16 WPM 0.07
## 17 XOM 0.06
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarters")
portfolio_returns_tbl
## # A tibble: 20 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.0394
## 2 2013-06-28 -0.0149
## 3 2013-09-30 0.0599
## 4 2013-12-31 0.0163
## 5 2014-03-31 0.0665
## 6 2014-06-30 0.0580
## 7 2014-09-30 -0.0572
## 8 2014-12-31 0.00996
## 9 2015-03-31 -0.00127
## 10 2015-06-30 -0.00687
## 11 2015-09-30 -0.0855
## 12 2015-12-31 0.0875
## 13 2016-03-31 0.123
## 14 2016-06-30 0.0863
## 15 2016-09-30 0.0695
## 16 2016-12-30 0.0227
## 17 2017-03-31 0.0528
## 18 2017-06-30 0.0391
## 19 2017-09-29 0.117
## 20 2017-12-29 0.0697
portfolio_returns_tbl %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(color = "white", fill = "darkgoldenrod", binwidth = 0.04) +
geom_density() +
scale_x_continuous(labels = scales::percent_format()) +
labs(x = "Returns",
y = "Distribution",
title = "Portfolio Histogram & Density")
When looking at the chart, there a a couple ways to look at it; mode, median, and mean. Simply looking at the most common outcome (the mode) we could expect a return of roughly 4-6%. When looking a bit further into the concentration of returns (the median) we see the middle quarterly return at about 4%. For the most accurate reading, we take the mean of all 20 quarters to find an expected return of about 3.8%.