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



  • 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()



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


  • administrators

    @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.


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



  • @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()


  • administrators

    @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.


  • administrators

    @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.



  • @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()



  • @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?



  • @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()```


  • 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()
    

  • administrators

    @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.



  • 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



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

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


  • administrators

    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.



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


  • administrators

    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


Log in to reply
 

});