1. Download Daily Prices

The daily stock prices are downloaded from Yahoo Finance from January 1, 2024 through the current date. The adjusted closing price is used to calculate daily returns because it accounts for corporate actions such as dividends and stock splits.

packages <- c("quantmod", "dplyr", "tidyr", "knitr", "ggplot2")

installed <- rownames(installed.packages())
for (p in packages) {
  if (!(p %in% installed)) install.packages(p, repos = "https://cloud.r-project.org")
}
## package 'xts' successfully unpacked and MD5 sums checked
## package 'zoo' successfully unpacked and MD5 sums checked
## package 'TTR' successfully unpacked and MD5 sums checked
## package 'curl' successfully unpacked and MD5 sums checked
## package 'quantmod' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\DELL\AppData\Local\Temp\RtmpIx16N5\downloaded_packages
## package 'utf8' successfully unpacked and MD5 sums checked
## package 'pkgconfig' successfully unpacked and MD5 sums checked
## package 'withr' successfully unpacked and MD5 sums checked
## package 'generics' successfully unpacked and MD5 sums checked
## package 'glue' successfully unpacked and MD5 sums checked
## package 'magrittr' successfully unpacked and MD5 sums checked
## package 'pillar' successfully unpacked and MD5 sums checked
## package 'tibble' successfully unpacked and MD5 sums checked
## package 'tidyselect' successfully unpacked and MD5 sums checked
## package 'vctrs' successfully unpacked and MD5 sums checked
## package 'dplyr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\DELL\AppData\Local\Temp\RtmpIx16N5\downloaded_packages
## package 'stringi' successfully unpacked and MD5 sums checked
## package 'purrr' successfully unpacked and MD5 sums checked
## package 'stringr' successfully unpacked and MD5 sums checked
## package 'cpp11' successfully unpacked and MD5 sums checked
## package 'tidyr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\DELL\AppData\Local\Temp\RtmpIx16N5\downloaded_packages
## package 'farver' successfully unpacked and MD5 sums checked
## package 'labeling' successfully unpacked and MD5 sums checked
## package 'RColorBrewer' successfully unpacked and MD5 sums checked
## package 'viridisLite' successfully unpacked and MD5 sums checked
## package 'gtable' successfully unpacked and MD5 sums checked
## package 'isoband' successfully unpacked and MD5 sums checked
## package 'S7' successfully unpacked and MD5 sums checked
## package 'scales' successfully unpacked and MD5 sums checked
## package 'ggplot2' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\DELL\AppData\Local\Temp\RtmpIx16N5\downloaded_packages
library(quantmod)
library(dplyr)
library(tidyr)
library(knitr)
library(ggplot2)
tickers <- c("AAPL", "MSFT", "GOOG", "AMZN", "TSM", "NVDA")

prices_list <- lapply(tickers, function(ticker) {
  getSymbols(
    ticker,
    src = "yahoo",
    from = "2024-01-01",
    to = Sys.Date(),
    auto.assign = FALSE
  )
})

names(prices_list) <- tickers

Yahoo Finance provides historical daily market data, and quantmod::getSymbols() can retrieve Yahoo Finance OHLC data for specified date ranges.

2. Extract Adjusted Closing Prices

adjusted_prices <- lapply(names(prices_list), function(ticker) {
  data.frame(
    Date = index(prices_list[[ticker]]),
    Price = as.numeric(Ad(prices_list[[ticker]])),
    Ticker = ticker
  )
}) |>
  bind_rows()

head(adjusted_prices)
##         Date    Price Ticker
## 1 2024-01-02 183.4040   AAPL
## 2 2024-01-03 182.0307   AAPL
## 3 2024-01-04 179.7189   AAPL
## 4 2024-01-05 178.9977   AAPL
## 5 2024-01-08 183.3250   AAPL
## 6 2024-01-09 182.9100   AAPL

3. Compute Daily Returns

Daily simple returns are calculated as:

\[ R_t = \frac{P_t}{P_{t-1}} - 1 \]

where \(P_t\) is the adjusted closing price on day \(t\).

returns <- adjusted_prices |>
  arrange(Ticker, Date) |>
  group_by(Ticker) |>
  mutate(
    Daily_Return = Price / lag(Price) - 1
  ) |>
  ungroup()

4. First Few Daily Returns for All Stocks

first_returns <- returns |>
  filter(!is.na(Daily_Return)) |>
  group_by(Ticker) |>
  slice_head(n = 5) |>
  ungroup() |>
  select(Date, Ticker, Price, Daily_Return) |>
  arrange(Date, Ticker)

first_returns |>
  mutate(Daily_Return = round(Daily_Return, 6)) |>
  kable(
    caption = "First Five Daily Returns for Each Stock",
    digits = 6
  )
First Five Daily Returns for Each Stock
Date Ticker Price Daily_Return
2024-01-03 AAPL 182.03075 -0.007488
2024-01-03 AMZN 148.47000 -0.009738
2024-01-03 GOOG 139.04303 0.005732
2024-01-03 MSFT 362.85364 -0.000728
2024-01-03 NVDA 47.43152 -0.012436
2024-01-03 TSM 96.95766 -0.013395
2024-01-04 AAPL 179.71895 -0.012700
2024-01-04 AMZN 144.57001 -0.026268
2024-01-04 GOOG 136.74478 -0.016529
2024-01-04 MSFT 360.24921 -0.007178
2024-01-04 NVDA 47.85929 0.009019
2024-01-04 TSM 95.95103 -0.010382
2024-01-05 AAPL 178.99773 -0.004013
2024-01-05 AMZN 145.24000 0.004634
2024-01-05 GOOG 136.10091 -0.004709
2024-01-05 MSFT 360.06317 -0.000516
2024-01-05 NVDA 48.95511 0.022897
2024-01-05 TSM 96.41562 0.004842
2024-01-08 AAPL 183.32498 0.024175
2024-01-08 AMZN 149.10001 0.026577
2024-01-08 GOOG 139.21144 0.022855
2024-01-08 MSFT 366.85806 0.018871
2024-01-08 NVDA 52.10199 0.064281
2024-01-08 TSM 98.96128 0.026403
2024-01-09 AAPL 182.91003 -0.002263
2024-01-09 AMZN 151.36999 0.015225
2024-01-09 GOOG 141.22237 0.014445
2024-01-09 MSFT 367.93512 0.002936
2024-01-09 NVDA 52.98642 0.016975
2024-01-09 TSM 98.62251 -0.003423

5. Returns in Wide Format

returns_wide <- returns |>
  select(Date, Ticker, Daily_Return) |>
  pivot_wider(
    names_from = Ticker,
    values_from = Daily_Return
  )

head(returns_wide) |>
  kable(
    caption = "Daily Returns for All Stocks",
    digits = 6
  )
Daily Returns for All Stocks
Date AAPL AMZN GOOG MSFT NVDA TSM
2024-01-02 NA NA NA NA NA NA
2024-01-03 -0.007488 -0.009738 0.005732 -0.000728 -0.012436 -0.013395
2024-01-04 -0.012700 -0.026268 -0.016529 -0.007178 0.009019 -0.010382
2024-01-05 -0.004013 0.004634 -0.004709 -0.000516 0.022897 0.004842
2024-01-08 0.024175 0.026577 0.022855 0.018871 0.064281 0.026403
2024-01-09 -0.002263 0.015225 0.014445 0.002936 0.016975 -0.003423

6. Plot Daily Returns

ggplot(
  returns |>
    filter(!is.na(Daily_Return)),
  aes(x = Date, y = Daily_Return)
) +
  geom_line() +
  facet_wrap(~Ticker, scales = "free_y", ncol = 2) +
  labs(
    title = "Daily Stock Returns, 2024 to Present",
    x = "Date",
    y = "Daily Return"
  ) +
  theme_minimal()

7. Basic Summary Statistics

summary_stats <- returns |>
  filter(!is.na(Daily_Return)) |>
  group_by(Ticker) |>
  summarise(
    Observations = n(),
    Mean_Return = mean(Daily_Return),
    SD_Return = sd(Daily_Return),
    Minimum = min(Daily_Return),
    Maximum = max(Daily_Return)
  )

summary_stats |>
  mutate(
    Mean_Return = round(Mean_Return, 6),
    SD_Return = round(SD_Return, 6),
    Minimum = round(Minimum, 6),
    Maximum = round(Maximum, 6)
  ) |>
  kable(
    caption = "Summary Statistics of Daily Returns",
    digits = 6
  )
Summary Statistics of Daily Returns
Ticker Observations Mean_Return SD_Return Minimum Maximum
AAPL 675 0.001032 0.017467 -0.092456 0.153289
AMZN 675 0.001006 0.020517 -0.089791 0.153206
GOOG 675 0.001497 0.019208 -0.075061 0.099709
MSFT 675 0.000602 0.016884 -0.099932 0.155067
NVDA 675 0.002705 0.030398 -0.169682 0.187227
TSM 675 0.002537 0.025986 -0.133270 0.122940

Conclusion

This analysis downloads daily adjusted prices for AAPL, MSFT, GOOG, AMZN, TSM, and NVDA from 2024 to the present, calculates daily simple returns, displays the first five returns for each stock, and provides summary statistics and return plots. Because the data are downloaded using Sys.Date(), re-knitting the document updates the analysis automatically through the current date.