For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Testing Stochastic with SafeDiv not working
-
Hey there,
Can anyone test this indicator I made up to include double smoothing of stochastic but with SafeDiv on?
Apparently it doesn't work on my data as I always get and error for float division by zero.
class Stochastic_DoubleSmooth(bt.Indicator): ''' Doesn't need to follow the OHLC standard naming and it includes the SafeDiv ''' lines = ('k', 'd', 'OB', 'OS') params = (('period',14),('pd',3),('pdslow',2),('OB',71), ('movav',bt.indicators.SMA),('slowav',None), ('safediv', True), ('safezero', 0.0)) def __init__(self): # Get highest from period k from 1st data highesthigh = bt.ind.Highest(self.data.high, period=self.p.period) # Get lowest from period k from 2nd data lowestlow = bt.ind.Lowest(self.data.low, period=self.p.period) #SafeDiv not working??? knum = self.data.close - lowestlow kden = highesthigh - lowestlow if self.p.safediv: kraw = 100.0 * bt.DivByZero(knum, kden, zero=self.p.safezero) else: kraw = 100.0 * (knum / kden) # The standard k in the indicator is a smoothed versin of K self.l.k = k = self.p.movav(kraw, period=self.p.pd) # Smooth k => d slowav = self.p.slowav or self.p.movav # chose slowav self.l.d = slowav(k, period=self.p.pdslow) self.l.OB = OB = (self.l.d / self.l.d) * self.p.OB self.l.OS = OS = ((self.l.d / self.l.d) * 100) - self.l.OB
This is the error I get.
Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-9dfd15ad4cb9>", line 1, in <module> runfile('/Users/frankie/PycharmProjects/PycharmProjectsShared/TradingProject/StrategyScripts/CAStrategy/CA_Macd_Stoch_Backtesting.py', wdir='/Users/frankie/PycharmProjects/PycharmProjectsShared/TradingProject/StrategyScripts/CAStrategy') File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "User/Fc/PycharmProjects/PycharmProjectsShared/TradingProject/StrategyScripts/Strategy/Macd_Stoch_Backtesting.py", line 411, in <module> Cerebro = cerebro.run(tradehistory=True) File "/usr/local/lib/python3.9/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/usr/local/lib/python3.9/site-packages/backtrader/cerebro.py", line 1293, in runstrategies self._runonce(runstrats) File "/usr/local/lib/python3.9/site-packages/backtrader/cerebro.py", line 1652, in _runonce strat._once() File "/usr/local/lib/python3.9/site-packages/backtrader/lineiterator.py", line 297, in _once indicator._once() File "/usr/local/lib/python3.9/site-packages/backtrader/lineiterator.py", line 297, in _once indicator._once() File "/usr/local/lib/python3.9/site-packages/backtrader/linebuffer.py", line 631, in _once self.once(self._minperiod, self.buflen()) File "/usr/local/lib/python3.9/site-packages/backtrader/linebuffer.py", line 755, in once self._once_op(start, end) File "/usr/local/lib/python3.9/site-packages/backtrader/linebuffer.py", line 772, in _once_op dst[i] = op(srca[i], srcb[i]) ZeroDivisionError: float division by zero
I really can't figure it out!!
-
@frankiec Forgot to post Custom MACD
from __future__ import (absolute_import, division, print_function, unicode_literals) import backtrader as bt from backtrader.indicators import EMA class MACD(bt.Indicator): lines = ('macd', 'signal', 'histo', 'zero', 'delta') params = (('period_me1', 12), ('period_me2', 26), ('period_signal', 9)) def __init__(self): me1 = EMA(self.data, period=self.p.period_me1) me2 = EMA(self.data, period=self.p.period_me2) self.l.macd = macd = me1 - me2 self.l.signal = signal = EMA(self.l.macd, period=self.p.period_signal) self.l.delta = macd - signal self.l.histo = self.l.macd - self.l.signal self.l.zero = me1 - me1
-
Solved!
The problem was in init(self):
self.l.OB = OB = (self.l.d / self.l.d) * self.p.OB self.l.OS = OS = ((self.l.d / self.l.d) * 100) - self.l.OB
Added aline of code to have an integer as a line.