library(tidyverse)
library(tidyquant)
library(lubridate)
library(scales)
# Stock tickers
tickers <- c("AAPL","AMD","MU","NIO","NVDA","PRPL","SNAP","TSLA")
# Date range
start_date <- as.Date("2016-01-01")
end_date <- as.Date("2024-12-31")
# Download daily adjusted prices
prices_daily <- tq_get(
tickers,
from = start_date,
to = end_date,
get = "stock.prices"
) %>%
select(symbol, date, adjusted) %>%
arrange(symbol, date)
# Sample every 5 trading days
sample_every <- 5
prices_sampled <- prices_daily %>%
group_by(symbol) %>%
mutate(row_id = row_number()) %>%
filter(row_id %% sample_every == 1) %>%
ungroup() %>%
select(-row_id)
# Plot 1: Sampled stock prices
ggplot(prices_sampled, aes(x = date, y = adjusted, color = symbol)) +
geom_line(linewidth = 0.9) +
labs(
title = "Sampled Stock Prices (2016–2024)",
x = "Date",
y = "Adjusted Close Price",
color = "Stock"
) +
theme_minimal()

# Normalize prices (base = 1)
prices_normalized <- prices_sampled %>%
group_by(symbol) %>%
mutate(norm_price = adjusted / first(adjusted)) %>%
ungroup()
# Plot 2: Normalized sampled prices
ggplot(prices_normalized, aes(x = date, y = norm_price, color = symbol)) +
geom_line(linewidth = 0.9) +
labs(
title = "Normalized Stock Prices (2016–2024)",
x = "Date",
y = "Normalized Price (Base = $1)",
color = "Stock"
) +
theme_minimal()
