Skip to content

Interactive Brokers

4 messages · Guy Yollin, Noah Silverman, Brian G. Peterson

#
Hi,

I'm just getting started on coding a script that will use the IBrokers package.  

I've worked through creating my own instance of the eWrapper list and understand how to create my own functions to handle incoming data.

There are a few variables, "msg", "timestamp", etc. that appear to contain several pieces of data.  (Perhaps they are lists or vectors?)  I can't find any documentation as to the details of these variables.  Subsequently, I don't know how to extract the desired information from them.

Is there more detailed documentation of this somewhere?

Thanks!

--
Noah Silverman
UCLA Department of Statistics
8117 Math Sciences Building
Los Angeles, CA 90095
#
Hi Noah,

Of course Jeff Ryan is the ultimate authority on this subject and a 
number of his IBrokers presentations are kicking around the web.

Nevertheless, here's a short script that I put together a while back to 
help me decipher the IBrokers messages.  The script logs messages to a 
file; after a while, hit the escape key and the log file will be read 
and parsed a little bit.

To go further, you can cross-reference some of this stuff with the IB 
API docs.

Hope this helps.

Best,

G


# initialize log file
fn <- "IBlog.dat"
if( file.exists(fn) )
   file.remove(fn)

# log messages from IB
library(IBrokers)
tws <- twsConnect()
equity1 <- twsEquity("SPY")
reqMktData(tws, equity1, CALLBACK = NULL, file = fn)
# hit the escape key after you've logged for a while
twsDisconnect(tws)

# read log file and make an xts object
x <- read.table(file=fn,header=F,sep=" ",as.is=T,fill=T,strip.white = T)
timeStamp.raw <- strptime(paste(x[,1],x[,2]), "%Y%m%d %H:%M:%OS")
num.mat <- apply(x[!is.na(timeStamp.raw),-(1:2)],2,as.numeric)
timeStamp <- timeStamp.raw[!is.na(timeStamp.raw)]
IB.xts.all <- xts(num.mat,timeStamp)
IB.xts <- 
xts(cbind(ID=num.mat[,3],MSG=num.mat[,1],tickType=num.mat[,4],parm1=num.mat[,5],parm2=num.mat[,6]),timeStamp)
#
# .twsIncomingMSG
#  TICK_PRICE =   "1"
#  TICK_SIZE  =   "2"
#  ORDER_STATUS = "3"
#  ERR_MSG =      "4"
#  TICK_GENERIC = "45"
#  TICK_STRING =  "46"
#
# .twsTickType
#  BID_SIZE              0
#  BID                   1
#  ASK                   2
#  ASK_SIZE              3
#  LAST                  4
#  LAST_SIZE             5
#  HIGH                  6
#  LOW                   7
#  VOLUME                8
#  CLOSE                 9
#  OPEN                  14
#  OPTION_HISTORICAL_VOL 23
#  LAST_TIMESTAMP        45

# Trade messages
# MSG=46,tickType=45 LAST_TIMESTAMP
# MSG=2,tickType=8 VOLUME
# MSG=2,tickType=5 LAST_SIZE
# MSG=1,tickType=4 LAST

# trade related messages
trades.idx <- IB.xts[,"tickType"]==4 | IB.xts[,"tickType"]==5 | 
IB.xts[,"tickType"]==8 | IB.xts[,"tickType"]==45
trades <- IB.xts[trades.idx,]

# quote related messages
ba.idx <- IB.xts[,"tickType"]==0 | IB.xts[,"tickType"]==1 | 
IB.xts[,"tickType"]==2 | IB.xts[,"tickType"]==3
quotes <- IB.xts[ba.idx,]

# plot sequence of trade related messages
lastPrice <- IB.xts[IB.xts[,"tickType"]==4,]
lastSize <- IB.xts[IB.xts[,"tickType"]==5,]
Volume <- IB.xts[IB.xts[,"tickType"]==8,]
lastTimeStamp <- IB.xts[IB.xts[,"tickType"]==45,]

#pdf(width=8,height=4,file="IBmess.pdf")
par(lab=c(5,1,7),yaxt="n")
plot(lastPrice[,1],type="n",pch=18,col=2,ylim=c(0,1),yaxt="n",main="")
title("IBroker Trade-related Messages")
text(x=start(lastPrice)-0,y=seq(0.2,0.8,by=0.2),c("last","size","stamp","volume"),pos=2,xpd=NA)
points(x=index(lastPrice),y=rep(0.2,nrow(lastPrice)),pch=18,col=2)
points(x=index(lastSize),y=rep(0.4,nrow(lastSize)),pch=18,col=3)
points(x=index(lastTimeStamp),rep(0.6,nrow(lastTimeStamp)),pch=18,col=4)
points(x=index(Volume),y=rep(0.8,nrow(Volume)),type="p",pch=18,col=6)
#dev.off()
On 7/14/2011 10:12 AM, Noah Silverman wrote:
#
Guy,

That's a big help.  Thanks!

I also started digging into the source code for the R library.  Much of the information is buried in there.

--
Noah Silverman
UCLA Department of Statistics
8117 Math Sciences Building
Los Angeles, CA 90095
On Jul 14, 2011, at 11:23 AM, Guy Yollin wrote:

            
#
On Thu, 2011-07-14 at 11:23 -0700, Guy Yollin wrote:
I'll underline this statement.  The IBrokers package is 'just' an R
wrapper for the IB API.  Things like valid message types, contents of
messages, data formats, etc. have been wrapped for R, but the *values*
are those defined by IB's API.

Cheers,

   - Brian