Thanks. Forgot to mention - I'm using PyCharm.
The funny thing it that the code breaks only when there is no single completed transaction (i.e. no self.buy() and self.sell() in the strategy). Adding even a single transaction that is getting executed 'cures' the problem. This is a 'poor man's workaround' I'm now using....
afaik this is coming from IB, because of the nature of stoptraillimit orders. to place a valid stop-trail-limit order you will need the "limit price" at which you want to trail to start or an offset which is then used to calculate the "limit price". without either of these its not a valid stl order.
but the error could also mean you are specifying both limit price and limit price offset value, based on the above code you are only specifying plimit and not offset, so its coming from somewhere else. I saw one thread on ib_insync where this happened because TWS has a default offset is set in Order Presets configured. maybe you can check and confirm.
Could be the same behavior as the one reported in the following post:
It seems the IB store was designed with live trading in mind. The capability of loading the historical data was added as a mean for backfilling the historical data during live trading . So in this regard it less suitable for pure backtesting scenario.
Am I wrong ? Anyone having a different experience ?
Unfortunately the fix wasn't as trivial - since the error codes 2105/2016 were sent in other scenarios as well - so since nobody was interested I've just abandoned the fix. Currently I'm just making my best to avoid such double connections :-(
try to initialize the Cerebro engine with preload=False:
cerebro = bt.Cerebro(stdstats=False, live=False, preload=False)
please report if it solved the problem. It seems that IBData will not report the end of historical data load ending properly in case the preload was requested - need to investigate further.
Nevermind I figured it out. I wanted to do line[-5] as the value because this would fit the offsetting problem but it always gives errors, but I just had to basically skip the beginning of the line (where line[-5] didn't exist yet) and that fixed it. Sorry idk how I didn't figure this out last night guess I was just burned out. So yeah if anyone comes across this problem just basically do:
line[-5] = value you normally would put at line
and if you have a situation like mine, I had to use the pivot period so it was like above but had to convert the period to Int
@Atul-Patel were you able to get this done for IEX data? How did it compare to IB data feed? Did you find any issues with BT resampling for indicators as values are off like mentioned in my others posts.
Any updates on alternate and reliable data feed for Backtrader would be very much appreciated
@Mango-Loco Hi, it seems that I am working on the same issue.
But the code snippets are not enough for me to can solve my problem.
Can you please sent to me the complete code (without trading strategy) only the part to get the own data fields in backtrader, that I can use it as indicators.
Many thanks in advance.
This is strange indeed. In case you see the print inside the if not self._statelivereconn: - if we follow the code flow - it seems nothing should prevent issuing another subscription. It means that the appropriate request should have been seen in the API logs - but apparently it did not.
On another note:
Just compared the patch in the master branch of mementum\backtrader repository and my fork and there are some differences. In particular the patch in mementum\backtrader lacks the subscription cancellation code.
I'll upload the updated PR in the coming days after additional testing.
You may try to use my fork (https://github.com/vladisld/backtrader in 'lazytrader' branch) or just take a diff of ibdata.py and ibstore.py from there and see if it works for you until the updated PR will be uploaded.