17/2/2021

Introduction

This presentation will show the code for downloading stock prices from Yahoo Finance for a given set of tickers, and then close price will be plotted using the library plotly. You can customize the set of tickers.

Code for adquire and process the data

# set dates
first.date <- Sys.Date() - 720
last.date <- Sys.Date()
freq.data <- 'daily'
# set tickers
tickers <- c('AAPL','GOOG','AMZN','MSFT')

l.out <- BatchGetSymbols(tickers = tickers, 
                         first.date = first.date,
                         last.date = last.date, 
                         freq.data = freq.data,
                         cache.folder = file.path(tempdir(), 
                                                  'BGS_Cache') ) # cache in tempdir()


stocks_orig <- data.frame(l.out$df.tickers)
stocks <- subset(stocks_orig, select = c(ticker, price.close, ref.date))

stocks <- stocks %>% 
    mutate(ref.date = as.Date(ref.date))

#plot_ly(stocks, x = ref.date, y = price.close, color = ticker)

Slide with Plot