Search code examples
interactive-brokersibrokers

IBrokers R API and same day intraday prices


I think this is be an IB API more than the IBrokers R package. I am using reqHistoricalData to get 30 minutes intraday historical data. The market is open and I am not getting the same day's data. I only get yesterday's data.

Is it possible to get the same day intraday bar data? here is the code I am using, it only gives data for the previous day, not same day.

library(tidyverse)
library(IBrokers)
tws = twsConnect()
contract <- twsEquity('VOD','SMART')
VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endDateTime = "20210408 13:24:28", barSize = "1 min", duration = "1 D")
VOD_intraday %>% as.data.frame() %>% rownames_to_column(var = "time") %>% arrange(desc(time)) %>% head()

It's 13:27 GMT on 2021-04-08 and London is open. And here is the response - it only gives data from 2020-04-07:

> contract <- twsEquity('VOD','SMART')
> VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endDateTime = "20210408 13:24:28", barSize = "1 min", duration = "1 D")
waiting for TWS reply on VOD .... done.
> VOD_intraday %>% as.data.frame() %>% rownames_to_column(var = "time") %>% arrange(desc(time)) %>% head()
                 time VOD.Open VOD.High VOD.Low VOD.Close VOD.Volume VOD.WAP VOD.hasGaps VOD.Count
1 2021-04-07 20:59:00    18.96    18.98   18.95     18.98       1131  18.958           0       265
2 2021-04-07 20:58:00    18.96    18.96   18.95     18.96         90  18.957           0        42
3 2021-04-07 20:57:00    18.96    18.97   18.95     18.95        258  18.960           0        72
4 2021-04-07 20:56:00    18.96    18.96   18.95     18.95        124  18.959           0        58
5 2021-04-07 20:55:00    18.96    18.96   18.95     18.96         56  18.958           0        34
6 2021-04-07 20:54:00    18.95    18.96   18.95     18.95         26  18.951           0        12

Instead of VOD, you can use SPY, MSFT or any US security while the US market is open.

Edit: It turns out you need realtime subscription to get same day data. The answer below works.


Solution

  • One has to specify the ending time, or leave it blank to get the most recent data available.

    Try this:

     VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endTime = "", barSize = "1 min", duration = "1 D")
    

    Here's the execution when I run it:

    > library(tidyverse)
    > library(IBrokers)
    IBrokers version 0.9-10.  Implementing API Version 9.64
    
    IBrokers comes with NO WARRANTY.  Not intended for production use!
    
    
    See ?IBrokers for details.
    > tws = twsConnect()
    > contract <- twsEquity('SPY','SMART')
    > VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract,     endDateTime = "", barSize = "1 min", duration = "1 D")
    waiting for TWS reply on SPY ........... done.
    > head(VOD_intraday)
                        SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.WAP  SPY.hasGaps SPY.Count
    2021-04-08 07:30:00   407.93   407.98  407.68    407.80       5042 407.846           0      1709
    2021-04-08 07:31:00   407.81   408.00  407.74    407.98       1615 407.844           0      1065
    2021-04-08 07:32:00   407.99   408.05  407.81    407.90       2451 407.932           0      1560
    2021-04-08 07:33:00   407.89   407.98  407.88    407.95       2353 407.932           0      1300
    2021-04-08 07:34:00   407.95   407.97  407.81    407.81       1708 407.907           0      1012
    2021-04-08 07:35:00   407.82   407.86  407.61    407.67       2729 407.726           0      1458
    

    And for symbol VOD:

    > contract <- twsEquity('VOD','SMART')
    > VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endDateTime = "", barSize = "1 min", duration = "1 D")
    waiting for TWS reply on VOD .... done.
    > head(VOD_intraday)
                        VOD.Open VOD.High VOD.Low VOD.Close VOD.Volume VOD.WAP VOD.hasGaps VOD.Count
    2021-04-08 07:30:00    18.95    18.95   18.91     18.92        246  18.921           0        49
    2021-04-08 07:31:00    18.91    18.91   18.90     18.90         69  18.905           0        31
    2021-04-08 07:32:00    18.90    18.90   18.87     18.87        237  18.881           0        44
    2021-04-08 07:33:00    18.87    18.87   18.86     18.87         45  18.870           0        20
    2021-04-08 07:34:00    18.87    18.87   18.85     18.86        173  18.860           0        57
    2021-04-08 07:35:00    18.86    18.87   18.85     18.86         39  18.859           0        19