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/

    Having trouble coding the Demark Indicator

    Indicators/Strategies/Analyzers
    1
    4
    77
    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.
    • Norbert Sithi
      Norbert Sithi last edited by

      Having some issues converting the Demarker indicator to backtrader.

      alt text
      alt text

      I am trying the following code:

      class Demarker(bt.Indicator):
          lines = ('demark',)
          params = (('period', 5),)
      
          def __init__(self):
              self.addminperiod(self.params.period)
              low_diff = self.data.low(-1) - self.data.low
              low_diff = bt.If(low_diff < 0, 0, low_diff)
              high_diff = self.data.high - self.data.high(-1)
              high_diff = bt.If(high_diff < 0, 0, high_diff)
              avg_max = bt.talib.SMA(high_diff, timeperiod=self.p.period)
              avg_min = bt.talib.SMA(low_diff, timeperiod=self.p.period)
              self.lines.demark = avg_max / (avg_max + avg_min)
      

      and I keep getting the following error.

      Traceback (most recent call last):
        File "C:/Users/Norbert/PycharmProjects/RR/main.py", line 278, in <module>
          runstrat()
        File "C:/Users/Norbert/PycharmProjects/RR/main.py", line 255, in runstrat
          cerebro.run()
        File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies
          self._runonce(runstrats)
        File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1652, in _runonce
          strat._once()
        File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\lineiterator.py", line 297, in _once
          indicator._once()
        File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\lineiterator.py", line 297, in _once
          indicator._once()
        File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\linebuffer.py", line 631, in _once
          self.once(self._minperiod, self.buflen())
        File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\linebuffer.py", line 755, in once
          self._once_op(start, end)
        File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\linebuffer.py", line 772, in _once_op
          dst[i] = op(srca[i], srcb[i])
      ZeroDivisionError: float division by zero
      
      Process finished with exit code 1
      
      

      Any help would be appreciated.

      Norbert Sithi 1 Reply Last reply Reply Quote 0
      • Norbert Sithi
        Norbert Sithi @Norbert Sithi last edited by

        @norbert-sithi

        Update on the issue:

        Changed the code a little:

        class Demarker(bt.Indicator):
            lines = ('demark',)
            params = (('period', 5),)
        
            def __init__(self):
                self.addminperiod(self.params.period)
            def __next__(self):
                low_diff = self.data.low[-1] - self.data.low[0]
                low_diff = bt.If(low_diff < 0, 0, low_diff)
                high_diff = self.data.high[0] - self.data.high[-1]
                high_diff = bt.If(high_diff < 0, 0, high_diff)
                avg_max = bt.indicators.SMA(high_diff, period=self.p.period)
                avg_min = bt.indicators.SMA(low_diff,  period=self.p.period)
                self.lines.demark[0] = avg_max / (avg_max + avg_min)
        

        Now I am getting this error:

        Traceback (most recent call last):
          File "C:/Users/Norbert/PycharmProjects/RR/main.py", line 280, in <module>
            runstrat()
          File "C:/Users/Norbert/PycharmProjects/RR/main.py", line 257, in runstrat
            cerebro.run()
          File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1127, in run
            runstrat = self.runstrategies(iterstrat)
          File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies
            self._runonce(runstrats)
          File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1652, in _runonce
            strat._once()
          File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\lineiterator.py", line 297, in _once
            indicator._once()
          File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\lineiterator.py", line 318, in _once
            self.once(self._minperiod, self.buflen())
          File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\talib.py", line 196, in once
            output = self._tafunc(*narrays, **self.p._getkwargs())
          File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\talib\__init__.py", line 27, in wrapper
            return func(*args, **kwargs)
          File "talib/_func.pxi", line 4525, in talib._ta_lib.SMA
          File "talib/_func.pxi", line 68, in talib._ta_lib.check_begidx1
        Exception: inputs are all NaN
        
        Process finished with exit code 1
        
        
        Norbert Sithi 1 Reply Last reply Reply Quote 0
        • Norbert Sithi
          Norbert Sithi @Norbert Sithi last edited by

          @norbert-sithi
          One more update:

          class Demarker(bt.Indicator):
              lines = ('demark',)
              params = (('period', 5),)
          
              def __init__(self):
                  self.addminperiod(self.p.period)
                  low_diff = bt.If((self.data.low(-1) - self.data.low(0)) < 0, 0, (self.data.low(-1) - self.data.low(0)))
                  high_diff = bt.If((self.data.high(0) - self.data.high(-1)) < 0, 0, (self.data.high(0) - self.data.high(-1)))
                  avg_max = bt.indicators.SMA(high_diff, period=self.p.period)
                  avg_min = bt.indicators.SMA(low_diff, period=self.p.period)
          
              def __next__(self):
                  self.lines.demark[0] = self.avg_max[0] / (self.avg_max[0] + self.avg_min[0])
          
          --------------------------------------------------
                            datetime     open     high      low    close
          0      2021-03-01 17:00:00  1.22396  1.22396  1.22373  1.22395
          1      2021-03-01 17:01:00  1.22387  1.22420  1.22385  1.22395
          2      2021-03-01 17:02:00  1.22396  1.22398  1.22382  1.22382
          3      2021-03-01 17:03:00  1.22383  1.22396  1.22376  1.22378
          4      2021-03-01 17:04:00  1.22378  1.22385  1.22296  1.22347
          ...                    ...      ...      ...      ...      ...
          399968 2022-01-31 23:54:00  1.12435  1.12440  1.12433  1.12440
          399969 2022-01-31 23:55:00  1.12442  1.12445  1.12441  1.12443
          399970 2022-01-31 23:56:00  1.12442  1.12447  1.12442  1.12445
          399971 2022-01-31 23:57:00  1.12444  1.12445  1.12433  1.12433
          399972 2022-01-31 23:58:00  1.12434  1.12434  1.12428  1.12431
          
          [399973 rows x 5 columns]
          --------------------------------------------------
          Starting Portfolio Value: 100000.00
          Traceback (most recent call last):
            File "C:/Users/Norbert/PycharmProjects/RR/main.py", line 278, in <module>
              runstrat()
            File "C:/Users/Norbert/PycharmProjects/RR/main.py", line 255, in runstrat
              cerebro.run(runonce=True)
            File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1127, in run
              runstrat = self.runstrategies(iterstrat)
            File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies
              self._runonce(runstrats)
            File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\cerebro.py", line 1652, in _runonce
              strat._once()
            File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\lineiterator.py", line 297, in _once
              indicator._once()
            File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\lineiterator.py", line 318, in _once
              self.once(self._minperiod, self.buflen())
            File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\backtrader\talib.py", line 196, in once
              output = self._tafunc(*narrays, **self.p._getkwargs())
            File "C:\Users\Norbert\anaconda3\envs\RR\lib\site-packages\talib\__init__.py", line 27, in wrapper
              return func(*args, **kwargs)
            File "talib/_func.pxi", line 4525, in talib._ta_lib.SMA
            File "talib/_func.pxi", line 68, in talib._ta_lib.check_begidx1
          Exception: inputs are all NaN
          
          Process finished with exit code 1
          
          
          Norbert Sithi 1 Reply Last reply Reply Quote 0
          • Norbert Sithi
            Norbert Sithi @Norbert Sithi last edited by

            @norbert-sithi Problem has been solved on further inspection of the docementation and use of the prenext() method

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