简体   繁体   English

使用库“ib_insync”检索历史数据时,一根柱的时间段不正确

[英]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. reqHistoricalData的时间间隔不正确,无法每 5 秒检索一次数据"barSizeSetting='5 secs'"但是使用reqRealTimeBars检索数据时,可以每 5 秒检索一次数据。 Please feel free to comment how to fix the problem.请随时评论如何解决问题。

Test Cases测试用例

ib_insync initialisation ib_insync 初始化

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带有 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 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带有 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 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)
  1. Make sure the time is UTC or you are with similar time zones.确保时间是 UTC,或者您使用类似的时区。
  2. whatToShow='MIDPOINT' --> Change it to other options. whatToShow='MIDPOINT' --> 将其更改为其他选项。 Make it whatToShow='TRADES' or whatToShow='BID_ASK'使其成为 whatToShow='TRADES' 或 whatToShow='BID_ASK'
  3. useRTH = False使用 RTH = 假

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM