For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

How can I create an indicator and ignore div by 0 error in data?

  • I am trying to create an indicator and have some div by 0 error. Can you replace the div by 0 with 0 in the indicator?
    For some minute data, the high and low is the same. I will get a div by 0 for these minute bars.

    Also How could I do a Moving average of 15 minutes of f1 divided by the 15 total of price*volume?
    I am wondering how could I do this in a way like lambda


    import backtrader as bt
    import backtrader.feeds as btfeed
    class customCSV(btfeed.GenericCSVData):
        params = (
            ('dtformat', '%Y-%m-%d %H:%M:%S'),
            ('datetime', 0),
            ('open', 1),
            ('high', 2),
            ('low', 3),
            ('close', 4),
            ('volume', 5),
            ('openinterest', -1)
    class SmaCross(bt.SignalStrategy):
        params = (('pfast', 10), ('pslow', 30),)
        def __init__(self):
   **( /2 
    cerebro = bt.Cerebro()
    data = customCSV(dataname='C:\\Test\\SPY.csv')

  • Your question has two parts.
    Part 1: Try this out. = bt.If(
    ( **( /2 

    Part 2:
    Have a look at this page and see if this helps.

  • thanks, run-out!
    I did try your code for part 1. But I still get the same error. Can you try ur code on this csv?

    Here is the error:

    Traceback (most recent call last):
      File "C:/Users/XXX/PycharmProjects/TD/", line 41, in <module>
      File "C:\Users\XXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\backtrader\", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "C:\Users\XXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\backtrader\", line 1293, in runstrategies
      File "C:\Users\XXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\backtrader\", line 1652, in _runonce
      File "C:\Users\XXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\backtrader\", line 297, in _once
      File "C:\Users\XXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\backtrader\", line 631, in _once
        self.once(self._minperiod, self.buflen())
      File "C:\Users\XXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\backtrader\", line 755, in once
        self._once_op(start, end)
      File "C:\Users\XXX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\backtrader\", line 772, in _once_op
        dst[i] = op(srca[i], srcb[i])
    ZeroDivisionError: float division by zero

  • Please take a look at the following post. Quoting some code from there:

    class BollingerBandsPct(BollingerBands):
        Extends the Bollinger Bands with a Percentage line
        lines = ('pctb',)
        params = (('safediv', False), ('safezero', 0.0))
        plotlines = dict(pctb=dict(_name='%B'))  # display the line as %B on chart
        def __init__(self):
            super(BollingerBandsPct, self).__init__()
            if not self.p.safediv:
                self.l.pctb = ( - / ( -
                self.l.pctb = DivByZero( -, -, self.p.safezero)
    class BollingerBandsPct_SafeDiv(BollingerBandsPct):
        params = (('safediv', True),)

    Also the following doc may be relevant:

  • @vladisld Thanks for your reply!
    I tried the MFI code here and the indicator is calculated and displayed.

    After I make minor changes, the indicator is not displaying anymore. But the code did run and plot is fine except no indicator. This is really weird. I have not tried the moving average yet.

    class MoneyFlowIndex(bt.Indicator):
        lines = ('mfi',)
        params = (
            ('period', 14),
        def init(self):
            money_flow_raw = ( -
            money_flow_net = ( -
            mfiratio = bt.ind.DivByZero(money_flow_net, money_flow_raw, zero=0.5)*50+50
            self.l.mfi =  mfiratio
    cerebro = bt.Cerebro()
    data = customCSV(dataname='E:\\SPY.csv')
    myind = MoneyFlowIndex(data)
    myind.plotinfo.subplot = True
    cerebro.add_signal(bt.SIGNAL_LONGSHORT, MoneyFlowIndex)

  • I find out that I forget to use init method to initialize. Sorry

Log in to reply