# Load packages
library(tidyverse)
library(tidyquant)
symbols <-c("VOOG", "NVDA", "TSLA")
stocks <- tq_get(x = symbols,
get = "stock.prices",
from = "2019-01-01",
to = "2022-01-01")
stocks
## # A tibble: 2,271 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 VOOG 2019-01-02 133. 135. 133. 135. 239200 129.
## 2 VOOG 2019-01-03 134. 134. 131. 131. 177400 126.
## 3 VOOG 2019-01-04 133. 137. 133. 136. 198400 130.
## 4 VOOG 2019-01-07 136. 138. 136. 137. 262000 131.
## 5 VOOG 2019-01-08 139. 139. 137. 139. 178600 133.
## 6 VOOG 2019-01-09 139. 140. 138. 139. 207000 133.
## 7 VOOG 2019-01-10 139. 140. 138. 140. 156200 134.
## 8 VOOG 2019-01-11 139. 140. 139. 140. 123400 134.
## 9 VOOG 2019-01-14 139. 139. 138. 139. 75500 133.
## 10 VOOG 2019-01-15 139. 141. 139. 141. 128500 135.
## # ℹ 2,261 more rows
asset_returns_tbl <- stocks %>%
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: 36 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 VOOG 2019-03-29 0.140
## 2 VOOG 2019-06-28 0.0444
## 3 VOOG 2019-09-30 0.00714
## 4 VOOG 2019-12-31 0.0793
## 5 VOOG 2020-03-31 -0.155
## 6 VOOG 2020-06-30 0.231
## 7 VOOG 2020-09-30 0.111
## 8 VOOG 2020-12-31 0.102
## 9 VOOG 2021-03-31 0.0225
## 10 VOOG 2021-06-30 0.112
## # ℹ 26 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.01) +
facet_wrap(~asset,ncol = 1) +
labs(title = "Distribution of monthly returns 2012-2016",
y = "freqeuncy",
x = "rate of returns",
caption = "")
Tsla was my riskiest stock as in some quarters it had the highest return, but it also had the biggest decrease in quarters. As voog was a lot differnce with a constent increase in rate or return.
Hide the code, messages, and warnings