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/

    Beginner question: Interactive Brokers Data not loading

    General Code/Help
    2
    2
    41
    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.
    • DVX
      DVX last edited by

      Hey guys!

      I just started using Backtrader and am currently facing a problem with receiving data from IB.

      Previously I have been working with IB API, IBridgePy and IBpy already and never faced problems with retrieving data.
      My account includes live tick data so it should be no problem to get the data.

      import backtrader as bt
      
      
      class PrintClose(bt.Strategy):
      
      
          def logdata(self):
              txt = []
              txt.append('{}'.format(len(self)))
              txt.append('{}'.format(
                  self.data.datetime.datetime(0).isoformat()))
              txt.append('{:.2f}'.format(self.data.open[0]))
              txt.append('{:.2f}'.format(self.data.high[0]))
              txt.append('{:.2f}'.format(self.data.low[0]))
              txt.append('{:.2f}'.format(self.data.close[0]))
              txt.append('{:.2f}'.format(self.data.volume[0]))
              print(','.join(txt))
          def next(self):
              self.logdata()
      
      
      def run(args=None):
          cerebro = bt.Cerebro()
          ibstore = bt.stores.IBStore(host='127.0.0.1', port=7497, clientId=35)
          data = ibstore.getdata(dataname='EUR.USD-CASH-IDEALPRO',
                             timeframe=bt.TimeFrame.Seconds, compression=5)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=2)
      
          cerebro.adddata(data) #not sure if this is needed, but commenting it out does not fix the problem
      
          cerebro.addstrategy(PrintClose)
      
      
          cerebro.run()
      
      if __name__ == '__main__':
          run()
      

      This is my current code. TWS API port is set correctly.

      This is what I get from my terminal.

      Server Version: 76
      TWS Time at connection:20201209 17:48:10 MEZ
      Server Version: 76
      TWS Time at connection:20201209 17:48:18 MEZ
      Server Version: 76
      TWS Time at connection:20201209 17:48:26 MEZ
      
      

      I am certain that it must be some really minor thing that keeps me from printing the data,

      Thanks in advance for your help,
      DVX

      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        I would suggest to take a look at the TWS API logs and see if all the data farm connections were OK for example and there are no other error notifications.

        Alternatively you may implement the notify_store method in your strategy and print the messages received from the IB store. for example:

            def notify_store(self, msg, *args, **kwargs):
                print('STORE NOTIF:{}', msg)
        

        I've run your code using TWS desktop and got the following output:

        Server Version: 76
        TWS Time at connection:20201209 23:45:21 Israel Standard Time
        STORE NOTIF:{} <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:hfarm>
        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:jfarm>
        STORE NOTIF:{} <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfuture>
        STORE NOTIF:{} <error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:cashfarm>
        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:cashhmds>
        STORE NOTIF:{} <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:fundfarm>
        STORE NOTIF:{} <error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds>
        STORE NOTIF:{} <error id=-1, errorCode=2158, errorMsg=Sec-def data farm connection is OK:secdefeu>
        1,2020-12-07T17:18:00,1.21,1.21,1.21,1.21,-1.00
        2,2020-12-07T17:22:00,1.21,1.21,1.21,1.21,-1.00
        3,2020-12-07T17:26:00,1.21,1.21,1.21,1.21,-1.00
        4,2020-12-07T17:30:00,1.21,1.21,1.21,1.21,-1.00
        5,2020-12-07T17:34:00,1.21,1.21,1.21,1.21,-1.00
        6,2020-12-07T17:38:00,1.21,1.21,1.21,1.21,-1.00
        ...
        
        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }