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
-
Having some issues converting the Demarker indicator to backtrader.
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.
-
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
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 Problem has been solved on further inspection of the docementation and use of the prenext() method