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

## 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)
## Warning: `BatchGetSymbols()` was deprecated in BatchGetSymbols 2.6.4.
## ℹ Please use `yfR::yf_get()` instead.
## ℹ 2022-05-01: Package BatchGetSymbols will soon be replaced by yfR.  More
##   details about the change is available at github
##   <<www.github.com/msperlin/yfR> You can install yfR by executing:
## 
## remotes::install_github('msperlin/yfR')
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## 
## Running BatchGetSymbols for:
##    tickers =TSLA, NIO, PRPL, AAPL, SNAP, MU, AMD, NVDA, TWTR
##    Downloading data for benchmark ticker
## ^GSPC | yahoo (1|1)
## TSLA | yahoo (1|9) - Got 100% of valid prices | OK!
## NIO | yahoo (2|9) - Got 91% of valid prices | Nice!
## PRPL | yahoo (3|9) - Got 100% of valid prices | Well done!
## AAPL | yahoo (4|9) - Got 100% of valid prices | Got it!
## SNAP | yahoo (5|9) - Got 100% of valid prices | OK!
## MU | yahoo (6|9) - Got 100% of valid prices | Got it!
## AMD | yahoo (7|9) - Got 100% of valid prices | Good job!
## NVDA | yahoo (8|9) - Got 100% of valid prices | You got it!
## TWTR | yahoo (9|9) - Error in download..
head(stocks)
## $df.control
## # A tibble: 8 × 6
##   ticker src   download.status total.obs perc.benchmark.dates threshold.decision
##   <chr>  <chr> <chr>               <int>                <dbl> <chr>             
## 1 TSLA   yahoo OK                   1724                1     KEEP              
## 2 NIO    yahoo OK                   1562                0.906 KEEP              
## 3 PRPL   yahoo OK                   1724                1     KEEP              
## 4 AAPL   yahoo OK                   1724                1     KEEP              
## 5 SNAP   yahoo OK                   1724                1     KEEP              
## 6 MU     yahoo OK                   1724                1     KEEP              
## 7 AMD    yahoo OK                   1724                1     KEEP              
## 8 NVDA   yahoo OK                   1724                1     KEEP              
## 
## $df.tickers
## # A tibble: 656 × 10
##    ticker ref.date       volume price.open price.high price.low price.close
##    <chr>  <date>          <dbl>      <dbl>      <dbl>     <dbl>       <dbl>
##  1 AAPL   2018-01-22 1282969200       44.3       44.9      41.2        41.9
##  2 AAPL   2018-02-01 3711577200       41.8       45.2      37.6        44.5
##  3 AAPL   2018-03-01 2854910800       44.6       45.9      41.2        41.9
##  4 AAPL   2018-04-02 2664617200       41.7       44.7      40.2        41.3
##  5 AAPL   2018-05-01 2483905200       41.6       47.6      41.3        46.7
##  6 AAPL   2018-06-01 2110498000       47.0       48.5      45.2        46.3
##  7 AAPL   2018-07-02 1574765600       46.0       49.0      45.9        47.6
##  8 AAPL   2018-08-01 2801275600       49.8       57.2      49.3        56.9
##  9 AAPL   2018-09-04 2715888000       57.1       57.4      53.8        56.4
## 10 AAPL   2018-10-01 3158994000       57.0       58.4      51.5        54.7
## # ℹ 646 more rows
## # ℹ 3 more variables: price.adjusted <dbl>, ret.adjusted.prices <dbl>,
## #   ret.closing.prices <dbl>
## Verify Returns
stocks_DT <- stocks$df.tickers %>% setDT() %>%          # Convert to data.table
  .[order(ticker, ref.date)]                           # Order by ticker and date
library(ggplot2)

# Create the plot
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")