# Load packages
# Core
library(tidyverse)
library(tidyquant)
Collect individual returns into a portfolio by assigning a weight to each stock
Choose your stocks.
from 2017-12-31 to 2022-12-31
# Choose stocks
symbols <- c("MSFT", "JPM", "GM", "TMUS", "IRVRF")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2017-12-31",
to = "2022-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
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "GM" "IRVRF" "JPM" "MSFT" "TMUS"
# weights
weights <- c(0.25, 0.2, 0.2, 0.1, 0.25)
weights
## [1] 0.25 0.20 0.20 0.10 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
## symbols weights
## <chr> <dbl>
## 1 GM 0.25
## 2 IRVRF 0.2
## 3 JPM 0.2
## 4 MSFT 0.1
## 5 TMUS 0.25
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: 19 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2018-06-29 0.0130
## 2 2018-09-28 0.00548
## 3 2018-12-31 0.0486
## 4 2019-03-29 0.0771
## 5 2019-06-28 0.168
## 6 2019-09-30 -0.0208
## 7 2019-12-31 0.100
## 8 2020-03-31 -0.297
## 9 2020-06-30 0.242
## 10 2020-09-30 0.0518
## 11 2020-12-31 0.162
## 12 2021-03-31 0.0628
## 13 2021-06-30 0.0257
## 14 2021-09-30 -0.135
## 15 2021-12-31 0.0495
## 16 2022-03-31 -0.102
## 17 2022-06-30 -0.164
## 18 2022-09-30 -0.0804
## 19 2022-12-30 0.123
portfolio_returns_tbl %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(fill = "olivedrab", binwidth = 0.01) +
geom_density()+
# Formatting
scale_x_continuous(labels = scales::percent_format()) +
# Labeling
labs(x = "Quarterly Returns",
y = "Distribution",
tittle = "Portfolio Histogram & Density")
When analyzing the 19 observations for the portfolios quarterly returns, there would be an expectation that a typical quarterly return would fall between 1% and 5%. That being said, the data set range includes observations as low as -30%, and up to 25%, with very few repeated entries. The data also included portfolio returns from quarters during the pandemic, as well as the latter half of 2022 when tech stocks experienced tumultuous performance. The large-cap stocks that were selected for the portfolio will likely have a better performance in 2023, based on fundamental valuations and technical analysis on the individual positions. The historical data shows that the portfolio might under perform indexes such as the S&P500, but a typical quarter should be anticipated as surpassing the 1%-5% estimate.