Makes a request to the Interactive Brokers Trader Workstation (TWS), and returns an xts object containing the results of the request if successful.
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, ...)
Returns an xts
object containing the requested data, along
with additional information stored in the objects xtsAttributes
,
unless callback
or file
is specified.
a twsConnection
object
a twsContract
end date/time for request. See details.
bar size to retrieve
time span the request will cover
limited to regular trading hours
type of data to be extracted
POSIX style or seconds from 1970-01-01
time zone of the resulting intraday series (if applicable)
should progress be documented
a unique id to associte with the request
callback function to process data
file to write data to
args to pass to reqHistoricalData
Jeffrey A. Ryan
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.
Interactive Brokers https://www.interactivebrokers.com
twsContract
, twsConnect
if (FALSE) {
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)
}
Run the code above in your browser using DataLab