Reading Financial Time Series Data with R

September 17, 2015

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

by Joseph Rickert

In a recent post focused on plotting time series with the new dygraphs package, I did not show how easy it is to read financial data into R. However, in a thoughtful comment to the post, Achim Zeileis pointed out a number of features built into the basic R time series packages that everyone ought to know. In this post, I will just elaborate a little on what Achim sketched out. First off, I began the previous post with url strings that point to stock data for IBM and LinkedIn. Yahoo Finance make this sort of thing easy. A quick search for a stock will bring you to a page with historical stock prices. Then, its is only a matter of copying the url to the associated csv file, and as Achim points out:

If you already have the download URLs ibm_url and lnkd_url, then you can also simply use zoo::read.zoo() and merge the resulting closing prices:

z <- merge(
  ibm = read.zoo(ibm_url, header = TRUE, sep = ",")$Close,
  lnkd = read.zoo(lnkd_url, header = TRUE, sep = ",")$Close

And the ggplot2 figure can just be drawn with the autoplot() method for zoo series:

autoplot(z, facets = NULL)

The resulting plot takes you most of the way to the ggplot produced in the post.


The final formatting can be accomplished with the additional ggplot commands used in my post. This is just delightful: 3 lines of code that fetch the data and prepare it for plotting, and half-line to get a sophisticated default plot.

Of course, the manual step of hunting for urls is completely unnecessary. The get.historic.quote function in the tseries package will fetch a time series object for you that can also be plotted with the zoo autoplot function.

ibm2 <- get.hist.quote("IBM", quote="Close", provider="yahoo",
            start="2010-08-24", end = "2015-09-02", retclass="zoo")

If you are really interested in working like a quant then the defaults in the quantmod package will give you the look and feel of a traders screen. The function:


will bring the "xts" / "zoo" time series object, IBM, containing historic IBM stock data directly into your work space with no need even to make an assignment!

           IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume
2007-01-03    97.18    98.40   96.26     97.27    9199500
2007-01-04    97.25    98.79   96.88     98.31   10557200
2007-01-05    97.60    97.95   96.91     97.42    7222900
2007-01-08    98.50    99.50   98.35     98.90   10340100
2007-01-09    99.08   100.33   99.07    100.07   11108900
2007-01-10    98.50    99.05   97.93     98.89    8744900

Moreover, IBM is an "OHLC" object that, with the right plotting function like chartSeries from the quantmod package, will produce the kind of open-high-low-close charts favored by stock analysts for charting financial instruments. (There is even an R function to determine if you have an OHLC object.)

#[1] TRUE


The getSymbols function will fetch data from the Yahoo, Google, FRED and oanda financial services sites and also read as well as reading from MySQL data bases and .csv and RData files.

Quandl, however, is probably the best place to go for free (and premium) financial data. Once you signup for an account with Quandl the following code will get data frame with several columns of IBM stock information.

token <- "your_token_string"
Quandl.auth(token)	# Authenticate your token
ibmQ = Quandl("WIKI/IBM", start_date="2010-08-24", end_date="2015-09-03")
Date   Open   High    Low  Close  Volume Ex-Dividend Split Ratio Adj. Open Adj. High Adj. Low Adj. Close Adj. Volume
1 2015-09-02 144.92 145.08 143.18 145.05 4243473           0           1    144.92    145.08   143.18     145.05     4243473
2 2015-09-01 144.84 144.98 141.85 142.68 5258877           0           1    144.84    144.98   141.85     142.68     5258877
3 2015-08-31 147.26 148.40 146.26 147.89 4093078           0           1    147.26    148.40   146.26     147.89     4093078
4 2015-08-28 147.75 148.20 147.18 147.98 4058832           0           1    147.75    148.20   147.18     147.98     4058832
5 2015-08-27 148.63 148.97 145.66 148.54 4762003           0           1    148.63    148.97   145.66     148.54     4762003
6 2015-08-26 144.09 146.98 142.14 146.70 6186742           0           1    144.09    146.98   142.14     146.70     6186742

Here, I have presented just some of the very basics, still not coming close to describing all that R offers for acquiring and manipulating financial time series information.

As a final note: a couple of years ago I posted a short tutorial for getting started with the Quandl R API. Unfortunately, since that time Quandl has changed it's coding scheme so my R code from that tutorial will not run without changes. The code in the file Quandl_code, however, produces the following plot of Asian Currency exchange rates and may serve as an updated example.


Look here to decipher the Quandl currency codes.


To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)