user113156
user113156

Reputation: 7127

Downloading data from Interactive Brokers TWS

I have been playing around with the Interactive Brokers TWS and R and I have been having varying success.

library(IBrokers)
IBConn <- twsConnect(port = xxxx)
currency_df = twsCurrency("NZD",currency = "USD")
test = reqHistoricalData(IBConn, Contract = currency_df, whatToShow ='BID_ASK', useRTH = "0", barSize = '1 min', duration="1 D", endDateTime = paste0(gsub("-","", reqCurrentTime(IBConn))," EST"))
plot(test$NZD.USD.Close)

library(quantmod)
plot(test$NZD.USD.Close)
chartSeries(test$NZD.USD.Close)
addBBands(n = 20, sd = 2, ma = "SMA", draw = 'bands', on = -1)

Which works well, I am able to download 1 minute currency data for that day.

The problem arises when I try to get stock data of a firm

tws = twsConnect(port=7497)
symbol = twsSTK("AAPL")
data_AAPL = reqHistoricalData(tws, symbol)
print (data_AAPL)

However I do not get the same result as this blog (reqHistoricalData Function - approx halfway down the page).

Other data that I have requested using the following code runs for hours and I am forced to click "stop" in the R console.

tws <- twsConnect()
aapl.csv <- file("AAPL.csv", open="w")

# run an infinite-loop ( <C-c> to break )
reqMktData(tws, twsSTK("AAPL"), 
           eventWrapper=eWrapper.MktData.CSV(1), 
           file=aapl.csv)

close(aapl.csv)
close(tws)

My question is, how can I download the previous days 1 or 5 minute data for AAPL stock (Open, High, Low, Close) using the Interactive Brokers R package? I can collect daily data using the quantmod package but I am wondering if I can collect minute data using the IBrokers package in the format of Open High Low and Close.

Note: I am using an Interactive Brokers demo trading account.

Upvotes: 1

Views: 3209

Answers (1)

hvollmeier
hvollmeier

Reputation: 2986

The reason you don’t get the same data as in the blog you mentioned is that you are referring to historical (i.e. reqHistoricalData(tws, symbol) data where the example in the blog is referring to market data ( i.e. reqMktData) which is streaming and quite different. To get historical intraday data for a stock (AAPL) you must define a contract like so:

contract = twsContract(0,"AAPL","STK","SMART","ISLAND", "","0.0","USD","","","",NULL,NULL,"0”)

example:

library(IBrokers)
IBConn <- twsConnect(port = 7497)
contract = twsContract(0,"AAPL","STK","SMART","ISLAND", "","0.0","USD","","","",NULL,NULL,"0")
data_AAPL = reqHistoricalData(IBConn, contract, whatToShow ='BID_ASK', useRTH = "0", barSize = '1 min', duration="1 D", endDateTime = paste0(gsub("-","", reqCurrentTime(IBConn))))


> head(data_AAPL)
                    AAPL.Open AAPL.High AAPL.Low AAPL.Close
2017-10-23 10:00:00    156.00    157.20   156.00     156.64
2017-10-23 10:01:00    155.99    156.65   155.95     156.64
2017-10-23 10:02:00    155.95    156.65   155.95     156.65
2017-10-23 10:03:00    155.95    156.65   155.95     156.65
2017-10-23 10:04:00    155.95    156.60   155.95     156.60
2017-10-23 10:05:00    155.95    156.62   155.95     156.60
                    AAPL.Volume AAPL.WAP AAPL.hasGaps AAPL.Count
2017-10-23 10:00:00          -1       -1            0         -1
2017-10-23 10:01:00          -1       -1            0         -1
2017-10-23 10:02:00          -1       -1            0         -1
2017-10-23 10:03:00          -1       -1            0         -1
2017-10-23 10:04:00          -1       -1            0         -1
2017-10-23 10:05:00          -1       -1            0         -1

Don’t forget to close the connection with twsDisconnect(IBConn) :-)

Upvotes: 5

Related Questions