{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
```{r} library(pacman) pacman::p_load(data.table, fixest, BatchGetSymbols, finreportr, ggplot2, lubridate, esquisse)
Download stock price data
```{r stock-price, warning = FALSE, message = FALSE}
## Set parameters
first.date <- Sys.Date() - 2500
last.date <- Sys.Date()
freq.data <- "monthly"
tickers <- c("TSLA", "NIO", "PRPL", "AAPL", "SNAP", "MU", "AMD",
"NVDA", "TWTR")
## Get Stock Prices
stocks <- BatchGetSymbols(tickers = tickers,
first.date = first.date,
last.date = last.date,
freq.data = freq.data,
do.cache = FALSE,
thresh.bad.data = 0)
head(stocks)
{r } ## Verify Returns stocks_DT <- stocks$df.tickers %>% setDT() %>% # Convert to data.table .[order(ticker, ref.date)] # Order by ticker and date
```{r } library(ggplot2)
ggplot(stocks_DT, aes(x = ref.date, y = price.close, color = ticker)) + geom_line() + facet_wrap(~ ticker, ncol = 3) + labs(title = “Stock Prices Over Time”, x = “Date”, y = “Closing Price”, color = “Ticker”) + theme_minimal() + theme(legend.position = “right”) ```