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/

    TypeError

    General Code/Help
    3
    6
    43
    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.
    • Abhishek Agrawal
      Abhishek Agrawal last edited by

      **Getting this error again. Started again with Backtrader after few days of break. Please help
      @run-out @ab_trader @backtrader **```
      import backtrader as bt
      import datetime
      import backtrader.feeds as btfeed

      class MyStrategy(bt.Strategy):
      def init(self):
      sma_short = bt.ind.SMA(period=10)
      sma_long = bt.ind.SMA(period=20)
      self.crossover = bt.ind.CrossOver(sma_short, sma_long)

      def next(self):
          if not self.position:
              if self.crossover > 0:
                  self.buy()
          elif self.crossover < 0:
              self.close()
      

      if name == 'main':
      cerebro = bt.Cerebro

      class DataFeed(btfeed.GenericCSVData):
          params = (
      
              ('dtformat', '%Y%m%d'),
              ('tmformat', '%H:%M:%S'),
              ('datetime', 0),
              ('time', 1),
              ('open', 2),
              ('high', 3),
              ('low', 4),
              ('close', 5),
              ('volume', 6),
              ('openinterest', 7)
          )
      
      
      data = DataFeed(dataname='BANKNIFTYAUG19.csv', timeframe=bt.TimeFrame.Minutes, compression=15,
                      fromdate=datetime.datetime(2019, 7, 20),
                      todate=datetime.datetime(2019, 8, 30))
      
      cerebro.adddata(data)
      cerebro.addstrategy(MyStrategy)
      cerebro.run()
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        There are no mind readers here, so you may want to put full error message.
        Also use backticks to post code blocks, this is written on the every top of the page.

        1 Reply Last reply Reply Quote 1
        • Abhishek Agrawal
          Abhishek Agrawal last edited by

          So sorry for not posting the error.
          Here you go.

          Traceback (most recent call last):
          File "C:/Users/Abhishek/Backtrader/orb.py", line 44, in <module>
          cerebro.adddata(data)
          TypeError: adddata() missing 1 required positional argument: 'data'

          import backtrader as bt
          import datetime
          import backtrader.feeds as btfeed
          
          
          class MyStrategy(bt.Strategy):
              def __init__(self):
                  ma_short = bt.ind.SMA(period=10)
                  ma_long = bt.ind.SMA(period=20)
                  self.crossover = bt.ind.CrossOver(ma_long, ma_short)
          
              def next(self):
                  if not self.position:
                      if self.crossover > 0:
                          self.buy()
                  elif self.crossover < 0:
                      self.close()
          
          
          if __name__ == '__main__':
              cerebro = bt.Cerebro
          
          
              class DataFeed(btfeed.GenericCSVData):
                  params = (
          
                      ('dtformat', '%Y%m%d'),
                      ('tmformat', '%H:%M:%S'),
                      ('datetime', 0),
                      ('time', 1),
                      ('open', 2),
                      ('high', 3),
                      ('low', 4),
                      ('close', 5),
                      ('volume', 6),
                      ('openinterest', 7)
                  )
          
          
              data = DataFeed(dataname='BANKNIFTYAUG19.csv', timeframe=bt.TimeFrame.Minutes, compression=15,
                              fromdate=datetime.datetime(2019, 7, 20),
                              todate=datetime.datetime(2019, 8, 30))
          
              cerebro.adddata(data)
              cerebro.addstrategy(MyStrategy)
              cerebro.broker.setcash(100000)
          
              cerebro.run()
          
          
          run-out 1 Reply Last reply Reply Quote 0
          • run-out
            run-out @Abhishek Agrawal last edited by

            @Abhishek-Agrawal said in TypeError:

            cerebro = bt.Cerebro

            You need brackets on bt.Cerebro

            bt.Cerebro()
            
            1 Reply Last reply Reply Quote 1
            • Abhishek Agrawal
              Abhishek Agrawal last edited by

              Thanks a lot for the reply man it helped.
              @run-out there's one another problem- The code is not ending anywhere. It is just running

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

                Print your log out to terminal for OHLCV and see what's happening.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                $(document).ready(function () { app.coldLoad(); }); }