Using BatchGetSymbols to download financial data for several tickers

Marcelo Perlin



One of the great things of working in finance is that financial datasets from capital markets are freely available from sources such as Google and Yahoo Finance. This is an excelent feature for building up to date content for classes and conducting academic research.

In the past I have used function GetSymbols from the CRAN package quantmod in order to download end of day trade data for several stocks in the financial market. The problem in using GetSymbols is that it does not aggregate or clean the financial data for several tickers. In the usage of GetSymbols, each stock will have its own xts object with different column names and this makes it harder to store data from several tickers in a single dataframe.

Package BatchGetSymbols is my solution to this problem. Based on a list of tickers and a time period, the function will download the data by automatically choosing the correct source, yahoo or google, and output two dataframes: a single dataframe with all the information for the stocks in the list and a dataframe with a report of the download process. User can also set a benchmark ticker in order to compare dates and eliminate assets with a low number of observations from the resulting dataframe.

Main features:

A simple example

As a simple exercise, let’s download data for three stocks, facebook (FB), 3M (MMM), PETR4.SA (PETROBRAS) and abcdef, a ticker I just made up. We will use the last 60 days as the time period. This example will show the simple interface of the package and how it handles invalid tickers.

if (!require(BatchGetSymbols)) install.packages('BatchGetSymbols')
## Loading required package: BatchGetSymbols
## Loading required package: rvest
## Loading required package: xml2

# set dates <- Sys.Date() - 60 <- Sys.Date()

# set tickers
tickers <- c('FB','NYSE:MMM','PETR4.SA','abcdef')

l.out <- BatchGetSymbols(tickers = tickers, 
                         cache.folder = file.path(tempdir(), 
                                                  'BGS_Cache') ) # cache in tempdir()
## Running BatchGetSymbols for:
##    tickers = FB, NYSE:MMM, PETR4.SA, abcdef
##    Downloading data for benchmark ticker | Not Cached
## FB | yahoo (1|4) | Not Cached - Got it!
## NYSE:MMM | google (2|4) | Not Cached - You got it!
## PETR4.SA | yahoo (3|4) | Not Cached - Well done!
## abcdef | yahoo (4|4) | Not Cached - Error in download..

After downloading the data, we can check the success of the process for each ticker. Notice that the last ticker does not exist in yahoo finance or google and therefore results in an error. All information regarding the download process is provided in the dataframe df.control:

##     ticker    src download.status total.obs perc.benchmark.dates
## 1       FB  yahoo              OK        38                    1
## 2 NYSE:MMM google              OK        38                    1
## 3 PETR4.SA  yahoo              OK        42                    1
## 4   abcdef  yahoo          NOT OK         0                    0
##   threshold.decision
## 1               KEEP
## 2               KEEP
## 3               KEEP
## 4                OUT

Moreover, we can plot the daily closing prices using ggplot2:

p <- ggplot(l.out$df.tickers, aes(x =, y = price.close))
p <- p + geom_line()
p <- p + facet_wrap(~ticker, scales = 'free_y') 

Downloading data for all tickers in the SP500 index

The package was designed for large scale download of financial data. An example is downloading all stocks in the current composition of the SP500 stock index. The package also includes a function that downloads the current composition of the SP500 index from the internet. By using this function along with BatchGetSymbols, we can easily import end-of-day data for all assets in the index.

In the following code we download data for the SP500 stocks for the last year. The code is not executed in this vignette given its time duration, but you can just copy and paste on its own R script in order to check the results. In my computer it takes around 5 minutes to download the whole dataset.

library(BatchGetSymbols) <- Sys.Date()-365 <- Sys.Date()

df.SP500 <- GetSP500Stocks()
tickers <- df.SP500$tickers

l.out <- BatchGetSymbols(tickers = tickers,