This report downloads daily stock prices for AAPL, MSFT, GOOG, AMZN, TSM, and NVDA from January 1, 2024 to the present, and computes their daily returns.
library(quantmod)
library(PerformanceAnalytics)
tickers <- c("AAPL", "MSFT", "GOOG", "AMZN", "TSM", "NVDA")
start_date <- "2024-01-01"
end_date <- Sys.Date()
getSymbols(tickers, src = "yahoo", from = start_date, to = end_date)
## [1] "AAPL" "MSFT" "GOOG" "AMZN" "TSM" "NVDA"
prices <- do.call(merge, lapply(tickers, function(t) Ad(get(t))))
colnames(prices) <- tickers
head(prices)
## AAPL MSFT GOOG AMZN TSM NVDA
## 2024-01-02 183.4040 363.1179 138.2505 149.93 98.27406 48.02880
## 2024-01-03 182.0308 362.8536 139.0430 148.47 96.95766 47.43152
## 2024-01-04 179.7189 360.2491 136.7448 144.57 95.95101 47.85929
## 2024-01-05 178.9977 360.0631 136.1009 145.24 96.41563 48.95511
## 2024-01-08 183.3250 366.8581 139.2114 149.10 98.96128 52.10199
## 2024-01-09 182.9100 367.9351 141.2224 151.37 98.62251 52.98643
returns <- na.omit(ROC(prices, type = "discrete"))
colnames(returns) <- tickers
head(returns)
## AAPL MSFT GOOG AMZN TSM
## 2024-01-03 -0.007487707 -0.0007279819 0.005732427 -0.009737821 -0.013395102
## 2024-01-04 -0.012700208 -0.0071776367 -0.016528934 -0.026267892 -0.010382396
## 2024-01-05 -0.004012885 -0.0005164069 -0.004708813 0.004634420 0.004842289
## 2024-01-08 0.024174745 0.0188716442 0.022854736 0.026576704 0.026402841
## 2024-01-09 -0.002263287 0.0029358119 0.014445334 0.015224607 -0.003423315
## 2024-01-10 0.005671219 0.0185740503 0.008697967 0.015590941 -0.010697668
## NVDA
## 2024-01-03 -0.012435843
## 2024-01-04 0.009018659
## 2024-01-05 0.022896734
## 2024-01-08 0.064280954
## 2024-01-09 0.016975121
## 2024-01-10 0.022770001
summary(returns)
## Index AAPL MSFT
## Min. :2024-01-03 Min. :-0.092456 Min. :-0.0999314
## 1st Qu.:2024-09-04 1st Qu.:-0.007047 1st Qu.:-0.0072157
## Median :2025-05-07 Median : 0.001231 Median : 0.0006759
## Mean :2025-05-07 Mean : 0.001008 Mean : 0.0005931
## 3rd Qu.:2026-01-07 3rd Qu.: 0.009210 3rd Qu.: 0.0090349
## Max. :2026-09-10 Max. : 0.153289 Max. : 0.1550674
## GOOG AMZN TSM
## Min. :-0.075061 Min. :-0.0897913 Min. :-0.133270
## 1st Qu.:-0.009207 1st Qu.:-0.0102374 1st Qu.:-0.012303
## Median : 0.002127 Median : 0.0004179 Median : 0.002762
## Mean : 0.001477 Mean : 0.0009782 Mean : 0.002523
## 3rd Qu.: 0.011523 3rd Qu.: 0.0125254 3rd Qu.: 0.016052
## Max. : 0.099709 Max. : 0.1532059 Max. : 0.122940
## NVDA
## Min. :-0.169682
## 1st Qu.:-0.014100
## Median : 0.002658
## Mean : 0.002710
## 3rd Qu.: 0.019833
## Max. : 0.187227