Getting Financial Data in R

I have recently started providing consultancy to a hedge fund and as far as I can see, R looks like it has a good set of libraries for this domain. In my previous job I used an embedded domain specific language in Haskell (Frankau et al. 2009). I’d like to be able to use Haskell again but my impression is that publicly available libraries either do not exist or are not particularly mature although I would love to be proved wrong.

I’ve used knitr to produce this post rather than my usual BlogLiteratelyD.

For example, let us plot an index.

First we load the quantmod library

library(quantmod)

We can chart the S&P 500 for 2013.

GSPC <- getSymbols("^GSPC", src = "yahoo", auto.assign = FALSE)
dim(GSPC)
## [1] 1768    6
head(GSPC, 4)
##            GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume
## 2007-01-03      1418      1429     1408       1417   3.429e+09
## 2007-01-04      1417      1422     1408       1418   3.004e+09
## 2007-01-05      1418      1418     1406       1410   2.919e+09
## 2007-01-08      1409      1415     1404       1413   2.763e+09
##            GSPC.Adjusted
## 2007-01-03          1417
## 2007-01-04          1418
## 2007-01-05          1410
## 2007-01-08          1413
tail(GSPC, 4)
##            GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume
## 2014-01-06      1832      1837     1824       1827   3.295e+09
## 2014-01-07      1829      1840     1829       1838   3.512e+09
## 2014-01-08      1838      1840     1831       1837   3.652e+09
## 2014-01-09      1839      1843     1830       1838   3.581e+09
##            GSPC.Adjusted
## 2014-01-06          1827
## 2014-01-07          1838
## 2014-01-08          1837
## 2014-01-09          1838
chartSeries(GSPC, subset = "2013", theme = "white")
S&P 500 for 2013

S&P 500 for 2013

We can also chart currencies e.g. the Rupee / US Dollar exchange rate.

INRUSD <- getSymbols("INR=X", src = "yahoo", auto.assign = FALSE)
dim(INRUSD)
## [1] 1805    6
head(INRUSD, 4)
##            INR=X.Open INR=X.High INR=X.Low INR=X.Close INR=X.Volume
## 2007-01-01      44.22      44.22     44.04       44.22            0
## 2007-01-02      44.21      44.22     44.08       44.12            0
## 2007-01-03      44.12      44.41     44.09       44.11            0
## 2007-01-04      44.12      44.48     44.10       44.10            0
##            INR=X.Adjusted
## 2007-01-01          44.22
## 2007-01-02          44.12
## 2007-01-03          44.11
## 2007-01-04          44.10
tail(INRUSD, 4)
##            INR=X.Open INR=X.High INR=X.Low INR=X.Close INR=X.Volume
## 2014-01-01      61.84      61.97     61.80       61.80            0
## 2014-01-02      61.84      62.41     61.74       61.84            0
## 2014-01-03      62.06      62.57     62.06       62.06            0
## 2014-01-06      62.23      62.45     61.94       62.23            0
##            INR=X.Adjusted
## 2014-01-01          61.80
## 2014-01-02          61.84
## 2014-01-03          62.06
## 2014-01-06          62.23
chartSeries(INRUSD, subset = "2013", theme = "white")
Rupee / US Dollar exchange rate for 2103

Rupee / US Dollar exchange rate for 2013

Bibliography

Frankau, Simon, Diomidis Spinellis, Nick Nassuphis, and Christoph Burgard. 2009. “Commercial Uses: Going Functional on Exotic Trades.” J. Funct. Program. 19 (1) (jan): 27–45. doi:10.1017/S0956796808007016. http://dx.doi.org/10.1017/S0956796808007016.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s