Search code examples
pythonpython-asynciointeractive-brokers

What does += mean in the context of event handling?


In Scanner data (streaming) I can find the following statement (you can find below the full code):

scanData.updateEvent += onScanData

What does the += mean?

So, I understand that onScanData() is the event handler function that should be called when the scanData.updateEvent is fired.

But why do I have a += here and not a simple =? What get's incremented and where can I find the incremented variable later on?

Remark: to get the code snippet below running, the Interactive-Brokers software TWS has be running.

import datetime
from ib_insync import *

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)


def onScanData(scanData):
    print(scanData[0])
    print(len(scanData))

sub = ScannerSubscription(
    instrument='FUT.US',
    locationCode='FUT.GLOBEX',
    scanCode='TOP_PERC_GAIN')
scanData = ib.reqScannerSubscription(sub)
scanData.updateEvent += onScanData
ib.sleep(60)
ib.cancelScannerSubscription(scanData)

Solution

  • reqScannerSubscription returns an instance of ScanDataList. Its updateEvent property is an instance of eventkit.Event. It overloads its __iadd__ method to be an alias for connect, which connects a listener to this event.