# Load packages
library(tidyverse)
library(tidyquant)
# Choose stocks
symbols <- c("TSLA", "DELL", "AAPL", "SOXL", "SMCI")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-01-01",
to = "2016-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: 64 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 TSLA 2012-03-30 0.282
## 2 TSLA 2012-06-29 -0.174
## 3 TSLA 2012-09-28 -0.0664
## 4 TSLA 2012-12-31 0.146
## 5 TSLA 2013-03-28 0.112
## 6 TSLA 2013-06-28 1.04
## 7 TSLA 2013-09-30 0.588
## 8 TSLA 2013-12-31 -0.251
## 9 TSLA 2014-03-31 0.326
## 10 TSLA 2014-06-30 0.141
## # ℹ 54 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 = 1, binwidth = 0.1) +
facet_wrap(~asset, ncol = 1) +
# Labeling
labs(title = "Distribution of Monthly Returns, 2012-2016",
y = "Frequency",
x = "Rate of Returns")
AAPL has the highest typical quarterly return
Hide the code, messages, and warnings