[R-SIG-Finance] re[R-sig-finance] trieving option info from IB

Wind2 windspeedo99 at gmail.com
Wed Jun 10 15:42:06 CEST 2009

Jeff's IBrokers's great.  
Finnally I have been able to retreive the snapshot data from IB for futures
and future options.   Here is the code in case if anyone is interested in.


>tws <- twsConnect() # make a new connection to the TWS
>reqCurrentTime(tws) # check the server's timestamp

>myWrapper <- eWrapper(symbols=sapply(futures,function(x)
>reqMktData.snapshot(tws, futures,eventWrapper=myWrapper)

I modified the reqMktData and twsCALLBACK a liittle. The code is in the
myIBrokersFun.r.   A parameter of  numId=length(Contract) has been
transferred to twsCALLBACK by reqMkeData.  So twsCALLBACK could terminate
itself once there has been numId times occurence of

It works well for futures.

As for the future option, there is no curMsg==46, so twsCALLBACK would be
terminated because of an error.  That's OK for my task. :)

I guess the problem is that IB does not send an end message for the snapshot
mode.    Maybe IB thinks that the number of messages sent is fixed for each
kind of symbols.   But I am not familiar with IB, so I'd rather wait for the
no message error to terminate the data retrieving progress.

Now I just wonder how people utilize the default output of reqMktData.   The
output seems customized for read by eyes but not for being readed by R and
transferred into matrix or data.frame.          
But if use the eWrapper to modify the output format,  I could not figure out
how to show the symbols in the output.

Any suggestion for reading the output of reqMktData into R data would be

Wind2 wrote:
> I have figured out how to get market data for future options with
> IBrokers.
> strike="",currency="USD",right="",local="",multiplier="100",NULL,NULL,"0"))
> [1] 1480
>>temp<-lapply(oc,function(x){if ((x$contract$strike=="1000.0") &
(substr(x$contract$expiry,1,6)=="200909")) x else NULL  })
>>temp1<-temp[!sapply(temp, is.null)]
>>reqMktData(tws, temp2$contract)
> <20090605 01:39:56.089000> id=1 symbol= Volume: 41
> <20090605 01:39:56.091000> id=1 symbol= highPrice: 55.5
> <20090605 01:39:56.093000> id=1 symbol= lowPrice: 51.4
> <20090605 01:39:56.094000> id=1 symbol= bidOption: 0.2925249666770512
> 0.49009769315987844
> <20090605 01:39:56.097000> id=1 symbol= askOption: 0.30040373020930494
> 0.49204912017993013
> <20090605 01:39:56.098000> id=1 symbol= lastOption: 0.2930042176111911
> 0.49021807490565755
> <20090605 01:39:56.606000> id=1 symbol= bidOption: 0.2925249666770512 -2
> <20090605 01:39:56.607000> id=1 symbol= askOption: 0.30040373020930494 -2
> <20090605 01:39:56.610000> id=1 symbol= lastOption: 0.2930042176111911 -2
> I guess my codes could be optimized furthur.
> Now there is only one issue to be settled: how to get only the current
> bid, ask and size for one time instead of the continuous data feed.
> On Thu, Jun 4, 2009 at 10:28 PM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
>> Hi Wind,
>> Most of IBrokers behaves 1:1 with the "official" API from Interactive
>> Brokers.  That is meant to provide a smaller learning curve when
>> coming off the official API, but at the cost of being forced to deal
>> with some of the quirks of the platform as well.
>> That said, it is *not* a simple wrapper to the API though.  Many
>> design decisions were made to make it more productive for an R user,
>> as well as more productive from a trading perspective than the
>> standard API.  The eWrapper structure and using R in general makes it
>> more productive than all the other APIs in my opinion.
>> With respect to the contract information, that is really hit or miss.
>> I'll look into creating a 'best practices' or FAQ to help pool our
>> collective wisdom --- maybe a simple wiki would be a good start.
>> For getting snapshot information, IB's interface (API) is lacking.  I
>> have that on a list of things to try and put together from what the
>> API *can* do (it can't do what you want per se), and will be looking
>> to incorporate that into a future release.
>> The newest quantmod has a function called "getOptionChain" which pulls
>> from Yahoo.  Obviously yahoo data caveats apply, but it is a decent
>> start to getting snapshots.
>> More documentation for IBrokers and quantmod is coming, as well as a
>> presentation at the upcoming Rmetrics conference in Switzerland.  For
>> those not signed up yet, take a look at www.rmetrics.org for more
>> information.
>> HTH,
>> Jeff
>> On Thu, Jun 4, 2009 at 9:10 AM, Wind <windspeedo99 at gmail.com> wrote:
>>> I have successfully retrieved price info from IB via IBrokers for stocks
>>> and
>>> simple options.  The package is very efficient.Yet I could not get info
>>> on
>>> future option.
>>>> oc<-reqContractDetails(tws, twsOption(local="",
>>>> right="",symbol="QQQQ"))
>>>> length(oc)
>>> [1] 1018
>>>> oc<-reqContractDetails(tws, twsOption(local="",right="",symbol="GC"))
>>> Error in reqContractDetails(tws, twsOption(local = "", right = "",
>>> symbol =
>>> "GC")) :
>>>  Unable to complete ContractDetails request
>>>> oc<-reqContractDetails(tws,
>>> twsOption(local="",right="",exch="NYMEX",symbol="GC"))
>>> Error in reqContractDetails(tws, twsOption(local = "", right = "", exch
>>> =
>>> "NYMEX",  :
>>>  Unable to complete ContractDetails request
>>> By the way, how could we retrieving the option chain,  just the current
>>> bid,
>>> ask, and size.  A snap shot only.
>>>  I have only find functions for data feed and historical data download.
>>> Any suggestion would be appreciated.
>>> Wind
>>>        [[alternative HTML version deleted]]
>>> _______________________________________________
>>> R-SIG-Finance at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>> -- Subscriber-posting only.
>>> -- If you want to post, subscribe first.
>> --
>> Jeffrey Ryan
>> jeffrey.ryan at insightalgo.com
>> ia: insight algorithmics
>> www.insightalgo.com
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.

View this message in context: http://www.nabble.com/retrieving-option-info-from-IB-tp23870812p23962592.html
Sent from the Rmetrics mailing list archive at Nabble.com.

More information about the R-SIG-Finance mailing list