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

Zero division error on developed indicators



  • Hi, I try to develop a new simple indicator about however zero division error araises (something related to the acc dist).
    I've checked the forum as well as the solution for the RSI, however I'm still short a little bit.
    Could someone advise?
    I've tried two approaches:

    1. Except: However the "ZeroDivisionError: float division by zero" persists.
        lines = ('accdist',)
        
        def __init__(self):
            super(Accdist, self).__init__()
            
            try:
                mfm = ((self.data.close - self.data.low) - (self.data.high - self.data.close))/(self.data.high - self.data.low)
            except ZeroDivisionError:
                mfm = 0.0
    
            mfv = mfm * self.data.volume  
            self.lines.accdist =  mfv + mfv(-1)```
    
    2. Conditional approach, however there is an issue with the object. 
    ```class Accdist(bt.Indicator):
        lines = ('accdist',)
    
        def __init__(self):
            super(Accdist, self).__init__()
            
            numerator = ((self.data.close - self.data.low) - (self.data.high - self.data.close))
            denominator = (self.data.high - self.data.low)
    
            zero = 0.0
            mfm = numerator / denominator if numerator else zero
     
            mfv = mfm * self.data.volume 
            
            self.lines.accdist =  mfv + mfv(-1)```
    
    Anyone can help me with a simple way to handle it.
    Many thanks
    v


  • Hello, I've found the issue with condition bt.If (in this nice threat: https://community.backtrader.com/topic/531/indicator-development-missing-a-key-piece/3), that's fine and I could not see the link. However, my issue still persists even if the condition works fine:
    ZeroDivisionError: float division by zero

    Anyone can explain it?

        lines = ('accdist', )
    
        def __init__(self):
            super(Accdist, self).__init__()
            
            numerator = ((self.data.close - self.data.low) - (self.data.high - self.data.close))
            denominator = (self.data.high - self.data.low)        
            mfm = bt.If(self.data.high > self.data.low, numerator/denominator, 0)         
            mfv = mfm * self.data.volume         
            self.lines.accdist =  mfv + mfv(-1)```
    
    Many thanks,
    v

  • administrators

    @vaclavku said in Zero division error on developed indicators:

    I've found the issue with condition bt.If (in this nice threat: https://community.backtrader.com/topic/531/indicator-development-missing-a-key-piece/3),

    I guess your intention is to say that you have found out how to address your issue with information from that thread.

    The issue at hand is that you are not managing the division by zero condition. You need DivByZero, which executes a division with parts (i.e.: lines) working inside backtrader and delivers a specific result if a division by zero happens.

    See here: https://community.backtrader.com/topic/1179/min-period-for-custom-stochasticrsi-indicator/4



  • Hello administor, you're right as always.
    And thanks for the clue about the DivByZero.


Log in to reply
 

});