Introducing IBrokers (and Jeff Ryan)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Josh had kindly invited me to post on FOSS Trading around the time when he first came up with the idea for the blog. Fast forward a year and I am finally taking him up on his offer.
For those who dislike Windows and C++, the community of IB users have a few “non-official” options. They include some nice implementations in C, Python (2), Matlab, and something even more abstracted in the trading-shim. While all well and good, there was one missing: R.
Many of you may know I am a rather large proponent of R. I have authored or coauthored quite a few packages and help to organize the R/Finance conference in Chicago each Spring. I am also a huge single-language solution kind of guy. If I could order food and surf the internet from R, all the world would be mine. But I digress…
The IBrokers package on CRAN is my contribution to the landscape. A pure R implementation of most of the API, using nothing but R and some magic. It is now possible to connect to a running TWS (aka Trader Workstation) and retrieve historical data, request market data feeds, and even place orders — all from R.
> install.packages(“IBrokers”)

> library(IBrokers)Loading required package: xtsLoading required package: zooIBrokers version 0.2-7: (alpha)Implementing API Version 9.62This software comes with NO WARRANTY. Not intended for production use!See ?IBrokers for details> tws <- twsConnect()> tws>
> aapl <- reqHistoricalData(tws, twsSTK("AAPL"))TWS Message: 2 -1 2104 Market data farm connection is OK:usfutureTWS Message: 2 -1 2104 Market data farm connection is OK:usfarmwaiting for TWS reply ……. done.
> twsSTK(“AAPL”)List of 14$ conId : num 0$ symbol : chr “AAPL”$ sectype : chr “STK”$ exch : chr “SMART”$ primary : chr “”$ expiry : chr “”$ strike : chr “0.0”$ currency : chr “USD”$ right : chr “”$ local : chr “”$ multiplier : chr “”$ combo_legs_desc: NULL$ comboleg : NULL$ include_expired: chr “0”
> str(aapl)An ‘xts’ object from 2010-04-14 to 2010-05-13 containing:Data: num [1:22, 1:8] 245 246 249 247 248 …– attr(*, “dimnames”)=List of 2..$ : NULL..$ : chr [1:8] “AAPL.Open” “AAPL.High” “AAPL.Low” “AAPL.Close” …Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicagoxts Attributes:List of 4$ from : chr “20100413 21:35:34”$ to : chr “20100513 21:35:34”$ src : chr “IB”$ updated: POSIXct[1:1], format: “2010-05-13 15:35:36.396084”
Next time we’ll explore the real-time data features of IBrokers, including live market data, real-time bars, and order-book data capabilities.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.