install.packages("pacman")
library(pacman)
install.packages("esquisse", dependencies=TRUE)
library(esquisse)
install.packages("ggplot2", dependencies=TRUE)
library(ggplot2)
pacman::p_load(data.table, fixest, BatchGetSymbols, finreportr, ggplot2, lubridate, esquisse)
first.date <- Sys.Date() - 2500
> last.date <- Sys.Date()
> freq.data <- "monthly"
> tickers <- c("TSLA", "NIO", "PRPL", "AAPL", "SNAP", "MU", "AMD",
+ "NVDA", "TWTR")
> stocks <- BatchGetSymbols(tickers = tickers,
+ first.date = first.date,
+ last.date = last.date,
+ freq.data = freq.data,
+ do.cache = FALSE,
+ thresh.bad.data = 0)
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 | Looking good!
NIO | yahoo (2|9) - Got 61% of valid prices | Well done!
PRPL | yahoo (3|9) - Got 100% of valid prices | Nice!
AAPL | yahoo (4|9) - Got 100% of valid prices | Youre doing good!
SNAP | yahoo (5|9) - Got 84% of valid prices | Feels good!
MU | yahoo (6|9) - Got 100% of valid prices | Well done!
AMD | yahoo (7|9) - Got 100% of valid prices | Youre doing good!
NVDA | yahoo (8|9) - Got 100% of valid prices | OK!
TWTR | yahoo (9|9) - Got 99% of valid prices | Feels good!Warning message:
`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.
## Verify Returns
> stocks_DT <- stocks$df.tickers %>% setDT() %>% # Convert to data.table
+ .[order(ticker, ref.date)] # Order by ticker and date
> head(stocks_DT)
ticker ref.date volume price.open price.high price.low price.close
1: AAPL 2016-01-19 2494882400 24.6025 25.3825 23.0975 24.3350
2: AAPL 2016-02-01 3243450400 24.1175 24.7225 23.1475 24.1725
3: AAPL 2016-03-01 2984198400 24.4125 27.6050 24.3550 27.2475
4: AAPL 2016-04-01 3489534800 27.1950 28.0975 23.1275 23.4350
5: AAPL 2016-05-02 3602686000 23.4925 25.1825 22.3675 24.9650
6: AAPL 2016-06-01 3117990800 24.7550 25.4725 22.8750 23.9000
price.adjusted ret.adjusted.prices ret.closing.prices
1: 22.27824 NA NA
2: 22.24956 -0.001287489 -0.006677543
3: 25.07994 0.127210521 0.127210585
4: 21.57073 -0.139920886 -0.139921099
5: 23.11892 0.071772861 0.065287009
6: 22.13268 -0.042659600 -0.042659724
ggplot(stocks_DT) +
+ aes(x = ref.date, y = price.close, fill = ticker) +
+ labs(x = "Date", y = "Closing price") +
+ geom_line(size = 0.5, colour = "#112446") +
+ scale_fill_manual(values = c(AAPL = "#F8766D", AMD = "#D39200", MU = "#93AA00", NIO = "#00BA38",
+ NVDA = "#00C19F", PRPL = "#00B9E3", SNAP = "#619CFF", TSLA = "#DB72FB", TWTR = "#FF61C3")) +
+ theme_gray() +
+ facet_wrap(vars(ticker), scales = "free_y")

ggplot(stocks_DT) +
+ aes(x = ref.date, y = ret.adjusted.prices, fill = ticker, colour = ticker) +
+ labs(x = "Date", y = "Monthly return") +
+ geom_line(size = 0.5) +
+ scale_fill_hue(direction = 1) +
+ scale_color_hue(direction = 1) +
+ theme_gray() +
+ facet_wrap(vars(ticker))
