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",
timeFormat = "1",
tzone = "",
verbose = TRUE,
tickerId = "1",
eventHistoricalData,
file)
reqHistory(conn, Contract, barSize, ...)
Arguments
conn |
a |
Contract |
a |
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 |
timeFormat |
POSIX style or seconds from 1970-01-01 |
tzone |
time zone of the resulting intraday series (if applicable) |
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
values are
‘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’.
Partial matching is attempted, but it is best to specify the barSize
value exactly as they are given above. 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 u’ where ‘n’ is an integer and ‘u’ is one of: ‘S’ (seconds), ‘D’ (days), ‘W’ (weeks), ‘M’ (months), or ‘Y’ (year). For example, ‘1 W’ would return one week of data. 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
The rules for historical data requests are somewhat vague. Not all symbols have data, and those that do may only be available with specific combinations of barSize and duration arguments. At present the only way to know is to try the combination in question.
There is a strictly enforced 10 seconds between request pacing rule implemented by the TWS. Keep this in mind. IBrokers currently does not manage this for the user via reqHistoricalData, though reqHistory does.
Author(s)
Jeffrey A. Ryan
References
Interactive Brokers https://www.interactivebrokers.com
See Also
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
Sys.sleep(10) # mandatory 10s between request to avoid IB pacing violation
reqHistory(tws, Contract=contract)
## End(Not run)