erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.81k stars 753 forks source link

Request historical trades with keepUpToDate #653

Closed Roh-codeur closed 8 months ago

Roh-codeur commented 11 months ago

Hi

thanks for all your work on this.

I am using the below code to subscribe to historical Trades and keepThemUpToDate. I see historical trades, I don't however, see any updates to that. I have tried both barUpdateEvent and updateEvent. Do you know whereI could have gone wrong please? or is this not supported? also, I am using Paper account. I did, however, tried placing orders which got filled. so, presumably, that should have triggered a response.

    contract = Stock("AAPL", "SMART", "USD")
    def onBarUpdate(bars, hasNewBar):
        if hasNewBar:
            print(util.df(bars))
        else:
            print("No new bars")
    ib.positions()
    bars = ib.reqHistoricalData(
        contract,
        endDateTime='',
        durationStr='1 D',
        barSizeSetting='5 secs',
        whatToShow='TRADES',
        useRTH=False,
        keepUpToDate=True,
        formatDate=1)

    print(util.df(bars))
    # ib.barUpdateEvent += onBarUpdate
    ib.barUpdateEvent += onBarUpdate
    bars.updateEvent += onBarUpdate

thanks Roh

Roh-codeur commented 11 months ago

Edit: removed previous comment.

The issue I am facing is that req historical data call with keep up to date doesn’t do a callback. I can confirm that if I call this method repeatedly, I see updated data. I am wondering what I have got wrong in the callback. I do indeed use the ib.run() in the end. My other callbacks work fine

cgntiger commented 9 months ago
def onBarUpdate(bars, hasNewBar):
    if hasNewBar:
        print(bars)
    else:
        print("No new bars")

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

ib.barUpdateEvent += onBarUpdate
ib.run()

durationStr你设置一天,我怀疑是数据量太大,盈透不给了,或者处理不了五秒获取一天的数据 我记得获取数据有限制来着

cgntiger commented 9 months ago

获取数据的限制:https://interactivebrokers.github.io/tws-api/historical_limitations.html durationStr=1D,barSizeSetting的范围要在1min~1D

cgntiger commented 9 months ago

image

Roh-codeur commented 9 months ago

@cgntiger : you, my friend, are a Star! thanks for this. I will try out with the 1min~1day.

thanks a lot!

erdewit commented 8 months ago

The proper data subscriptions are needed, or else delayed data can be used with ib.reqMarketDataType(3).