reqTBBOhistory: Download and save to disk historical Bid, Ask, and Trades...

Description Usage Arguments Details Value Note Author(s) References See Also Examples

View source: R/reqTBBOhistory.R

Description

This will make several requests for historical data from IBrokers repecting the historical data request limitations.

Usage

1
2
3
reqTBBOhistory(Symbols, base_dir = "/mnt/W", ndays = 95, endDateTime, tws,
  barSize = "1 min", useRTH = "1", env = .GlobalEnv, save = FALSE,
  chronological = FALSE)

Arguments

Symbols

names of instruments for which to request data

base_dir

base_dir that contains sub-directories ‘BID’, ‘ASK’, ‘TRADES’, and ‘BAT’

ndays

total number of days to retrieve. Default is 95. Max is 365

endDateTime

‘YYYYMMDD H:M:S’

tws

twsConnection object (Not yet implemented)

barSize

barSize to retrieve. see reqHistoricalData

useRTH

Use regular trading hours? 1 for TRUE, 0 for FALSE

env

where to put the final BATVM or BAM xts object

save

logical. Save to disk the BATVM or BAM object?

chronological

should the requests be made in chronological order. Default is FALSE because, at least for futures and options, requests for old data are more likely to fail than requests for recent data. For stocks, it might make sense to use TRUE so that if a request is interrupted, you will not have gaps in your data. update.data uses chronological=TRUE

Details

Experimental code – see Note section

Important: You should have a base directory that contains these sub-directories: ‘BID’, ‘ASK’, ‘TRADES’, and ‘BAT’.

The default base_dir is “/mnt/W”. If you do not have a directory there, you should either create it, or not use the default value for base_dir.

reqTBBOhistory will make requests for 5 days of bid, ask, and trade data and then pause for 30 seconds. In between requests, all BID, ASK, and TRADES data are saved into the respective subdirectories. Data are saved in rda files named with the format YYYY.MM.DD.Symbol.rda. After all requests have been made (approx. 37 minutes), the data are re-read and merged together into BATMV format and (if save=TRUE) stored in the BAT directory

IBrokers does not disseminate TRADES data for FX (CASH). Therefore, reqTBBOhistory will make 2 requests every 20 seconds (BID and ASK data) instead of 3 requests every 30 seconds.

For best results, you should define your instruments before calling this function.

This has only been tested on a debian-based linux system.

IB limits historical data requests to 6 every 60 seconds

Value

called for side-effect. Returns the names of Symbols.

Note

Warning: Interactive Brokers *back adjusts* their data for stock splits (but not for dividends). If you are storing stock data, you should probably unadjust the last year's worth of data, then, make sure you download the most recent data each day, being careful not to overwrite any data already stored on disk. This way you could have all unadjusted data that you could adjust (with qmao:::adjustIntraday.yahoo, for example). Otherwise, you'll have some data that is split-adjusted (or partially split-adjusted) and some that isn't.

Author(s)

Garrett See

References

InteractiveBrokers www.interactivebrokers.com

IB API http://interactivebrokers.com/php/apiUsersGuide/apiguide.htm

IB Historic Data Limitations http://individuals.interactivebrokers.com/php/apiUsersGuide/apiguide/api/historical_data_limitations.htm#XREF_93621_Historical_Data

See Also

twsInstrument:::update.data (unexported due to possibility of name change, and current lack of documentation),

getBAT, makeBATs, reqHistoricalData, reqHistory

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## Not run: 
library(twsInstrument)
dir.create("tmpdata/BID", recursive=TRUE)
dir.create("tmpdata/ASK")
dir.create("tmpdata/TRADES")
dir.create("tmpdata/BAT")
define_stocks("SPY")
# get the last 20 days of minutely data
# (this will take ~2 minutes)
reqTBBOhistory("SPY",
                base_dir="tmpdata",
                ndays=20,
                save=TRUE,
                chronological=TRUE)
# Now to prove it's on disk, delete from workspace
# and load it.
rm("SPY")
getSymbols("SPY", src='FI', dir='tmpdata/BAT',
            from=Sys.Date() - 20, verbose=TRUE)

# If you had called reqTBBOhistory with save=FALSE (the default)
# or if your "BAT" directory were wiped out, you can use
# the unexported makeBATs function to get the BID, ASK, TRADES
# data from disk and rebuild the BAT data.
unlink("tmpdata/BAT")
twsInstrument:::makeBATs("SPY", base_dir='tmpdata', ndays=20)
rm("SPY")
getSymbols("SPY", src='FI', dir='tmpdata/BAT',
            from=Sys.Date() - 20, verbose=TRUE)

# Clean up -- Delete everything
unlink("tmpdata", recursive=TRUE)

## End(Not run)

twsInstrument documentation built on May 31, 2017, 3:25 a.m.