library(pacman) #pacman allows us to use p_load to help load function more efficiently.
pacman::p_load(data.table, BatchGetSymbols, finreportr, ggplot2, lubridate, ggthemes)
#batchgetsymbols = closing/open/high/lowprices/volume/returns
#finreportr = income statement, cash flow statement, balance sheet. 

#df.FTSE <- GetFTSE100Stocks() #Get the FTSE 100 stocks #tickers <- df.FTSE$Tickers

first.date <- Sys.Date() - 1000 #current date - 1000 days
last.date <- Sys.Date()
freq.data <- "monthly"
tickers <- c("TSLA", "CRSR", "AMC", "BB", "SPWR", "DOGE")

#download the stock prices from yahoo finance. 




stocks <- BatchGetSymbols(tickers = tickers, 
                         first.date = first.date,
                         last.date = last.date, 
                         freq.data = freq.data,
                         do.cache = FALSE, # save data onto computer 
                         thresh.bad.data = 0) #eliminate stock if not enough observation
## 
## Running BatchGetSymbols for:
##    tickers =TSLA, CRSR, AMC, BB, SPWR, DOGE
##    Downloading data for benchmark ticker
## ^GSPC | yahoo (1|1)
## TSLA | yahoo (1|6) - Got 100% of valid prices | Nice!
## CRSR | yahoo (2|6) - Got 14% of valid prices | Youre doing good!
## AMC | yahoo (3|6) - Got 100% of valid prices | Nice!
## BB | yahoo (4|6) - Got 100% of valid prices | You got it!
## SPWR | yahoo (5|6) - Got 100% of valid prices | OK!
## DOGE | yahoo (6|6) - Error in download..
#the result of this function above can be seen in $df.control showing the ticker, source, download, total observation, and percentage that were corrputed from the thresh.bad.data

## Verify Returns
stocks_DT <- stocks$df.tickers %>% setDT() %>%     # extract data, #setDT = changes data frame into a table
  .[order(ticker, ref.date)]                      # Order by ticker and date
 

## Graph monthly return and price
#%in% searches the operator to CHECK if said element is in the function EX. Is "Nio" inside the ticker function?
#one chart of all returns
returns_plot_all <- ggplot(stocks_DT, aes(x= ref.date, y = ret.adjusted.prices, colour = ticker)) +
  geom_line(size=1.3) + theme_bw() + labs(title = "Multiple Stocks Monthly Return Comparision", x = "Year", y= "Monthly Returns") 
returns_plot_all

#one chart of all prices
price_plot_all <- ggplot(stocks_DT, aes(x= ref.date, y = price.close, colour = ticker)) +
  geom_line(size=1.3) + theme_hc() + labs(title = "Multiple Stocks Price Comparision", x = "Year", y= "Closing Price") 
price_plot_all

#multiple chart of returns
returns <- ggplot(stocks_DT[ticker %in% c("AMC", "BB", "DOGE")], aes(x = ref.date, y = ret.adjusted.prices)) + geom_line(size=1.3) + facet_wrap(~ticker, scales = "free_y") + theme_economist() + labs(x = "Year", y= "Monthly Returns") 
returns

#multiple chart of prices
prices <- ggplot(stocks_DT[ticker %in% c("AAPL","TSLA","SPWR")], aes(x = ref.date, y = price.close)) + geom_line(size=1.3) + facet_wrap(~ticker, scales = "free_y") + theme_hc() + labs(x = "Year", y= "Closing Price") 
prices