Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Connectivity issues to Interactive Broker

    General Code/Help
    2
    7
    889
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • run-out
      run-out last edited by run-out

      Connecting to Interactive Broker gets stuck at delay. I am using a simplified script to isolate the problem.

      def run(args=None):
          cerebro = bt.Cerebro(stdstats=False)
          store = bt.stores.IBStore(host='127.0.0.1', port=7497, clientId=147)
          cerebro.broker = store.getbroker()
          # also: 'TQQQ-STK-ISLAND-USD'
          data = store.getdata(dataname='AAPL-STK-SMART-USD', timeframe=bt.TimeFrame.Days)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Days, compression=1)
      
          cerebro.addstrategy(St)
          cerebro.run()
      

      The error message is:

      Server Version: 76
      TWS Time at connection:20190825 07:09:20 EST
      ***** STORE NOTIF: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm.nj>
      ***** STORE NOTIF: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:eufarm>
      ***** STORE NOTIF: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm>
      ***** STORE NOTIF: <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:euhmds>
      ***** STORE NOTIF: <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds.nj>
      ***** STORE NOTIF: <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds>
      ***** DATA NOTIF: DELAYED
      

      I have verified the connection to TWS works using IBDataCache script which connects using ibConnection. I am connecting using an approved and funded account and connected to a paper trading account.

      When debugging, I can get as far as notify_data. Script does not get to next().

      I've installed Interactive Brokers Python API Version 0.9/70 Author Troy Melhase. I've also created a new 'clean' envirnment to reduce the posibility of conflicts. The same error comes up.

      I'm using:
      Python 3.7
      Backtrader 1.9.74.123
      ib-api 0.9.70
      linux-ubuntu 18.04.3 LTS

      Thanks for any assistance, I've exhausted the docs...

      RunBacktest.com

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @run-out last edited by

        @run-out said in Connectivity issues to Interactive Broker:

        I've installed Interactive Brokers Python API Version 0.9/70 Author Troy Melhase

        I know Troy Melhase was the original author of the Java => Python translator, which he used to create the Python wrapper of the API. But his work ended long ago and his project was continued by other people, to ensure the API remained compatible.

        @run-out said in Connectivity issues to Interactive Broker:

        ib-api 0.9.70

        I don't know what that is. Never seen that in my life. If that was created by Troy Melhase it must be a real relic, probably meant for a museum. That's not even the latest package created by Troy. See

        • https://stackoverflow.com/questions/29313794/cant-call-ib-package

        The backtrader docs Docs - Live Trading - Interactive Brokers - (https://www.backtrader.com/docu/live/ib/ib/ say:

        pip install git+https://github.com/blampe/IbPy.git
        

        There is the possibility to install directly from pypi with:

        pip install IbPy2
        

        as indicated in the repository: https://github.com/blampe/IbPy

        (The package did not exist back when the docs were first written)

        1 Reply Last reply Reply Quote 0
        • run-out
          run-out last edited by

          Thanks for the reply. Turns out I was selecting the wrong item from inside pycharm. I'm now using IbPy2 but still the program is hanging up at

          /home/runout/Documents/quant/ibtest/venv/bin/python /home/runout/Documents/quant/ibtest/ibsimple.py
          Server Version: 76
          TWS Time at connection:20190825 13:02:17 EST
          ***** STORE NOTIF: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm.nj>
          ***** STORE NOTIF: <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm>
          ***** STORE NOTIF: <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:euhmds>
          ***** STORE NOTIF: <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds>
          ***** DATA NOTIF: DELAYED
          

          The program is still active here.

          Also, the TWS is showing an API Connection status of 'accepted', not connected.

          API Connections Status

          When I break/stop the program, I get the following:

          Traceback (most recent call last):
            File "/home/runout/Documents/quant/ibtest/ibsimple.py", line 95, in <module>
              run()
            File "/home/runout/Documents/quant/ibtest/ibsimple.py", line 91, in run
              cerebro.run()
            File "/home/runout/Documents/quant/ibtest/venv/lib/python3.7/site-packages/backtrader/cerebro.py", line 1127, in run
              runstrat = self.runstrategies(iterstrat)
            File "/home/runout/Documents/quant/ibtest/venv/lib/python3.7/site-packages/backtrader/cerebro.py", line 1298, in runstrategies
              self._runnext(runstrats)
            File "/home/runout/Documents/quant/ibtest/venv/lib/python3.7/site-packages/backtrader/cerebro.py", line 1542, in _runnext
              drets.append(d.next(ticks=False))
            File "/home/runout/Documents/quant/ibtest/venv/lib/python3.7/site-packages/backtrader/feed.py", line 407, in next
              ret = self.load()
            File "/home/runout/Documents/quant/ibtest/venv/lib/python3.7/site-packages/backtrader/feed.py", line 479, in load
              _loadret = self._load()
            File "/home/runout/Documents/quant/ibtest/venv/lib/python3.7/site-packages/backtrader/feeds/ibdata.py", line 442, in _load
              self.qlive.get(timeout=self._qcheck))
            File "/home/runout/anaconda3/lib/python3.7/queue.py", line 179, in get
              self.not_empty.wait(remaining)
            File "/home/runout/anaconda3/lib/python3.7/threading.py", line 300, in wait
              gotit = waiter.acquire(True, timeout)
          KeyboardInterrupt
          
          Process finished with exit code 1
          

          RunBacktest.com

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            Never checked the Connected/Accepted states but the position in the code only simply means that the data feed is awaiting data.

            Actually ... and leaving the ib-api 0.9.70 topic behind ...

            The program is awaiting data because AAPL is NOT trading right now, which means there are no incoming ticks.

            Check the historical parameter in the documentation linked above if you simply want to download data and not actually engage in live trading.

            1 Reply Last reply Reply Quote 1
            • B
              backtrader administrators last edited by

              As a side note, PyCharm and all other kernel-hijacking super-duper-integrated-destruction-environments are known to have problems with (for example) multiprocessing. I don't know about threading, but I personally would never execute code from inside those things.

              1 Reply Last reply Reply Quote 1
              • run-out
                run-out last edited by

                Thanks for your support. I appreciate it and all the work you do.

                I tried running historical and was still hanging up, so I did what your'e not supposed to do, I deleted and reinstalled backtrader, IbPy2, and TWS all at the same time.

                When I reinstalled TWS I installed the 'older' version.

                And now, no problem, historical info coming in at light speed. Which is great.

                But since I also did the other uninstalls and installs, I can only speculate that the latest/best/greatest stable TWS was the problem? Personally I think that was it.

                Regarding your side note, what do you normally use for running your programs if not an IDE like pycharm?

                Thanks again DR for all your help! Awesome package. Keep up the great work.

                RunBacktest.com

                B 1 Reply Last reply Reply Quote 0
                • B
                  backtrader administrators @run-out last edited by

                  @run-out said in Connectivity issues to Interactive Broker:

                  Regarding your side note, what do you normally use for running your programs if not an IDE like pycharm?

                  The shell ...

                  1 Reply Last reply Reply Quote 1
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors