reqHistoricalData {IBrokers}R Documentation

Request Historical Data From TWS

Description

Makes a request to the Interactive Brokers Trader Workstation (TWS), and returns an xts object containing the results of the request if successful.

Usage

reqHistoricalData(conn,
                  Contract,
                  endDateTime,
                  barSize = "1 day",
                  duration = "1 M",
                  useRTH = "1",
                  whatToShow = "TRADES", 
                  time.format = "1",
                  verbose = TRUE,
                  tickerId = "1",
                  eventHistoricalData,
                  file)

reqHistory(conn, Contract, barSize, ...)

Arguments

conn a twsConnection object
Contract a twsContract
endDateTime end date/time for request. See details.
barSize bar size to retrieve
duration time span the request will cover
useRTH limited to regular trading hours
whatToShow type of data to be extracted
time.format POSIX style or seconds from 1970-01-01
verbose should progress be documented
tickerId a unique id to associte with the request
eventHistoricalData callback function to process data
file file to write data to
... args to pass to reqHistoricalData

Details

The reqHistory function is a simple wrapper to request maximal history from IB. It is meant to be used directlty, or as a template for new wrappers.

All arguments should be character strings. Attempts will be made to coerce, but should not be relied upon.

The endDateTime argument must be of the form 'CCYYMMDD HH:MM:SS TZ'. If not specified the current time as returned from the TWS server will be used. This is the preferred method for backfilling data. The ‘TZ’ portion of the string is optional.

Legal barSize settings are technically ‘1 secs’,‘5 secs’,‘15 secs’, ‘30 mins’,‘1 min’,‘2 mins’, ‘3 mins’,‘5 mins’,‘15 mins’, ‘30 mins’,‘1 hour’,‘1 day’, ‘1 week’,‘1 month’,‘3 months’, and ‘1 year’. They must be specified exactly and there is no guarantee from the API that all will work for all securities or durations.

The duration string must be of the form ‘n S’ where the last character may be any one of ‘S’ (seconds), ‘D’ (days), ‘W’ (weeks), ‘M’ (months), and ‘Y’ (year). At present the limit for years is 1.

useRTH takes either ‘1’ or ‘0’, indicating the request to return only regular trade hour data, or all data, respectively.

whatToShow can be any one of the following, though depending on the overall request it may not succeed. ‘TRADES’, ‘MIDPOINT’, ‘BID’, ‘ASK’, ‘BID/ASK’.

time.format should simply be left alone. :D

eventHistoricalData accepts a user function to process the raw data returned by the TWS. This consists of a character vector that includes the first five elements of header information, with the fifth element specifying the number of rows in the results set. Passing NULL to eventHistoricalData will return the raw character vector. If nothing is specified, an xts object is returned.

The eventHistoricalData function, if any, is called after all data has been received by the client.

The file argument calls write.table to produce output suitable to reading in by read.csv. The file argument is passed to the write.table call, and if an empty string will return the output to the console.

The hasGaps column is converted automatically from (true,false) to 1 or 0, respectively.

Value

Returns an xts object containing the requested data, along with additional information stored in the objects xtsAttributes, unless callback or file is specified.

Note

As this code is under active development, the interface has changed again. The requests now require an valid twsConnection object as the first argument. The most recent release used an internal connection. When this caught an error, it became difficult to make further requests. The previous change was only made to accomodate a bug in R that has since been worked around by this author.

Historical data via the TWS API is at best unreliable. As historical data is not a priority of the IB API, outside of regular trading hours it may become unresponsive or unavailable. The only current way to handle this is to generally to disconnect from the TWS via twsDisconnect and then reconnect with twsConnect.

There is a strictly enforced 6 seconds between request pacing rule implemented by the TWS. Keep this in mind. IBrokers currently does not manage this for the user.

The TWS workstation must be running on the same machine as R.

The TWS must also be set up to allow for the API to be connected to. See the associated help on the main Interactive Brokers website.

The underlying API is subject to changes on an ongoing basis by Interactive Brokers. All attempts have been made to keep this current. If something should fail, please contact the maintainer of this package.

Author(s)

Jeffrey A. Ryan

References

Interactive Brokers www.interactivebrokers.com

See Also

twsContract, twsConnect

Examples

## Not run: 
tws <- twsConnect()
contract <- twsEquity('QQQQ','SMART','ISLAND')

# by default retreives 30 days of daily data
reqHistoricalData(tws, Contract=contract)

# by default retreives a year of 1 minute bars
reqHistory(tws, Contract=contract)

## End(Not run)

[Package IBrokers version 0.2-2 Index]