A new IB integration using ib_sync
-
Hey, guys!
I really appreciate the support of this community and also the work of Daniel behind backtrader. I have extended and created my own framework based on backtrader, there is nothing like this out there.
So your help is needed again, but in this case testing this version of the IB Store (Broker and DataFeed) using async integration with Interactive Brokers through ib_insync. I believe that this feature is one of the most recurrent improvement request for backtrader (and also a need for myself).
The repo is here: https://github.com/ultra1971/backtrader_ib_insync
Remember this an alpha+ version but fully working in my dev environment.
During the stress testing that I ran with the tick price implementation, an average of 3 orders per second were reached, very promising. The connection stability is another improvement, this means no more hangs and this is something that you're looking for in any algotrading platform.
You can find most of the original IB Store logic are there, cover most of the functionality, but happy to complete this with your support.
A special thanks to the ib_insync community and of course to Ewald for keep this project active.
Finally, this is the best way that I find to give you back my appreciation, thanks.
Regards
-
@ultra1971 Hey there, I installed your three broker files into a venv, and then ran a simple script just to connect.
import backtrader as bt from backtrader.stores.ibstore import IBStore class TestPositions(bt.Strategy): def __init__(self): self.traded = False def next(self): if not self.traded: self.buy() print(f"{self.data.datetime.datetime()} position: {self.position}") self.traded = True def run(): cerebro = bt.Cerebro() store = IBStore(host="127.0.0.1", port=7497, clientId=***) cerebro.setbroker(store.getbroker()) data = store.getdata( dataname="MSFT", historical=True, backfill=True, backfill_start=False ) cerebro.adddata(data) cerebro.addstrategy(TestPositions) cerebro.run() if __name__ == "__main__": run()
I get the following error.
File "/home/runout/projects/backtrader_ib_insync/venv/lib/python3.8/site-packages/backtrader/stores/ibstore.py", line 204, in getbroker return cls.BrokerCls(*args, **kwargs) TypeError: 'NoneType' object is not callable
I look forward to giving this a try! Thanks for your help.
-
@run-out yes, know the error very well, pls put also init.py into the folder to import everything.
The IDStore calls to the IBBoker.BrokerCls attribute but is not set not at this time. You can force this setting it manually pointing the location of the IBBroker, but is not the idea.
-
@ultra1971 I'm excited to use this but I'm still getting the error. Would it be possible for you to write up clear installation instructions in your readme file so we know exaclty how you want this installed? I'm making a mistake somewhere and it's because i'm guessing a bit.
-
@run-out Thanks!
-
i created a pull request, to make this module installable via pip and moved the files to a module dir.
Seems to be working fine. Had no problems in running this module.
One thing as a suggestion:
Maybe class naming could be different than backtraders code to avoid confusion what store, broker, feed is being used. Both implementations are called IBStore, IBBroker, IBData -
@run-out yes, no problem, and also I will include a simple mockup as an example of how to use.
But I suggest to create a folder into your own project and put all the files there, including "_ _ init_ _.py"
That part of the code that you get the error is very tricky, take me a lot of debug time understand the issue and how to solve.
-
@dasch I just started to use github, and need to learn how to use the platform. Thanks for your contribution!
I intentionally keep the names in order to make a full replacement to the legacy IBStore, and also reduce the changes into current code, but open to your feedback, guys
-
@ultra1971 thanks to @dasch i can use
pip install git+https://github.com/ultra1971/backtrader_ib_insyncas a installation method