library(pacman)
pacman::p_load(data.table, fixest, BatchGetSymbols, finreportr, ggplot2, lubridate, esquisse)

Download stock price data

## 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,5)
## Verify Returns
stocks_DT <- stocks$df.tickers %>% setDT() %>%         
# Convert to data.table
  .[order(ticker, ref.date)]                          
# Order by ticker and date
#plot stock prices
#install.packages("esquisse")
library(esquisse)
library(ggplot2)
ggplot(stocks_DT) +
 aes(x = ref.date, y = price.close, fill = ret.closing.prices) +
 geom_line(colour = "#112446") +
 scale_fill_gradient() +
 theme_gray() +
 theme(legend.position = "none") +
 facet_wrap(vars(ticker), 
 scales = "free_y")

library(ggplot2)

ggplot(stocks_DT) +
 aes(x = ref.date, y = ret.adjusted.prices, colour = ticker) +
 geom_line() +
 scale_color_hue(direction = 1) +
 theme_gray() +
 facet_wrap(vars(ticker))
## Warning: Removed 9 rows containing missing values (`geom_line()`).