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/

    Zero division error on developed indicators

    Indicators/Strategies/Analyzers
    2
    4
    502
    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.
    • V
      vaclavku last edited by

      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
      1 Reply Last reply Reply Quote 0
      • V
        vaclavku last edited by

        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
        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by backtrader

          @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

          1 Reply Last reply Reply Quote 0
          • V
            vaclavku last edited by

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

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors