<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-to-download-and-organize-financial-data-from-yahoo-finance-for-several-tickers-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://msperlin.github.io/2017-01-22-BatchGetSymbols/"> R and Finance</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> Using package BatchGetSymbols – </p>
<p>One of the great things of working in finance is that financial datasets<br>
are freely available from sources such as Google and Yahoo Finance. This<br>
is an excelent feature for building up to date content for classes and<br>
conducting academic research.</p>
<p>In the past I have used function GetSymbols from the CRAN package<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=quantmod">quantmod</a> in order to<br>
download end of day trade data for several stocks in the financial<br>
market. The problem in using GetSymbols is that it was not designed to<br>
take into consideration the aggregation of financial data for several<br>
tickers. In the usage of GetSymbols, each stock will have its own <code>xts</code><br>
object with different column names and this makes it troublesome to<br>
store data from several tickers in a single dataframe in the long<br>
format. Nonetheless, if you are using dplyr and ggplot2 to process and<br>
plot data, the long format is expected.</p>
<p>The package BatchGetSymbols is my solution to this problem. Based on a<br>
list of tickers and a time period, the function will download the data<br>
by automatically choosing the correct source, yahoo or google, and<br>
output two dataframes: a single dataframe with all the information for<br>
the stocks in the list and a dataframe with a report of the download<br>
process. The user can also set a benchmark ticker in order to compare<br>
dates and eliminate assets with a low number of observations from the<br>
resulting dataframe.</p>
<h2>A simple example</h2>
<p>As a simple exercise, let’s download data for three stocks, facebook<br>
(FB), 3M (MMM), PETR4.SA (PETROBRAS) and abcdef, a ticker I just made<br>
up. We will use the last 150 days as the time period. This example will<br>
show the simple interface of the package and how it handles invalid<br>
tickers.</p>
<div>
<pre><code>library(BatchGetSymbols)

## Loading required package: rvest

## Loading required package: xml2

## 
## Thank you for using BatchGetSymbols! If applicable, please use the following citation in your research report. Thanks! 
## 
## APA:
##  Perlin, M. (2016) BatchGetSymbols: Downloads and Organizes Financial Data for Multiple Tickers. CRAN Package, Available in https://CRAN.R-project.org/package=BatchGetSymbols. 
## 
## BIBTEX:
##  @misc{perlin2016batchgetsymbols,
##   title = {BatchGetSymbols: Downloads and Organizes Financial Data for Multiple Tickers},
##   author = {Marcelo Perlin},
##   year = {2016},
##   journal = {CRAN Package},
##   url = {https://CRAN.R-project.org/package=BatchGetSymbols}
## }
## }

first.date &lt;- Sys.Date()-150
last.date &lt;- Sys.Date()

tickers &lt;- c('FB','NYSE:MMM','PETR4.SA','abcdef')

l.out &lt;- BatchGetSymbols(tickers = tickers,
                         first.date = first.date,
                         last.date = last.date)

## 
## Running BatchGetSymbols for:
##    tickers = FB, NYSE:MMM, PETR4.SA, abcdef
##    Downloading data for benchmark ticker
## Downloading Data for FB from yahoo (1|4) - Boa!
## Downloading Data for NYSE:MMM from google (2|4) - Boa!
## Downloading Data for PETR4.SA from yahoo (3|4) - Good job!
## Downloading Data for abcdef from yahoo (4|4) - Error in download..
</code></pre>
</div>
<p>After downloading the data, we can check the success of the process for<br>
each ticker. Notice that the last ticker does not exist in yahoo finance<br>
or google and therefore results in an error. All information regarding<br>
the download process is provided in the dataframe df.control:</p>
<div>
<pre><code>print(l.out$df.control)

##     ticker    src download.status total.obs perc.benchmark.dates
## 1       FB  yahoo              OK       102                    1
## 2 NYSE:MMM google              OK       102                    1
## 3 PETR4.SA  yahoo              OK       106                    1
## 4   abcdef  yahoo          NOT OK         0                    0
##   threshold.decision
## 1               KEEP
## 2               KEEP
## 3               KEEP
## 4                OUT
</code></pre>
</div>
<p>Moreover, we can plot the daily closing prices using ggplot2:</p>
<div>
<pre><code>library(ggplot2)
 
p &lt;- ggplot(l.out$df.tickers, aes(x = ref.date, y = price.close))
p &lt;- p + geom_line()
p &lt;- p + facet_wrap(~ticker, scales = 'free_y') 
print(p)
</code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/msperlin.github.io/img/2017-01-02-BatchGetSymbols_files/figure-markdown_strict/plot.prices-1.png?w=456" data-recalc-dims="1"></p>
<h2>Downloading data for all tickers in the SP500 index</h2>
<p>A more advanced example would be to download data for all stocks in the<br>
current composition of the SP500 stock index. The package also includes<br>
a function that downloads the current composition of the SP500 index<br>
from the internet. By using this function along with BatchGetSymbols, we<br>
can easily import end-of-day data for all assets in the SP500 index.</p>
<p>Notice the following code, where we download data for the SP500 stocks<br>
for the last year. The code is not executed in this example given its<br>
time duration, but you can just copy and paste on its own R script in<br>
order to check the results. In my computer it takes around 5 minutes to<br>
download the whole dataset.</p>
<div>
<pre><code>library(BatchGetSymbols)

first.date &lt;- Sys.Date()-365
last.date &lt;- Sys.Date()

df.SP500 &lt;- GetSP500Stocks()
tickers &lt;- df.SP500$tickers

l.out &lt;- BatchGetSymbols(tickers = tickers,
                         first.date = first.date,
                         last.date = last.date)

print(l.out$df.control)
print(l.out$df.tickers)
</code></pre>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://msperlin.github.io/2017-01-22-BatchGetSymbols/"> R and Finance</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/hWXqpcP-yy8"></a></div>