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/

    float division by zero

    Indicators/Strategies/Analyzers
    3
    17
    1374
    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.
    • Mario Pellegrini
      Mario Pellegrini last edited by

      after using the bt.feeds.YahooFinanceCSVData
      to extract a dataset with pre-set dates
      I have carried out correctly
      cerebro.adddata (date)
      and set the strategy, but at the time of the run I get the following error:
      ZeroDivisionError: float division by zero
      The program is structured as follows:

      from pandas_datareader import data,wb
      import datetime
      start = datetime.datetime(2014,1,1)
      end = datetime.datetime(2019,3,1)
      df=data.get_data_yahoo('FCA.MI',start, end)
      df.to_csv('C:/dati/test/finance/fca.csv')

      data = bt.feeds.YahooFinanceCSVData(
      dataname='C:/dati/test/finance/fca.csv',
      fromdate=datetime.datetime(2015,1,1),
      # Do not pass values after this date
      todate=datetime.datetime(2018,12,31),
      reverse=False)

      class MyStrategy(bt.Strategy):
      ...

      cerebro = bt.Cerebro()
      cerebro.addstrategy(MyStrategy)
      cerebro.adddata(data)
      cerebro.broker.setcash(10000)
      cerebro.run()

      B 1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        If your data is full, was downloaded correctly and corresponds to bt.feeds.YahooFinanceCSVData format than the code shown should not generate division by zero error. If you want to go further, than provide strategy code as well as full error message. Python gives reference to the actual part of the script where the error happens, which helps a lot to identify the problem.

        PS Please read the message on top of the forum bout using ``` (aka backtick or grave accent) and use them.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        Mario Pellegrini 1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators @Mario Pellegrini last edited by

          @Mario-Pellegrini said in float division by zero:

          from pandas_datareader import data,wb
          import datetime
          start = datetime.datetime(2014,1,1)
          end = datetime.datetime(2019,3,1)
          df=data.get_data_yahoo('FCA.MI',start, end)
          df.to_csv('C:/dati/test/finance/fca.csv')
          data = bt.feeds.YahooFinanceCSVData(

          1. backtrader can directly read from Yahoo with YahooFinanceData
          2. backtrader can directly read from a pandas dataframe
          3. As such the path: pandas web reader => yahoo => dataframe => csv => yahoofinancecsvdata seems a bit of an overkill.
          1 Reply Last reply Reply Quote 0
          • Mario Pellegrini
            Mario Pellegrini last edited by

            I can't get backtrader read directly from Pandas, the following code gives me error:
            from pandas_datareader import data,wb
            import datetime
            start = datetime.datetime(2014,1,1)
            end = datetime.datetime(2019,3,1)
            data=data.get_data_yahoo('FCA.MI',start, end)
            cerebro = bt.Cerebro()
            cerebro.adddata(data)

            B 1 Reply Last reply Reply Quote 0
            • Mario Pellegrini
              Mario Pellegrini @ab_trader last edited by

              @ab_trader said in float division by zero:

              If your data is full, was downloaded correctly and corresponds to bt.feeds.YahooFinanceCSVData format than the code shown should not generate division by zero error. If you want to go further, than provide strategy code as well as full error message. Python gives reference to the actual part of the script where the error happens, which helps a lot to identify the problem.

              PS Please read the message on top of the forum bout using ``` (aka backtick or grave accent) and use them.

              I tried to re-run, surprisingly it alternates the error once yes and a no. Here is the complete code:

              from pandas_datareader import data,wb
              import datetime
              start = datetime.datetime(2014,1,1)
              end = datetime.datetime(2019,3,1)
              df=data.get_data_yahoo('FCA.MI',start, end)
              df.to_csv('C:/dati/test/finance/fca.csv')

              data = bt.feeds.YahooFinanceCSVData(
              dataname='C:/dati/test/finance/fca.csv',
              fromdate=datetime.datetime(2017,1,1),
              # Do not pass values after this date
              todate=datetime.datetime(2018,12,31),
              reverse=False)

              class MyStrategy(bt.Strategy):
              def init(self):
              self.rsi = bt.indicators.RSI_SMA(self.data.close, period=14)
              def next(self):
              if not self.position:
              if self.rsi < 30:
              self.buy(size=100)
              else:
              if self.rsi > 70:
              self.sell(size=100)

              cerebro = bt.Cerebro()
              cerebro.addstrategy(MyStrategy)
              cerebro.adddata(data)
              cerebro.broker.setcash(10000)
              cerebro.run()
              cerebro.plot()

              @ab_trader said in float division by zero:

              If your data is full, was downloaded correctly and corresponds to bt.feeds.YahooFinanceCSVData format than the code shown should not generate division by zero error. If you want to go further, than provide strategy code as well as full error message. Python gives reference to the actual part of the script where the error happens, which helps a lot to identify the problem.

              PS Please read the message on top of the forum bout using ``` (aka backtick or grave accent) and use them.

              I tried to re-run, surprisingly it alternates the error once yes and a no. Here is the complete code:

              from pandas_datareader import data,wb
              import datetime
              start = datetime.datetime(2014,1,1)
              end = datetime.datetime(2019,3,1)
              df=data.get_data_yahoo('FCA.MI',start, end)
              df.to_csv('C:/dati/test/finance/fca.csv')

              data = bt.feeds.YahooFinanceCSVData(
              dataname='C:/dati/test/finance/fca.csv',
              fromdate=datetime.datetime(2017,1,1),
              # Do not pass values after this date
              todate=datetime.datetime(2018,12,31),
              reverse=False)

              class MyStrategy(bt.Strategy):
              def init(self):
              self.rsi = bt.indicators.RSI_SMA(self.data.close, period=14)
              def next(self):
              if not self.position:
              if self.rsi < 30:
              self.buy(size=100)
              else:
              if self.rsi > 70:
              self.sell(size=100)

              cerebro = bt.Cerebro()
              cerebro.addstrategy(MyStrategy)
              cerebro.adddata(data)
              cerebro.broker.setcash(10000)
              cerebro.run()
              cerebro.plot()

              B 1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators @Mario Pellegrini last edited by

                @Mario-Pellegrini Sometimes ... looking at the docs (or using google to search) does really help ...

                [Docs - Data Feeds - Pandas]https://www.backtrader.com/docu/pandas-datafeed/pandas-datafeed/](https://www.backtrader.com/docu/pandas-datafeed/pandas-datafeed/)

                From a quick google search: https://community.backtrader.com/topic/255/read-data-from-pandas/2

                If your expectation is to pass any DataFrame and believe some kind of obscure artificial intelligence is going to find out if the input is valid or not ... it's a wrong expectation. You could also pass something that contains columns about the temperatures in Indonesia cross-correlated with the birth rate of turtles in the Cayman Islands in the 17th century, with exotic column names.

                1 Reply Last reply Reply Quote 0
                • B
                  backtrader administrators @Mario Pellegrini last edited by

                  @Mario-Pellegrini said in float division by zero:

                  I tried to re-run, surprisingly it alternates the error once yes and a no. Here is the complete code:

                  Even if your code is still not properly readable (@ab_trader already pointed out that you may want to put the code in a block surrounded by ``` (before and after, as indicated at the top, where a link to the markdown syntax is available) ...

                  Your "yes/no" seems to indicate a download problem (either no download, or the data doesn't qualify as good or ...)

                  You may still choose to read directly from Yahoo with backtrader ... but it is your choice.

                  Mario Pellegrini 1 Reply Last reply Reply Quote 0
                  • Mario Pellegrini
                    Mario Pellegrini @backtrader last edited by

                    @backtrader said in float division by zero:

                    @Mario-Pellegrini said in float division by zero:

                    I tried to re-run, surprisingly it alternates the error once yes and a no. Here is the complete code:

                    Even if your code is still not properly readable (@ab_trader already pointed out that you may want to put the code in a block surrounded by ``` (before and after, as indicated at the top, where a link to the markdown syntax is available) ...

                    Your "yes/no" seems to indicate a download problem (either no download, or the data doesn't qualify as good or ...)

                    You may still choose to read directly from Yahoo with backtrader ... but it is your choice.

                    I tried to read directly from Yahoo as suggested, but in execution of cerebro I get another error:
                    AttributeError: 'NoneType' object has no attribute 'close'

                    This is the code used:

                    from pandas_datareader import data,wb
                    import datetime

                    import backtrader as bt
                    from datetime import datetime
                    data = bt.feeds.Quandl(dataname='FCA.MI',
                    fromdate = datetime(2017,1,1),
                    todate = datetime(2018,12,31),
                    buffered= True )

                    class MyStrategy(bt.Strategy):
                    def init(self):
                    self.rsi = bt.indicators.RSI_SMA(self.data.close, period=14)
                    def next(self):
                    if not self.position:
                    if self.rsi < 30:
                    self.buy(size=100)
                    else:
                    if self.rsi > 70:
                    self.sell(size=100)

                    cerebro = bt.Cerebro()
                    cerebro.addstrategy(MyStrategy)
                    cerebro.adddata(data)
                    cerebro.broker.setcash(10000)
                    cerebro.run()
                    cerebro.plot()

                    B 1 Reply Last reply Reply Quote 0
                    • A
                      ab_trader last edited by

                      @Mario-Pellegrini why don't you put your code in the post using backticks (asked twice already) so it can be read much easier to read? Just wondering, you are asking for some help, but don't do even minimum effort to make it easier for helpers?

                      • If my answer helped, hit reputation up arrow at lower right corner of the post.
                      • Python Debugging With Pdb
                      • New to python and bt - check this out
                      1 Reply Last reply Reply Quote 0
                      • Mario Pellegrini
                        Mario Pellegrini last edited by

                        @Mario-Pellegrini said in float division by zero:

                        import datetime
                        
                        import backtrader as bt
                        from datetime import datetime
                        data = bt.feeds.Quandl(dataname='FCA.MI',
                        fromdate = datetime(2017,1,1),
                        todate = datetime(2018,12,31),
                        buffered= True )
                        
                        class MyStrategy(bt.Strategy):
                        def init(self):
                        self.rsi = bt.indicators.RSI_SMA(self.data.close, period=14)
                        def next(self):
                        if not self.position:
                        if self.rsi < 30:
                        self.buy(size=100)
                        else:
                        if self.rsi > 70:
                        self.sell(size=100)
                        
                        cerebro = bt.Cerebro()
                        cerebro.addstrategy(MyStrategy)
                        cerebro.adddata(data)
                        cerebro.broker.setcash(10000)
                        cerebro.run()
                        cerebro.plot()```
                        1 Reply Last reply Reply Quote 0
                        • Mario Pellegrini
                          Mario Pellegrini last edited by

                          from pandas_datareader import data,wb
                          import datetime
                          
                          import backtrader as bt
                          from datetime import datetime
                          data = bt.feeds.Quandl(dataname='FCA.MI',
                                 fromdate = datetime(2017,1,1),
                                 todate = datetime(2018,12,31),
                                 buffered= True )
                          
                          class MyStrategy(bt.Strategy):
                              def __init__(self):
                                  self.rsi = bt.indicators.RSI_SMA(self.data.close, period=14)
                              def next(self):  
                                  if not self.position:
                                      if self.rsi < 30:
                                          self.buy(size=100)  
                                  else: 
                                      if self.rsi > 70:
                                          self.sell(size=100) 
                          
                          
                          cerebro = bt.Cerebro()
                          cerebro.addstrategy(MyStrategy)
                          cerebro.adddata(data)
                          cerebro.broker.setcash(10000)
                          cerebro.run()
                          cerebro.plot()
                          
                          1 Reply Last reply Reply Quote 0
                          • B
                            backtrader administrators @Mario Pellegrini last edited by

                            @Mario-Pellegrini said in float division by zero:

                            I tried to read directly from Yahoo as suggested, but in execution of cerebro I get another error:
                            AttributeError: 'NoneType' object has no attribute 'close'

                            @Mario-Pellegrini said in float division by zero:

                            data = bt.feeds.Quandl(dataname='FCA.MI',
                            

                            Unless I am missing something very obvious, that doesn't read from Yahoo.

                            The home page contains a small section named Hello Algotrading with 3 different snippets, all of which work and read data directly from Yahoo.

                            • https://www.backtrader.com/home/helloalgotrading/
                            1 Reply Last reply Reply Quote 1
                            • Mario Pellegrini
                              Mario Pellegrini last edited by

                              the code is correct, there must be some problem with this ticker in particular, because with others it works. The fact is that I really wanted this FCA.MI, and with this ticker it returns an error

                              1 Reply Last reply Reply Quote 0
                              • Mario Pellegrini
                                Mario Pellegrini last edited by

                                Error:
                                AttributeError: 'NoneType' object has no attribute 'close'

                                for ENEL.MI, FCA.MI, UCG.MI, ....
                                maybe on Italian titles?

                                1 Reply Last reply Reply Quote 0
                                • B
                                  backtrader administrators last edited by

                                  Those tickers work. But you seem to be executing random pieces of code. The only obvious conclusion one could draw is that you have connection problems ... but it's a long shot.

                                  1 Reply Last reply Reply Quote 1
                                  • Mario Pellegrini
                                    Mario Pellegrini last edited by

                                    @Mario-Pellegrini said in float division by zero:

                                    from pandas_datareader import data,wb
                                    import datetime

                                    import backtrader as bt
                                    from datetime import datetime
                                    data = bt.feeds.Quandl(dataname='FCA.MI',
                                    fromdate = datetime(2017,1,1),
                                    todate = datetime(2018,12,31),
                                    buffered= True )

                                    class MyStrategy(bt.Strategy):
                                    def init(self):
                                    self.rsi = bt.indicators.RSI_SMA(self.data.close, period=14)
                                    def next(self):
                                    if not self.position:
                                    if self.rsi < 30:
                                    self.buy(size=100)
                                    else:
                                    if self.rsi > 70:
                                    self.sell(size=100)

                                    cerebro = bt.Cerebro()
                                    cerebro.addstrategy(MyStrategy)
                                    cerebro.adddata(data)
                                    cerebro.broker.setcash(10000)
                                    cerebro.run()
                                    cerebro.plot()

                                    does it work for you? to me, I repeat, it gives this error, when I execute "cerebro.run ()":
                                    AttributeError: 'NoneType' object has no attribute 'close'

                                    It is not a random code since you also say that the ticker works

                                    1 Reply Last reply Reply Quote 0
                                    • B
                                      backtrader administrators last edited by backtrader

                                      No, it cannot work for me, because that doesn't read from Yahoo. It tries

                                      @Mario-Pellegrini said in float division by zero:

                                      It is not a random code since you also say that the ticker works

                                      It is random, because you apparently want to read from Yahoo and apply Quandl. The code has also nothing to do with the code from the homepage (3 snippets) that directly reads data from Yahoo.

                                      Why don't you start from one of the scripts on the home page, change the ticker to FCA.MI and then slowly modify it? The link again for your reference

                                      • https://www.backtrader.com/home/helloalgotrading/
                                      1 Reply Last reply Reply Quote 0
                                      • 1 / 1
                                      • First post
                                        Last post
                                      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors