1. Download Stock Prices

Download daily adjusted closing prices for six major stocks (AAPL, MSFT, GOOG, AMZN, TSM, NVDA) from January 1, 2024 to the present date using the quantmod package.

# Install and load required packages
if (!require("quantmod")) install.packages("quantmod")
if (!require("PerformanceAnalytics")) install.packages("PerformanceAnalytics")
if (!require("knitr")) install.packages("knitr")
if (!require("kableExtra")) install.packages("kableExtra")

library(quantmod)
library(PerformanceAnalytics)
library(knitr)
library(kableExtra)

# Define stock tickers
tickers <- c("AAPL", "MSFT", "GOOG", "AMZN", "TSM", "NVDA")

# Download daily stock price data from Yahoo Finance up to the present date
getSymbols(tickers, src = "yahoo", from = "2024-01-01", to = Sys.Date(), auto.assign = TRUE)
## [1] "AAPL" "MSFT" "GOOG" "AMZN" "TSM"  "NVDA"
# Merge Adjusted Close prices into a single time series object
prices <- merge(Ad(AAPL), Ad(MSFT), Ad(GOOG), Ad(AMZN), Ad(TSM), Ad(NVDA))
colnames(prices) <- tickers

# Convert to data.frame and include Date column explicitly
prices_df <- data.frame(Date = index(prices), coredata(prices))

# Display formatted price table (First 6 rows)
head(prices_df) %>%
  kbl(caption = "Table 1: Daily Adjusted Closing Prices (First 6 Days)", digits = 2, row.names = FALSE) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
                full_width = FALSE, 
                position = "center") %>%
  row_spec(0, bold = TRUE, color = "white", background = "#2C3E50")
Table 1: Daily Adjusted Closing Prices (First 6 Days)
Date AAPL MSFT GOOG AMZN TSM NVDA
2024-01-02 183.40 363.12 138.25 149.93 98.27 48.08
2024-01-03 182.03 362.85 139.04 148.47 96.96 47.48
2024-01-04 179.72 360.25 136.74 144.57 95.95 47.91
2024-01-05 179.00 360.06 136.10 145.24 96.42 49.01
2024-01-08 183.32 366.86 139.21 149.10 98.96 52.16
2024-01-09 182.91 367.94 141.22 151.37 98.62 53.05

2. Compute Daily Returns

Compute the daily percentage returns for each stock and display the first few observations.

# Calculate daily returns
daily_returns <- Return.calculate(prices)

# Omit NA and convert to data.frame with explicit Date column
returns_clean <- na.omit(daily_returns)
returns_df <- data.frame(Date = index(returns_clean), coredata(returns_clean))

# Display formatted returns table (First 6 rows)
head(returns_df) %>%
  kbl(caption = "Table 2: Daily Percentage Returns (First 6 Days)", digits = 4, row.names = FALSE) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
                full_width = FALSE, 
                position = "center") %>%
  row_spec(0, bold = TRUE, color = "white", background = "#2C3E50")
Table 2: Daily Percentage Returns (First 6 Days)
Date AAPL MSFT GOOG AMZN TSM NVDA
2024-01-03 -0.0075 -0.0007 0.0057 -0.0097 -0.0134 -0.0124
2024-01-04 -0.0127 -0.0072 -0.0165 -0.0263 -0.0104 0.0090
2024-01-05 -0.0040 -0.0005 -0.0047 0.0046 0.0048 0.0229
2024-01-08 0.0242 0.0189 0.0229 0.0266 0.0264 0.0643
2024-01-09 -0.0023 0.0029 0.0144 0.0152 -0.0034 0.0170
2024-01-10 0.0057 0.0186 0.0087 0.0156 -0.0107 0.0228