# 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
# Choose stocks
symbols <- c("CRWD", "AMZN", "SHOP","TTD", "NVDA")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2021-01-01")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMZN" "CRWD" "NVDA" "SHOP" "TTD"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
## symbols weights
## <chr> <dbl>
## 1 AMZN 0.25
## 2 CRWD 0.25
## 3 NVDA 0.2
## 4 SHOP 0.2
## 5 TTD 0.1
# ?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months" )
portfolio_returns_tbl
## # A tibble: 45 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2021-02-26 0.0378
## 2 2021-03-31 -0.0978
## 3 2021-04-30 0.110
## 4 2021-05-28 0.00183
## 5 2021-06-30 0.149
## 6 2021-07-30 -0.000161
## 7 2021-08-31 0.0648
## 8 2021-09-30 -0.0992
## 9 2021-10-29 0.105
## 10 2021-11-30 0.0333
## # ℹ 35 more rows
portfolio_returns_tbl %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(fill = "cornflowerblue", binwidth = .01) +
geom_density() +
# Formatting
scale_x_continuous(labels = scales::percent_format()) +
labs(x = "returns",
y = "distrobution",
title = "Portfolio Histogram & Density")
What return should you expect from the portfolio in a typical quarter?
You should expect around a 3% return from these stocks, with a high variance or risk involved. There have been 3 negative 10 percent losses, so that is a posibility with this portfolio. An average return for this would be anywhere from -2 percent to around 10 percent. In the past this portfolio has seen a loss of 30 percent, and a few losses of 10-15 percent in a quarter. But it has also seen gains of over 20 percent.