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/

    GenericCSVData - 'Cerebro' object has no attribute '_exactbars'

    General Code/Help
    3
    6
    213
    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.
    • G
      Giacomo last edited by

      Hi there,

      newbie here.
      I'm trying to feed to cerebro a csv file, this is my code:

      import backtrader as bt
      from binance.client import Client
      import pandas as pd
      
      class firstStrategy(bt.Strategy):
          params = (
              ('period', 21),
              )
      
          def __init__(self):
              self.startcash = self.broker.getvalue()
              self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.params.period)
      
          def next(self):
              if not self.position:
                  if self.rsi < 30:
                      self.buy(size=100)
              else:
                  if self.rsi > 70:
                      self.sell(size=100)
      
          def stop(self):
              pnl = round(self.broker.getvalue() - self.startcash,2)
              print('RSI Period: {} Final PnL: {}'.format(
                  self.params.period, pnl))
      
      if __name__ == '__main__':
          startcash = 10000
      
      cerebro = bt.Cerebro()
      
      cerebro.optstrategy(firstStrategy, period=range(14, 21))
      
      data = bt.feeds.GenericCSVData(dataname='..../2020_1minute.csv',
                                     dtformat=('%Y-%m-%d'),
                                     tmformat=('%H.%M.%S'),
                                     datetime=11,
                                     open=0,
                                     high=1,
                                     low=2,
                                     close=3,
                                     volume=4,
                                     time=12,
                                     openinterest=-1)
      
      
      cerebro.adddata(data)
      
      cerebro.plot()
      

      this is what the csv looks like

      ,open,high,low,close,volume,close_time,quote_asset_volume,number_of_trades,taker_buy_base_asset_volume,taker_buy_quite_asset_volume,ignore,date,time
      0,7195.24000000,7196.25000000,7183.14000000,7186.68000000,51.64281200,2020-01-01 00:00:59.999000072,371233.51835535,493,19.59823000,140888.41428273,0,2020-01-01,00:00:00
      1,7187.67000000,7188.06000000,7182.20000000,7184.03000000,7.24814800,2020-01-01 00:01:59.999000072,52080.12778780,135,2.03177200,14599.21192429,0,2020-01-01,00:01:00
      2,7184.41000000,7184.71000000,7180.26000000,7182.43000000,11.68167700,2020-01-01 00:02:59.999000072,83903.74163545,202,5.47924400,39357.08177646,0,2020-01-01,00:02:00
      

      ultimately the error I get is the following:

      AttributeError: 'Cerebro' object has no attribute '_exactbars'
      

      If anyone has any idea of what might be happening I would be very grateful for any hint!

      1 Reply Last reply Reply Quote 0
      • R
        rajanprabu last edited by

        On the first look your csv index seems to be mixed up.. and Cerebro.run() is missing..

        G 1 Reply Last reply Reply Quote 2
        • G
          Giacomo @rajanprabu last edited by

          @rajanprabu thank you very much for pointing out the index mix up.
          I changed the csv file to have it looking as follow

          date,open,high,low,close,volume,time
          2020-01-01,7195.24000000,7196.25000000,7183.14000000,7186.68000000,51.64281200,00:00:00
          2020-01-01,7187.67000000,7188.06000000,7182.20000000,7184.03000000,7.24814800,00:01:00
          

          I also added cerebtro.run()

          However the same error persists.

          Here my current code

          import backtrader as bt
          
          class firstStrategy(bt.Strategy):
              params = (
                  ('period', 21),
                  )
          
              def __init__(self):
                  self.startcash = self.broker.getvalue()
                  self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.params.period)
          
              def next(self):
                  if not self.position:
                      if self.rsi < 30:
                          self.buy(size=100)
                  else:
                      if self.rsi > 70:
                          self.sell(size=100)
          
              def stop(self):
                  pnl = round(self.broker.getvalue() - self.startcash,2)
                  print('RSI Period: {} Final PnL: {}'.format(
                      self.params.period, pnl))
          
          if __name__ == '__main__':
              startcash = 10000
          
          cerebro = bt.Cerebro()
          
          cerebro.optstrategy(firstStrategy, period=range(14, 21))
          
          data = bt.feeds.GenericCSVData(dataname='/Users/giacomofederle/TRADINGBOT/crypto_bot_course/2020_1minute.csv',
                                         dtformat=('%Y-%m-%d'),
                                         tmformat=('%H:%M:%S'),
                                         datetime=0,
                                         open=1,
                                         high=2,
                                         low=3,
                                         close=4,
                                         volume=5,
                                         time=6,
                                         openinterest=-1)
          
          cerebro.adddata(data)
          
          cerebro.plot()
          
          cerebro.run()
          

          thank you again

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

            @Giacomo the
            run method should be called before
            plot.

            G 1 Reply Last reply Reply Quote 2
            • G
              Giacomo @vladisld last edited by

              @vladisld thank you! That did made a difference.

              Now I'm dividing by zero somewhere apparently

              ZeroDivisionError: float division by zero
              

              I know that the problem is with the bt.feeds.GenericCSVData part, because if I replace that bit with a feed from yahoo it works

              1 Reply Last reply Reply Quote 0
              • G
                Giacomo last edited by

                Solved

                self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.params.period, safediv=True)
                
                1 Reply Last reply Reply Quote 1
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors