Dynamic Zone Elasticity, division by zero
Forums › ProRealTime English forum › ProOrder support › Dynamic Zone Elasticity, division by zero
- This topic has 57 replies, 10 voices, and was last updated 2 years ago by deletedaccount051022.
-
-
01/08/2021 at 10:06 AM #156865
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?
123456789101112131415161718192021222324//Dynamic Zone ElasticityPeriod = 20smoothing = 2// ----------------once smoothing = max(1, smoothing+1)once period = max(1, Period)// Coeficient, change if requiredonce coef = 0.8// Signal linex = std[Period](close)signam = 1 + (-1* ( ((average[period](close) + (x*2)) - close) / x ) / 2)// Dynamic Zonessq = square(signam)mean = average[period](signam)vsqrt = sqrt( average[period](sq) - square(mean) )top = mean + coef * vsqrtbtm = mean - coef * vsqrtresult = average[smoothing](signam)c3 = result crosses over btmc4 = result crosses under top01/08/2021 at 11:06 AM #15688001/08/2021 at 11:20 AM #156885Thanks Nicolas, it runs on a 1 minute TF. This is the whole thing, if that helps:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189DEFPARAM CumulateOrders = false // Cumulating positions deactivatedDEFPARAM preloadbars = 5000//Money Management NASMM = 1 // = 0 for optimizationif MM = 0 thenpositionsize=1ENDIFif MM = 1 thenONCE startpositionsize = 1ONCE factor = 8 // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etcONCE margin = (close*.005) // tier 1 margin value of 1 contract in instrument currency; change decimal according to available leverageONCE margin2 = (close*.01)// tier 2 margin value of 1 contract in instrument currency; change decimal according to available leverageONCE tier1 = 200 // IG first tier margin limitONCE maxpositionsize = 2000 // IG tier 2 margin limitONCE minpositionsize = 1 // enter minimum position allowedIF StrategyProfit <> StrategyProfit[1] THENpositionsize = startpositionsize + Strategyprofit/(factor*margin)ENDIFIF StrategyProfit <> StrategyProfit[1] THENIF startpositionsize + Strategyprofit/(factor*margin) > tier1 thenpositionsize = (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 //incorporating tier 2 marginENDIFIF StrategyProfit <> StrategyProfit[1] THENif startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THENpositionsize = minpositionsize //keeps positionsize from going below allowed minimumENDIFIF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 > maxpositionsize thenpositionsize = maxpositionsize// keeps positionsize from going above IG tier 2 margin limitENDIFENDIFENDIFENDIFCtime = time >=143000 and time <210000once tradetype = 1 // [1] long/short [2]long [3]shortTIMEFRAME(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 | indicatorlengthRSI = 6 //RSI periodlengthStoch = 3 //Stochastic periodsmoothK = 6 //Smooth signal of stochastic RSIsmoothD = 5 //Smooth signal of smoothed stochastic RSImyRSI = RSI[lengthRSI](close)MinRSI = lowest[lengthStoch](myrsi)MaxRSI = highest[lengthStoch](myrsi)StochRSI = (myRSI-MinRSI) / (MaxRSI-MinRSI)K = average[smoothK](stochrsi)*100D = average[smoothD](K)c7 = K>Dc8 = K<DTIMEFRAME(default)mx3 = average[6,5](typicalprice)c9 = mx3 > mx3[1]c10 = mx3 < mx3[1]//Dynamic Zone ElasticityPeriod = 20smoothing = 2// ----------------once smoothing = max(1, smoothing+1)once period = max(1, Period)// Coeficient, change if requiredonce coef = 0.8// Signal linex = std[Period](close)signam = 1 + (-1* ( ((average[period](close) + (x*2)) - close) / x ) / 2)// Dynamic Zonessq = square(signam)mean = average[period](signam)vsqrt = sqrt( average[period](sq) - square(mean) )top = mean + coef * vsqrtbtm = mean - coef * vsqrtresult = average[smoothing](signam)c3 = result crosses over btmc4 = result crosses under top// Conditions to enter long positionsif tradetype=1 or tradetype=2 thenIF Ctime and c1 and c3 and c5 and c7 and c9 and c11 and c13 THENBUY positionsize CONTRACT AT MARKETSET STOP %LOSS 0.7SET TARGET %PROFIT 1.2ENDIFendif// Conditions to enter short positionsif tradetype=1 or tradetype=3 thenIF Ctime and c2 and c4 and c6 and c8 and c10 and c12 and c14 THENSELLSHORT positionsize CONTRACT AT MARKETSET STOP %LOSS 1SET TARGET %PROFIT 1.1ENDIFendif//trailing stop functiononce trailingstop = 1if trailingstop thentrailingpercentlong = 0.15// %trailingpercentshort = 0.16 // %stepPercentlong = 0.001stepPercentshort = 0.005tssensitivity=2 // 1 = close 2 = H,L 3 = l,H 4 = typicalpriceif onmarket thentrailingstartlong = tradeprice(1)*(trailingpercentlong/100) //trailing will start @trailingstart points profittrailingstartshort = tradeprice(1)*(trailingpercentshort/100) //trailing will start @trailingstart points profittrailingsteplong = tradeprice(1)*(stepPercentlong/100) //% step to move the stoplosstrailingstepshort = tradeprice(1)*(stepPercentshort/100) //% step to move the stoplossendif//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIFif tssensitivity=1 thentssensitivitylong=closetssensitivityshort=closeelsif tssensitivity=2 thentssensitivitylong=hightssensitivityshort=lowelsif tssensitivity=3 thentssensitivitylong=lowtssensitivityshort=highelsif tssensitivity=4 thentssensitivitylong=typicalpricetssensitivityshort=typicalpriceendif//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND tssensitivitylong-tradeprice(1)>=trailingstartlong THENnewSL = tradeprice(1)+trailingsteplongENDIF//next movesIF newSL>0 AND tssensitivitylong-newSL>trailingsteplong THENnewSL = newSL+trailingsteplongENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-tssensitivityshort>=trailingstartshort THENnewSL = tradeprice(1)-trailingstepshortENDIF//next movesIF newSL>0 AND newSL-tssensitivityshort>trailingstepshort THENnewSL = newSL-trailingstepshortENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFendif//****************************************************************************************IF longonmarket and barindex-tradeindex>1220 and close<positionprice thensell at marketendifIF shortonmarket and barindex-tradeindex>1370 and close>positionprice thenexitshort at marketendif01/08/2021 at 11:24 AM #156886I’m also using it in a 2 min algo with a similar combination of indicators where it runs with no problem.
01/08/2021 at 12:05 PM #156889I 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
1 user thanked author for this post.
01/08/2021 at 6:35 PM #156975@Monochrome, it’s made 2 trades today with that change – so far so good. thanks again.
01/08/2021 at 6:59 PM #156980No 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.
3 users thanked author for this post.
01/11/2021 at 6:42 PM #157384spoke 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.
01/11/2021 at 6:56 PM #15738701/11/2021 at 7:54 PM #157402thanks Roger, nice idea that could work for the div by zero, but unfortunately causes a 30% drop in performance. 🤔
01/11/2021 at 9:38 PM #157422I 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?
01/11/2021 at 10:37 PM #157426hard 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.
01/11/2021 at 10:52 PM #15742701/12/2021 at 8:55 AM #157448It’s random. Can be good for 2 or 3 days, take several trades, then gets the error.
1 user thanked author for this post.
01/12/2021 at 1:16 PM #157531The 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.
1mx3 = 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!? 🙂
-
AuthorPosts