# Load packages
library(tidyverse)
library(tidyquant)
COST - Costco Wholesale Corporation
ELF - E.l.f. Beauty Inc.
TSLA - Tesla Inc.
NFLX - Netflix Inc.
GOOG - Alphabet Inc Class C
symbols <- c("COST", "ELF", "TSLA", "NFLX", "GOOG")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2019-01-01",
to = "2024-01-01")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "quarterly",
type = "log") %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
asset_returns_tbl
## # A tibble: 100 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 COST 2019-03-29 0.170
## 2 COST 2019-06-28 0.0901
## 3 COST 2019-09-30 0.0886
## 4 COST 2019-12-31 0.0221
## 5 COST 2020-03-31 -0.0283
## 6 COST 2020-06-30 0.0638
## 7 COST 2020-09-30 0.160
## 8 COST 2020-12-31 0.0873
## 9 COST 2021-03-31 -0.0647
## 10 COST 2021-06-30 0.118
## # ℹ 90 more rows
asset_returns_tbl %>%
ggplot(aes(x = returns)) +
geom_density(aes(color = asset), show.legend = FALSE, alpha = 1) +
geom_histogram(aes(fill = asset), show.legend = FALSE, alpha = 0.3, binwidth = 0.05) +
facet_wrap(~asset, ncol = 1) +
# labeling
labs(title = "Destribution of Quarterly Returns, 2019-2024",
y = "Frequency",
x = "Rate of Returns")
Hide the code, messages, and warnings