title: “Financial Database Management and Application 3C” author: “Chinguun” date: “September 15, 2026” output: html_document: toc: true toc_float: true df_print: paged —————
This assignment downloads daily stock prices for AAPL, MSFT, GOOG, AMZN, TSM, and NVDA from January 1, 2024 to the present date. It then calculates daily returns and displays the first few returns for all six stocks.
The data source is Yahoo Finance. I use Adjusted Close prices because they are adjusted for stock splits and distributions and are suitable for calculating returns.
First, I load the required packages and set the stock tickers and dates.
# Install packages if they are not already installed
required_packages <- c("quantmod", "dplyr", "tidyr", "knitr")
installed <- rownames(installed.packages())
for (p in required_packages) {
if (!(p %in% installed)) {
install.packages(p, repos = "https://cloud.r-project.org")
}
}
## 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\user\AppData\Local\Temp\RtmpSe8AKI\downloaded_packages
# Load packages
library(quantmod)
library(dplyr)
library(tidyr)
library(knitr)
# Stock tickers
tickers <- c("AAPL", "MSFT", "GOOG", "AMZN", "TSM", "NVDA")
# Start and end dates
start_date <- as.Date("2024-01-01")
end_date <- Sys.Date()
The following code downloads the daily adjusted closing prices from Yahoo Finance.
# Download daily stock prices
price_list <- lapply(tickers, function(tkr) {
x <- getSymbols(
tkr,
src = "yahoo",
from = start_date,
to = end_date + 1,
auto.assign = FALSE
)
data.frame(
Date = index(x),
Price = as.numeric(Ad(x)),
Ticker = tkr,
row.names = NULL
)
})
# Combine all stocks into one data frame
prices_long <- bind_rows(price_list) %>%
arrange(Date, Ticker)
# Show the first few observations
head(prices_long, 10)
## Date Price Ticker
## 1 2024-01-02 183.40401 AAPL
## 2 2024-01-02 149.92999 AMZN
## 3 2024-01-02 138.25056 GOOG
## 4 2024-01-02 363.11792 MSFT
## 5 2024-01-02 48.02879 NVDA
## 6 2024-01-02 98.27405 TSM
## 7 2024-01-03 182.03075 AAPL
## 8 2024-01-03 148.47000 AMZN
## 9 2024-01-03 139.04301 GOOG
## 10 2024-01-03 362.85355 MSFT
The following table shows the first date, last date, and number of observations downloaded for each stock.
summary_table <- prices_long %>%
group_by(Ticker) %>%
summarise(
First_Date = min(Date),
Last_Date = max(Date),
Observations = n(),
.groups = "drop"
)
kable(
summary_table,
caption = "Daily Adjusted Price Data"
)
| Ticker | First_Date | Last_Date | Observations |
|---|---|---|---|
| AAPL | 2024-01-02 | 2026-09-14 | 677 |
| AMZN | 2024-01-02 | 2026-09-14 | 677 |
| GOOG | 2024-01-02 | 2026-09-14 | 677 |
| MSFT | 2024-01-02 | 2026-09-14 | 677 |
| NVDA | 2024-01-02 | 2026-09-14 | 677 |
| TSM | 2024-01-02 | 2026-09-14 | 677 |
Daily simple returns are calculated using the following formula:
\[ R_t = \frac{P_t}{P_{t-1}} - 1 \]
where \(P_t\) is the adjusted closing price on day \(t\), and \(P_{t-1}\) is the adjusted closing price on the previous trading day.
# Calculate daily returns
returns_long <- prices_long %>%
group_by(Ticker) %>%
arrange(Date, .by_group = TRUE) %>%
mutate(
Return = Price / lag(Price) - 1
) %>%
ungroup()
# Create wide format
returns_wide <- returns_long %>%
select(Date, Ticker, Return) %>%
pivot_wider(
names_from = Ticker,
values_from = Return
) %>%
arrange(Date)
The first observation for each stock is NA because there
is no previous trading-day price. Therefore, I display the first five
non-missing daily returns for each stock.
first_returns <- returns_long %>%
filter(!is.na(Return)) %>%
group_by(Ticker) %>%
slice_head(n = 5) %>%
ungroup() %>%
select(Date, Ticker, Return) %>%
mutate(
Return = sprintf("%.4f%%", Return * 100)
) %>%
pivot_wider(
names_from = Ticker,
values_from = Return
)
kable(
first_returns,
caption = "First Five Daily Returns"
)
| Date | AAPL | AMZN | GOOG | MSFT | NVDA | TSM |
|---|---|---|---|---|---|---|
| 2024-01-03 | -0.7488% | -0.9738% | 0.5732% | -0.0728% | -1.2436% | -1.3395% |
| 2024-01-04 | -1.2700% | -2.6268% | -1.6529% | -0.7177% | 0.9018% | -1.0382% |
| 2024-01-05 | -0.4013% | 0.4634% | -0.4709% | -0.0516% | 2.2897% | 0.4842% |
| 2024-01-08 | 2.4175% | 2.6577% | 2.2855% | 1.8872% | 6.4281% | 2.6403% |
| 2024-01-09 | -0.2263% | 1.5225% | 1.4445% | 0.2936% | 1.6975% | -0.3423% |
The following table shows the first ten rows of daily returns for all six stocks.
returns_wide_display <- returns_wide %>%
slice_head(n = 10) %>%
mutate(
across(
all_of(tickers),
~ sprintf("%.4f%%", .x * 100)
)
)
kable(
returns_wide_display,
caption = "First Ten Rows of Daily Returns"
)
| Date | AAPL | AMZN | GOOG | MSFT | NVDA | TSM |
|---|---|---|---|---|---|---|
| 2024-01-02 | NA% | NA% | NA% | NA% | NA% | NA% |
| 2024-01-03 | -0.7488% | -0.9738% | 0.5732% | -0.0728% | -1.2436% | -1.3395% |
| 2024-01-04 | -1.2700% | -2.6268% | -1.6529% | -0.7177% | 0.9018% | -1.0382% |
| 2024-01-05 | -0.4013% | 0.4634% | -0.4709% | -0.0516% | 2.2897% | 0.4842% |
| 2024-01-08 | 2.4175% | 2.6577% | 2.2855% | 1.8872% | 6.4281% | 2.6403% |
| 2024-01-09 | -0.2263% | 1.5225% | 1.4445% | 0.2936% | 1.6975% | -0.3423% |
| 2024-01-10 | 0.5671% | 1.5591% | 0.8698% | 1.8574% | 2.2770% | -1.0698% |
| 2024-01-11 | -0.3223% | 0.9432% | -0.0904% | 0.4859% | 0.8684% | 0.4167% |
| 2024-01-12 | 0.1778% | -0.3609% | 0.3968% | 0.9984% | -0.2043% | 0.0198% |
| 2024-01-16 | -1.2317% | -0.9442% | -0.1109% | 0.4634% | 3.0561% | 0.4247% |
In this assignment, I downloaded daily adjusted closing prices for AAPL, MSFT, GOOG, AMZN, TSM, and NVDA from January 1, 2024 to the present date using Yahoo Finance.
I calculated the daily returns for each stock using the percentage change in adjusted closing prices. The first five non-missing daily returns were displayed for all six stocks.
The return data can be used for further financial analysis, such as comparing stock performance, measuring volatility and risk, and studying the relationship between different stocks.
Data source: Yahoo Finance through the R
quantmod package.