quantopian / zipline

Zipline, a Pythonic Algorithmic Trading Library
https://www.zipline.io
Apache License 2.0
17.28k stars 4.67k forks source link

Error: unsupported broker: can't import module zipline.gens.brokers.ib_broker #2730

Closed FrankPSch closed 4 years ago

FrankPSch commented 4 years ago

Dear Zipline-Trader Maintainers,

Key Question: what do I need to install on top of python, C++ 14.0, IB TWS and zipline-trader for live trading? Backtesting is working well.

1) Before I tell you about my issue, let me describe my environment:

* Operating System: Windows 10
* Python Version: Python 3.6.8rc1
* Python Bitness: 64
* How did you install Zipline: pip
* Python packages: pure zipline-trader

(2) Now that you know a little about me, let me tell you about the issue I am having:

I am trying to connect to IB using TWS for trading, e.g. with

zipline run -f my_zl_trader_test.py --state-file my_zl_trader_test.state --broker ib --broker-url localhost:7496:1232 --bundle quantopian-quandl --data-frequency daily --realtime-bar-target ./realtime-bars

The error message is:

Error: unsupported broker: can't import module zipline.gens.brokers.ib_broker

If I check the file ib_broker.py I find:

from ib.ext.EClientSocket import EClientSocket
from ib.ext.EWrapper import EWrapper
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.ext.ExecutionFilter import ExecutionFilter
from ib.ext.EClientErrors import EClientErrors

-> Do I need to install anything else?

(3) Here is how you can reproduce this issue on your machine:

Create a new env
Install zipline-trader from pip
Have TWS running
Use the CLI

(4) What steps have you taken to resolve this already?

Followed the instructions from the zipline-trader site and followed the video on installing zipline
Followed the instructions from IB on how to set up the API

Did a lot of Research and found e.g.
    https://groups.google.com/forum/#!topic/zipline-live/jLc1jU4-nWQ

Did some bugfixes:

C:\Users\Frank\AppData\Local\Programs\Python\Python36\myzipenv\Lib\site-packages\zipline\__main__.py
in line 19, add:
    import pkgutil (-> fixed a bug)
    from importlib import import_module (-> fixed a bug)

pip install Polling (-> fixed a bug)

Installed the TWS API from IB (-> same error remains)

The error results from these lines in __main__:
    try:
        bmod = import_module(mod_name)
    except ImportError:
        ctx.fail("unsupported broker: can't import module %s" % mod_name)

Thanks a lot for taking the time, Frank

FrankPSch commented 4 years ago

Hi there,

it seems that zipline-trader requires IBpy to connect to TWS. Since IBpy has not been updated for several years, I try to connect to the official TWS API. Therefore, I installed the TWS API and created a new file ib_broker.zip which needs to be placed in the directory /zipline/gens/brokers. Any thoughts on this file are highly appreciated - so far no errors in my configuration, but not used in live trading so far as it is Sunday.

Thanks, Frank

FrankPSch commented 4 years ago

Summary of changes made (see above):

Imports #from ib.ext.EClientSocket import EClientSocket from ibapi.client import * #from ib.ext.EWrapper import EWrapper from ibapi.wrapper import * #from ib.ext.Contract import Contract from ibapi.contract import * #from ib.ext.Order import Order from ibapi.order import * #from ib.ext.ExecutionFilter import ExecutionFilter #from ib.ext.EClientErrors import EClientErrors from ibapi.common import * from ibapi import *

Classes #class TWSConnection(EClientSocket, EWrapper): class TWSConnection(EClient, EWrapper):

In the def init of class TWSConnection(EClient, EWrapper) # EClientSocket.__init__(self, anyWrapper=self) EClient.__init__(self, wrapper=self) self.eConnect = self.connect

At the end of class TWSConnection(EClient, EWrapper) #self.connect() self.myConnect()

Replacing def connect(self) #def connect(self): def myConnect(self):

ajjcoppola commented 4 years ago

Frank, Check out:

https://pypi.org/project/zipline-live2/ https://github.com/shlomikushchi/zipline-live2

Will save you work! alan

FrankPSch commented 4 years ago

Hi Alan,

thanks a lot for the links!

On the 2nd link, it says that zipline-live2 is now continued under https://github.com/shlomikushchi/zipline-trader, which is what I installed. Both versions import ib.ext in the file ib_broker.py and therefore seem to require an IBpy installation.

On https://github.com/blampe/IbPy (last updated 2017), it says Beginning with release 9.73, InteractiveBrokers is now officially supporting a new Python API client (Python 3 only). This should make this repo superfluous except for Python 2.

Is this what the community is using? Very good, should it still be up to date! With with Python version does it run stable, and should Python then be installed from Conda or from Python.Org?

Thanks again, Frank

ajjcoppola commented 4 years ago

Frank, I don't know exactly what is wrong with what your are doing.

I use Linux boxes...and have successfully run live IB-TWS/IB-Gateway trading on AWS Instances, Windows10-HyperV-Ubuntu, and AWS Workspaces and haven't seen the troubles with IBpy you are seeing, yet I'm not surprised you are seeing them...it is a persnickity process.

We are using anaconda, and then installing our slight modification(zipline-broker) of zipline-live2 (Note: we haven't tried the new version zipline-broker yet) on top of it.

That kind of install I can help you with...some version of Linux...for us, at this time, Windows 10 is a step too far that does nothing for our current goals. alan

FrankPSch commented 4 years ago

Hi Alan,

thanks a lot for your hepl! In the meantime, I was able to move a bit further, one step at a time... Still way to go but on other topics now, so I am closing this issue. It feels a bit strange to work with such old versions but it only means more thorough testing ;)

Frank