# Load packages
library(tidyverse)
library(tidyquant)
symbols <- c("AAPL", "MSFT", "INTC", "AMD", "GRMN")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2022-01-01",
to = "2023-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: 20 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AAPL 2022-03-31 -0.0402
## 2 AAPL 2022-06-30 -0.243
## 3 AAPL 2022-09-30 0.0122
## 4 AAPL 2022-12-30 -0.0600
## 5 MSFT 2022-03-31 -0.0802
## 6 MSFT 2022-06-30 -0.180
## 7 MSFT 2022-09-30 -0.0957
## 8 MSFT 2022-12-30 0.0321
## 9 INTC 2022-03-31 -0.0635
## 10 INTC 2022-06-30 -0.273
## 11 INTC 2022-09-30 -0.363
## 12 INTC 2022-12-30 0.0387
## 13 AMD 2022-03-31 -0.318
## 14 AMD 2022-06-30 -0.358
## 15 AMD 2022-09-30 -0.188
## 16 AMD 2022-12-30 0.0220
## 17 GRMN 2022-03-31 -0.113
## 18 GRMN 2022-06-30 -0.181
## 19 GRMN 2022-09-30 -0.193
## 20 GRMN 2022-12-30 0.147
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.1) +
facet_wrap(~asset, ncol = 1)
#Apple has the best overall quarterly returns in 2022.
Hide the code, messages, and warnings