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/

    Tws connection problem

    General Code/Help
    2
    3
    233
    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.
    • S
      Sasha last edited by Sasha

      Shows nothing after this command

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import backtrader as bt
      
      
      class St(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))
      
          data_live = False
      
          def notify_data(self, data, status, *args, **kwargs):
              print('*' * 5, 'DATA NOTIF:', data._getstatusname(status), *args)
              if status == data.LIVE:
                  self.data_live = True
      
          def notify_order(self, order):
              if order.status == order.Completed:
                  buysell = 'BUY ' if order.isbuy() else 'SELL'
                  txt = '{} {}@{}'.format(buysell, order.executed.size,
                                          order.executed.price)
                  print(txt)
      
          bought = 0
          sold = 0
      
          def next(self):
              self.logdata()
              if not self.data_live:
                  return
      
              if not self.bought:
                  self.bought = len(self)  # keep entry bar
                  self.buy()
              elif not self.sold:
                  if len(self) == (self.bought + 3):
                      self.sell()
      
      
      def run(args=None):
          cerebro = bt.Cerebro(stdstats=False)
          ibstore = bt.stores.IBStore(host='127.0.0.1', port=7497, clientId=35)
          data = ibstore.getdata(dataname='TWTR', timeframe=bt.TimeFrame.Ticks)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds, compression=10)
      
          cerebro.broker = ibstore.getbroker()
      
          cerebro.addstrategy(St)
          cerebro.run()
      if __name__ == '__main__':
          run()
      

      What should i change to have connection?

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

        enable API in IB Workstation, and check port number.

        S 1 Reply Last reply Reply Quote 0
        • S
          Sasha @rami last edited by

          @rami thank's, the problem was with port number. Successfully connected with IB Workstation, and I have another problem.For output, I get

          Server Version: 76
          TWS Time at connection:20190506 01:29:55 ALMT
          ***** DATA NOTIF: DELAYED
          ***** DATA NOTIF: NOTSUBSCRIBED
          
          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors