Say I am interested in two stocks, AAPL and TSLA. I want my algorithm to trade whenever the difference between the two stocks reach some number. How do I do this with ib_insync?
I somehow can’t figure out how to do it. The general idea appears to be (in pseudocode):
for contract in contracts: #(where contracts is a list of ticker symbols I'm interested in)
bars = ib.reqHistoricalData(contract, endDateTime = '', keepUpToDate = True)
bars.updateEvent += my_function()
However this leads to updateEvent triggering after only one set of data is received. How can I get it to wait until both sets of data are available?
It feels like this is of course not going to work because bars
is getting updated twice, so updateEvent should trigger twice – but I can’t figure out how else to do it. The best option should be putting both contracts into one ib.reqHistoricalData call, but that does not seem to be possible.