# After hours data - Interactive Brokers

I just started using Interactive Brokers because of their API. I'm using the IBrokers package in R. I've managed to get data for S&P 500 and other indices but now I want data for S&P 500 after hours.
Investing.com shows data for S&P 500 after hours (S&P 500 futures). How can I get this data by using Interactive brokers API?

Update

I just tried this without success. I only get data for regular trading hours.

spx = reqHistoricalData(tws2, twsIndex(symbol = "SPX", exch = "CBOE"), barSize = "15 mins", duration = "1 M", useRTH = "0")

Am I doing something wrong?

• I would also suggest you to ask this in the [TWS API] discussion group: groups.io/g/twsapi There is a vibrant community there of coders working with several aspects of the Interactive Brokers API. – Option7even Feb 15 '19 at 14:30

The function reqHistoricalData has an argument useRTH ("use regular trading hours"). Set useRTH = "0" to get data outside those hours.

This can only work for the futures, not for the index, which is only computed during normal trading hours.

library("IBrokers")
tws <- twsConnect()

contract <- twsContract(local = "ESH9",
sectype = "FUT",
exch = "GLOBEX",
currency = "USD",
include_expired = "1",
conId = "", symbol = "", primary = "",
expiry = "", strike = "", right = "",
multiplier = "", combo_legs_desc = "",
comboleg = "", secIdType = "", secId = "")

reqHistoricalData(tws,
contract,
barSize = "15 mins",
duration = "1 M",
useRTH = "0")