Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. konrad
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    K
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 1
    • Best 0
    • Controversial 0
    • Groups 0

    konrad

    @konrad

    0
    Reputation
    1
    Profile views
    1
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    konrad Unfollow Follow

    Latest posts made by konrad

    • SyntaxError: multiple exception types must be parenthesized

      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()
      
      posted in General Discussion
      K
      konrad