'When retrieve historical data with library "ib_insync", time period of one bar is incorrect

The time interval of reqHistoricalData is incorrect, it cannot retrieve data for every 5 seconds "barSizeSetting='5 secs'" but when retrieve data with reqRealTimeBars, data can be retrieved for every 5 seconds. Please feel free to comment how to fix the problem.

Test Cases

ib_insync initialisation

from ib_insync import *
import random
util.startLoop()  
 
random_id = random.randint(0, 9999)

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=random_id, timeout=0)

contract = Forex('EURUSD')

Bars with reqHistoricalData

bars = ib.reqHistoricalData(
        contract, 
        endDateTime='', 
        durationStr='1500 S', 
        barSizeSetting='5 secs',
        whatToShow='MIDPOINT', 
        useRTH=True,
        formatDate=1,
        keepUpToDate=True,
        )

def onBarUpdate(bars, hasNewBar):
        print(bars[-1])

# Set callback function for streaming bars
bars.updateEvent += onBarUpdate
    
ib.sleep(60)
ib.cancelHistoricalData(bars)

Result with reqHistoricalData

BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 5), open=1.10055, high=1.1006, low=1.10055, close=1.10055, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 10), open=1.10055, high=1.10055, low=1.10055, close=1.10055, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 10), open=1.10055, high=1.1006, low=1.10055, close=1.10055, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 15), open=1.10055, high=1.10055, low=1.10055, close=1.10055, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 15), open=1.10055, high=1.1006, low=1.10055, close=1.1006, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 20), open=1.1006, high=1.1006, low=1.1006, close=1.1006, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 20), open=1.1006, high=1.1007, low=1.10055, close=1.1007, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 25), open=1.1007, high=1.1007, low=1.1007, close=1.1007, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 25), open=1.1007, high=1.1007, low=1.10065, close=1.1007, volume=-1.0, average=-1.0, barCount=-1)
BarData(date=datetime.datetime(2022, 3, 29, 15, 50, 30), open=1.1007, high=1.1007, low=1.1007, close=1.1007, volume=-1.0, average=-1.0, barCount=-1)

Bars with reqRealTimeBars

bars = ib.reqRealTimeBars(contract, 5, 'MIDPOINT', False)

def onBarUpdate(bars, hasNewBar):
        print(bars[-1])

# Set callback function for streaming bars
bars.updateEvent += onBarUpdate
    
ib.sleep(60)
ib.cancelHistoricalData(bars)

Result with reqRealTimeBars

RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 5, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.100135, high=1.100195, low=1.100115, close=1.10017, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 10, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.10017, high=1.100185, low=1.10012, close=1.10015, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 15, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.10015, high=1.100165, low=1.10007, close=1.10007, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 20, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.10007, high=1.10011, low=1.10007, close=1.100105, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 25, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.100105, high=1.100105, low=1.10003, close=1.100055, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 30, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.100055, high=1.10006, low=1.100025, close=1.10003, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 35, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.10003, high=1.100075, low=1.10003, close=1.100055, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 40, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.100055, high=1.100075, low=1.100045, close=1.10005, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 45, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.10005, high=1.10006, low=1.100025, close=1.10006, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 50, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.10006, high=1.10008, low=1.10006, close=1.100075, volume=-1.0, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2022, 3, 29, 7, 52, 55, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.100075, high=1.10008, low=1.100035, close=1.100055, volume=-1.0, wap=-1.0, count=-1)


Solution 1:[1]

  1. Make sure the time is UTC or you are with similar time zones.
  2. whatToShow='MIDPOINT' --> Change it to other options. Make it whatToShow='TRADES' or whatToShow='BID_ASK'
  3. useRTH = False

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Farzad Amirjavid