erdewit / ib_insync

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

No contract information in barUpdateEvent #290

Closed xiangpeng2008 closed 4 years ago

xiangpeng2008 commented 4 years ago

Hi,

Is there anyway to know the contract for reqRealTimeBars ? barUpdateEvent won't give.

Versions:

python: 3.6.8
ib_insync: 0.9.61
IB: GateWay 978.2e
System: Centos 8

To produce:

from ib_insync import IB,util,Forex,Stock,ContFuture,Order,Index,LimitOrder
contracts = [Forex(pair) for pair in ('EURUSD', 'AUDUSD')]
ib = IB()
ib.connect('127.0.0.1', 6665, clientId=14)
ib.qualifyContracts(*contracts)
for contract in contracts:
    ib.reqRealTimeBars(contract, 5, 'MIDPOINT', True)

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

ib.barUpdateEvent+=real5bar2q
ib.sleep(20)
ib.disconnect()
exit()

Output:

Python 3.6.8 (default, Apr 16 2020, 01:36:27)
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ib_insync import IB,util,Forex,Stock,ContFuture,Order,Index,LimitOrder
>>> contracts = [Forex(pair) for pair in ('EURUSD', 'AUDUSD')]
>>> ib = IB()
>>> ib.connect('127.0.0.1', 6665, clientId=14)
<IB connected to 127.0.0.1:6665 clientId=14>
>>> ib.qualifyContracts(*contracts)
[Forex('EURUSD', conId=12087792, exchange='IDEALPRO', localSymbol='EUR.USD', tradingClass='EUR.USD'), Forex('AUDUSD', conId=14433401, exchange='IDEALPRO', localSymbol='AUD.USD', tradingClass='AUD.USD')]
>>> for contract in contracts:
...     ib.reqRealTimeBars(contract, 5, 'MIDPOINT', True)
...
[]
[]
>>> def real5bar2q(bars,hasNewBar):
...     print(bars[-1])
...
>>> ib.barUpdateEvent+=real5bar2q
>>> ib.sleep(20)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 26, 45, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.187295, high=1.187295, low=1.187265, close=1.187275, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 26, 45, tzinfo=datetime.timezone.utc), endTime=-1, open_=0.72167, high=0.72167, low=0.72167, close=0.72167, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 26, 50, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.187275, high=1.187275, low=1.187235, close=1.187255, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 26, 50, tzinfo=datetime.timezone.utc), endTime=-1, open_=0.72167, high=0.72169, low=0.721665, close=0.72169, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 26, 55, tzinfo=datetime.timezone.utc), endTime=-1, open_=0.72169, high=0.72173, low=0.72169, close=0.72172, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 26, 55, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.187255, high=1.187335, low=1.187255, close=1.187295, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 27, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.187295, high=1.187295, low=1.18729, close=1.187295, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 27, tzinfo=datetime.timezone.utc), endTime=-1, open_=0.72172, high=0.72172, low=0.72171, close=0.721715, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 27, 5, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.187295, high=1.187295, low=1.18729, close=1.187295, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 27, 5, tzinfo=datetime.timezone.utc), endTime=-1, open_=0.721715, high=0.721715, low=0.721715, close=0.721715, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 27, 10, tzinfo=datetime.timezone.utc), endTime=-1, open_=1.187295, high=1.187355, low=1.18729, close=1.187345, volume=-1, wap=-1.0, count=-1)
RealTimeBar(time=datetime.datetime(2020, 8, 17, 14, 27, 10, tzinfo=datetime.timezone.utc), endTime=-1, open_=0.721715, high=0.72172, low=0.72168, close=0.72172, volume=-1, wap=-1.0, count=-1)
True
>>> ib.disconnect()
>>> exit()

Screenshot 2020-08-17 at 10 29 11 PM

We see there's no specific information to tell which RealTimeBar is for EURUSD or AUDUSD, unless we look at the price value, which is however not a very systematic way.

Thanks a lot !

Xiangpeng

xiangpeng2008 commented 4 years ago

This is already discussed here: https://groups.io/g/insync/topic/multiple_symbols_onbarupdate/23325192?p=,,,20,0,0,0::recentpostdate%2Fsticky,,,20,2,0,23325192

BarDataList, it is more than simply a list of bars, it also contains the parameters of the original call to reqHistoricalData, including contract (emphasis added below)