[R-SIG-Finance] Unable to get option historical data using IBrokers

Marc Delvaux mdelvaux at gmail.com
Sun Jun 13 00:30:47 CEST 2010


Poking around, I am now able to retrieve the correct historical data.
The breakthrough was to understand that I could use reqContractDetails
to get a list of valid Contracts and finally to understand the
limitation on the number of bars returned.   It also seems that you
can ask other barSize than "1 min" contrary to the remark in the help
file of reqHistoricalData.

Jeff, I cannot thank you enough to release such a wonderful package.

> tws <- twsConnect()
> nly_opts <- reqContractDetails(tws, twsOption(local="", right="P", symbol="NLY"))
2 -1 2104 Market data farm connection is OK:usopt
2 -1 2104 Market data farm connection is OK:usfarm
2 -1 2106 HMDS data farm connection is OK:ushmds2a
> nly_opt_1_hist <-  reqHistoricalData(conn = tws,
+                     Contract = nly_opts[[1]]$contract,
+                          barSize = "30 mins",
+                     duration = "20 D",
+                                       whatToShow = "BID")
TWS Message: 2 1 366 No historical data query found for ticker id:1
waiting for TWS reply ....... done.
> str(nly_opt_1_hist)
An ‘xts’ object from 2010-05-14 06:30:00 to 2010-06-11 12:30:00 containing:
  Data: num [1:260, 1:8] 0.25 0.29 0.3 0.31 0.31 0.32 0.32 0.32 0.3 0.31 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:8] "NLY   110122P00010000.Open" "NLY
110122P00010000.High" "NLY   110122P00010000.Low" "NLY
110122P00010000.Close" ...
 - attr(*, "from")= chr "20100523  23:27:49"
 - attr(*, "to")= chr "20100612  23:27:49"
 - attr(*, "src")= chr "IB"
 - attr(*, "updated")= POSIXct[1:1], format: "2010-06-12 15:27:52.081"
  Indexed by objects of class: [POSIXt,POSIXct] TZ:
  xts Attributes:
List of 4
 $ from   : chr "20100523  23:27:49"
 $ to     : chr "20100612  23:27:49"
 $ src    : chr "IB"
 $ updated: POSIXct[1:1], format: "2010-06-12 15:27:52.081"

Marc Delvaux

On Sat, Jun 12, 2010 at 10:49 AM, Marc Delvaux <mdelvaux at gmail.com> wrote:
> I am trying to use IBrokers to get historical data for options.  The
> exact problem is the inability to figure how to construct a valid
> contract or in general to construct a valid request for options
> historical data
>
> Details:
> -          Working under windows, version 2.10.1
> -          IBrokers reports version 0.2.7 (alpha)
> -          TWS reports Build 906.2, Jun 10, 2010 6:27:24 PM
> -          I am able to connect and download stock historical data
>
> I tried to use both the local symbol found using the TWS contact
> details and the contract ID, and playing with some function
> parameters, but to no avail at this time.  I guess I just need one
> small nudge to see what I do wrong, but currently I am poking in the
> dark and would appreciate any help.  Example session below.
>
> On a side topic, thank you Jeff for IBrokers, it is a tremendous
> simpler way to get data in R than trying to use directly the IB API.
>
> Regards
>
> Marc Delvaux
>
>
> R version 2.10.1 (2009-12-14)
> Copyright (C) 2009 The R Foundation for Statistical Computing
> ISBN 3-900051-07-0
>
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under certain conditions.
> Type 'license()' or 'licence()' for distribution details.
>
> R is a collaborative project with many contributors.
> Type 'contributors()' for more information and
> 'citation()' on how to cite R or R packages in publications.
>
> Type 'demo()' for some demos, 'help()' for on-line help, or
> 'help.start()' for an HTML browser interface to help.
> Type 'q()' to quit R.
>
> IBrokers version 0.2-7: (alpha)
> Implementing API Version 9.62
>
> This software comes with NO WARRANTY.  Not intended for production use!
> See ?IBrokers for details
> [Previously saved workspace restored]
>
>> R.version
>               _
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status
> major          2
> minor          10.1
> year           2009
> month          12
> day            14
> svn rev        50720
> language       R
> version.string R version 2.10.1 (2009-12-14)
>> library(IBrokers)
> Loading required package: xts
> Loading required package: zoo
>> tws <- twsConnect()
>> aapl <- reqHistoricalData(tws,twsSTK("AAPL"))
> TWS Message: 2 -1 2104 Market data farm connection is OK:usopt
> TWS Message: 2 -1 2104 Market data farm connection is OK:usfarm
> TWS Message: 2 -1 2106 HMDS data farm connection is OK:ushmds2a
> waiting for TWS reply ..... done.
>> str(aapl)
> An ‘xts’ object from 2010-05-13 to 2010-06-11 containing:
>  Data: num [1:21, 1:8] 263 255 255 257 250 ...
>  - attr(*, "dimnames")=List of 2
>  ..$ : NULL
>  ..$ : chr [1:8] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
>  - attr(*, "from")= chr "20100512  18:43:14"
>  - attr(*, "to")= chr "20100612  18:43:14"
>  - attr(*, "src")= chr "IB"
>  - attr(*, "updated")= POSIXct[1:1], format: "2010-06-12 10:43:15.887"
>  Indexed by objects of class: [POSIXt,POSIXct] TZ:
>  xts Attributes:
> List of 4
>  $ from   : chr "20100512  18:43:14"
>  $ to     : chr "20100612  18:43:14"
>  $ src    : chr "IB"
>  $ updated: POSIXct[1:1], format: "2010-06-12 10:43:15.887"
>> nly_put <- reqHistoricalData(tws,twsOPT("NLY 100717P00017000"))
> TWS Message: 2 1 366 No historical data query found for ticker id:1
> waiting for TWS reply .....
> Error: Unable to complete historical data request
> In addition: Warning message:
> In errorHandler(con, verbose, OK = c(165, 300, 366, 2104, 2106,  :
>  No security definition has been found for the request.
>>
>



More information about the R-SIG-Finance mailing list