Backtrader Community

    • 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/

    SyntaxError: multiple exception types must be parenthesized

    General Discussion
    2
    2
    367
    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.
    • K
      konrad last edited by

      Hello, I am trying to run this sample code to connect backtrader to IB and do live trading. I am unable to resolve this error:

      SyntaxError: multiple exception types must be parenthesized

      Any solutions to this problem are appreciated.
      My code is attached.
      Thanks!

      import backtrader as bt
      
      class MyStrategy(bt.Strategy):
      
          def __init__(self):
              print("initializing strategy")
              self.data_ready = False
              
          def notify_data(self, data, status):
              print('Data Status =>', data._getstatusname(status))
              if status == data.LIVE:
                  self.data_ready = True
      
          def log_data(self):
              ohlcv = []
              ohlcv.append(str(self.data.datetime.datetime()))
              ohlcv.append(str(self.data.open[0]))
              ohlcv.append(str(self.data.high[0]))
              ohlcv.append(str(self.data.low[0]))
              ohlcv.append(str(self.data.close[0]))
              ohlcv.append(str(self.data.volume[0]))
              print(",".join(ohlcv))
          
          def next(self):
              self.log_data()
      
              if not self.data_ready:
                  return
      
              # if not self.position:
              #     self.buy(size=2)
              # elif self.position:
              #     self.sell()
      
      def start():
          print("starting backtrader")
          cerebro = bt.Cerebro()
      
          ##### For live trading #####
          # Can check port number by going into TWS and clicking file --> global configuration --> API --> settings
          # put in clientID?
          store = bt.stores.IBStore(port=7497)
      
          # Forex
          data = store.getdata(dataname='USD.JPY', sectype='CASH', exchange='IDEALPRO', timeframe=bt.TimeFrame.Seconds)
      
          # Aggregates into 15 second bars
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds, compression=15)
          
          cerebro.broker = store.getbroker()
          cerebro.addstrategy(MyStrategy)
          cerebro.run()
      
      start()
      
      1 Reply Last reply Reply Quote 0
      • Nurettin Onur TUĞCU
        Nurettin Onur TUĞCU last edited by

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