I’ve got an algo in demo using this indicator which takes trades but gets stopped every few days for division by zero. Everything else is pieces of code I’ve been using for ages so I assume the problem is with Dynamic Zone Elasticity. Anyone else with experience of this? Shall I post the whole algo?
//Dynamic Zone Elasticity
Period = 20
smoothing = 2
// ----------------
once smoothing = max(1, smoothing+1)
once period = max(1, Period)
// Coeficient, change if required
once coef = 0.8
// Signal line
x = std[Period](close)
signam = 1 + (-1* ( ((average[period](close) + (x*2)) - close) / x ) / 2)
// Dynamic Zones
sq = square(signam)
mean = average[period](signam)
vsqrt = sqrt( average[period](sq) - square(mean) )
top = mean + coef * vsqrt
btm = mean - coef * vsqrt
result = average[smoothing](signam)
c3 = result crosses over btm
c4 = result crosses under top
The only possible division by a zero value is contained at line 12 with the variable x. However, it should not be a zero value. What is the timeframe used please?
Thanks Nicolas, it runs on a 1 minute TF. This is the whole thing, if that helps:
DEFPARAM CumulateOrders = false // Cumulating positions deactivated
DEFPARAM preloadbars = 5000
//Money Management NAS
MM = 1 // = 0 for optimization
if MM = 0 then
positionsize=1
ENDIF
if MM = 1 then
ONCE startpositionsize = 1
ONCE factor = 8 // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etc
ONCE margin = (close*.005) // tier 1 margin value of 1 contract in instrument currency; change decimal according to available leverage
ONCE margin2 = (close*.01)// tier 2 margin value of 1 contract in instrument currency; change decimal according to available leverage
ONCE tier1 = 200 // IG first tier margin limit
ONCE maxpositionsize = 2000 // IG tier 2 margin limit
ONCE minpositionsize = 1 // enter minimum position allowed
IF StrategyProfit <> StrategyProfit[1] THEN
positionsize = startpositionsize + Strategyprofit/(factor*margin)
ENDIF
IF StrategyProfit <> StrategyProfit[1] THEN
IF startpositionsize + Strategyprofit/(factor*margin) > tier1 then
positionsize = (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 //incorporating tier 2 margin
ENDIF
IF StrategyProfit <> StrategyProfit[1] THEN
if startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THEN
positionsize = minpositionsize //keeps positionsize from going below allowed minimum
ENDIF
IF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 > maxpositionsize then
positionsize = maxpositionsize// keeps positionsize from going above IG tier 2 margin limit
ENDIF
ENDIF
ENDIF
ENDIF
Ctime = time >=143000 and time <210000
once tradetype = 1 // [1] long/short [2]long [3]short
TIMEFRAME(10 minute)
mx1 = average[78,8](close)
c11 = mx1 > mx1[1]
c12 = mx1 < mx1[1]
indicator2 = SuperTrend[1.5,10]
indicator2a = SAR[0.005,0.005,0.01]
c13 = (close > indicator2) or (close > indicator2a)
c14 = (close < indicator2) or (close < indicator2a)
TIMEFRAME(5 minute)
indicator1 = SuperTrend[3,7]
indicator1a = SAR[0.01,0.005,0.025]
c1 = (close > indicator1) or (close > indicator1a)
c2 = (close < indicator1) or (close < indicator1a)
mx2 = average[19,8](typicalprice)
c5 = mx2 > mx2[1]
c6 = mx2 < mx2[1]
//Stochastic RSI | indicator
lengthRSI = 6 //RSI period
lengthStoch = 3 //Stochastic period
smoothK = 6 //Smooth signal of stochastic RSI
smoothD = 5 //Smooth signal of smoothed stochastic RSI
myRSI = RSI[lengthRSI](close)
MinRSI = lowest[lengthStoch](myrsi)
MaxRSI = highest[lengthStoch](myrsi)
StochRSI = (myRSI-MinRSI) / (MaxRSI-MinRSI)
K = average[smoothK](stochrsi)*100
D = average[smoothD](K)
c7 = K>D
c8 = K<D
TIMEFRAME(default)
mx3 = average[6,5](typicalprice)
c9 = mx3 > mx3[1]
c10 = mx3 < mx3[1]
//Dynamic Zone Elasticity
Period = 20
smoothing = 2
// ----------------
once smoothing = max(1, smoothing+1)
once period = max(1, Period)
// Coeficient, change if required
once coef = 0.8
// Signal line
x = std[Period](close)
signam = 1 + (-1* ( ((average[period](close) + (x*2)) - close) / x ) / 2)
// Dynamic Zones
sq = square(signam)
mean = average[period](signam)
vsqrt = sqrt( average[period](sq) - square(mean) )
top = mean + coef * vsqrt
btm = mean - coef * vsqrt
result = average[smoothing](signam)
c3 = result crosses over btm
c4 = result crosses under top
// Conditions to enter long positions
if tradetype=1 or tradetype=2 then
IF Ctime and c1 and c3 and c5 and c7 and c9 and c11 and c13 THEN
BUY positionsize CONTRACT AT MARKET
SET STOP %LOSS 0.7
SET TARGET %PROFIT 1.2
ENDIF
endif
// Conditions to enter short positions
if tradetype=1 or tradetype=3 then
IF Ctime and c2 and c4 and c6 and c8 and c10 and c12 and c14 THEN
SELLSHORT positionsize CONTRACT AT MARKET
SET STOP %LOSS 1
SET TARGET %PROFIT 1.1
ENDIF
endif
//trailing stop function
once trailingstop = 1
if trailingstop then
trailingpercentlong = 0.15// %
trailingpercentshort = 0.16 // %
stepPercentlong = 0.001
stepPercentshort = 0.005
tssensitivity=2 // 1 = close 2 = H,L 3 = l,H 4 = typicalprice
if onmarket then
trailingstartlong = tradeprice(1)*(trailingpercentlong/100) //trailing will start @trailingstart points profit
trailingstartshort = tradeprice(1)*(trailingpercentshort/100) //trailing will start @trailingstart points profit
trailingsteplong = tradeprice(1)*(stepPercentlong/100) //% step to move the stoploss
trailingstepshort = tradeprice(1)*(stepPercentshort/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
if tssensitivity=1 then
tssensitivitylong=close
tssensitivityshort=close
elsif tssensitivity=2 then
tssensitivitylong=high
tssensitivityshort=low
elsif tssensitivity=3 then
tssensitivitylong=low
tssensitivityshort=high
elsif tssensitivity=4 then
tssensitivitylong=typicalprice
tssensitivityshort=typicalprice
endif
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tssensitivitylong-tradeprice(1)>=trailingstartlong THEN
newSL = tradeprice(1)+trailingsteplong
ENDIF
//next moves
IF newSL>0 AND tssensitivitylong-newSL>trailingsteplong THEN
newSL = newSL+trailingsteplong
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-tssensitivityshort>=trailingstartshort THEN
newSL = tradeprice(1)-trailingstepshort
ENDIF
//next moves
IF newSL>0 AND newSL-tssensitivityshort>trailingstepshort THEN
newSL = newSL-trailingstepshort
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
endif
//****************************************************************************************
IF longonmarket and barindex-tradeindex>1220 and close<positionprice then
sell at market
endif
IF shortonmarket and barindex-tradeindex>1370 and close>positionprice then
exitshort at market
endif
I’m also using it in a 2 min algo with a similar combination of indicators where it runs with no problem.
I had the same problem with this indicator. I have 2 strategies based on it, one works fine but the other gets the zero division error. I can only assume the problem is not with the indicator.
Try putting
ONCE period and ONCE smoothing
@Monochrome, it’s made 2 trades today with that change – so far so good. thanks again.
No problem. I too have tried a similar strategy with supertrend. I have shelved it long back and moved on, hoping to revisit it sometime again. It stills gets the zero division error but i think its due to the <> and = use.
This strategy uses no optimisation, (I personally hate optimisation). I like its entries but needs work on the exits.
I share this here (code is very messy). I think dynamic zone elasticity/rsi/stochastic has potential for shorttime frames.
spoke too soon, got stopped again today, div by zero. so frustrating.
I’ve read every thread on this subject and tried all the advice i could find. if anyone has any other bright suggestions I’d be most grateful.
Like Nicolas said, if x is the problem, maybe it could be a solution :
// Signal line
x = std[Period](close)
if x=0 then
x=x[1]
endif
But there could be some impacts on the p&l
thanks Roger, nice idea that could work for the div by zero, but unfortunately causes a 30% drop in performance. 🤔
I think it might be a issue with use of MTF. My ones that dont use mtf seems to run fine. The one I shared above uses some mtf and gets zero error.
Maybe?
hard to see how that should result in this particular error though. I’ve been through the code countless times, line by line – nothing stands out that could repeatedly produce a division by zero. Doesn’t make sense. Shame because the backtest looks quite good.
Does it give the division by zero error regularly or randomly?
Random meaning the Algo takes a few trades then threws up the error?
It’s random. Can be good for 2 or 3 days, take several trades, then gets the error.
The IG / PRT error messages are not perfect (when the roulette wheel stops turning) and I can’t help thinking that maybe it is not a divide by zero error, but maybe just a zero error??
What if a 1 min bar had zero typicalprice?
Would be worth trying below (at Line 72) to see if makes any difference?
OR GRAPH TypicalPrice = 0 (on 1 min TF) and see if it ever is zero?
Just a few thoughts.
mx3 = max (0.01, average[6,5](typicalprice))
Or even GRAPH average[6,5](typicalprice) = 0.
If 1 bar is zero on 1 min TF would the average of 6 bars = 0 or would the average use the other 5 bars only with a value > 0
More thoughts!? 🙂