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 3 years ago by
deletedaccount051022.
-
-
01/12/2021 at 6:30 PM #157593
Thanks Grahal, I’ll play around with it, load a bunch of variations in demo and see if anything sticks.
1 user thanked author for this post.
06/29/2021 at 11:34 AM #172708@Monochrome or anybody … are you still using the Algo below?
I am getting a ‘negative paramater error message’ (new one from IG?? 🙁 ) anybody got a fix please?
Here’s the code if anybody can spot the source of the negative parameter please?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461//-------------------------------------------------------------------------// Main code : Dj-3min Dyn/super v1.2//-------------------------------------------------------------------------defparam cumulateorders = false//defparam preloadbars = 10000DEFPARAM FLATBEFORE = 233000DEFPARAM FLATAFTER = 210000daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0once positionsize = 0.2//DYNAMIC ZONE ELASTICITY///----/////once period = 200once smoothing = 2once 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)//DYNAMIC ZONE ELASTICITY///----/////once period2 = 20once smoothing2 = 2once smoothing2 = max(1, smoothing2+1)once period2 = max(1, Period2)// Coeficient, change if requiredonce coef2 = 0.8// Signal linex2 = std[Period2](close)signam2 = 1 + (-1* ( ((average[period2](close) + (x2*2)) - close) / x2 ) / 2)// Dynamic Zonessq2 = square(signam2)mean2 = average[period2](signam2)vsqrt2 = sqrt( average[period2](sq2) - square(mean2) )top2 = mean2 + coef2 * vsqrt2btm2 = mean2 - coef2 * vsqrt2result2 = average[smoothing2](signam2)////DYNAMIC ZONE ELASTICITY///----/////once period4 = 40once smoothing4 = 2once smoothing4 = max(1, smoothing4+1)once period4 = max(1, Period4)// Coeficient, change if requiredonce coef4 = 0.8// Signal linex4 = std[Period4](close)signam4 = 1 + (-1* ( ((average[period4](close) + (x4*2)) - close) / x4 ) / 2)// Dynamic Zonessq4 = square(signam4)mean4 = average[period4](signam4)vsqrt4 = sqrt( average[period4](sq4) - square(mean4) )top4 = mean4 + coef4 * vsqrt4btm4 = mean4 - coef4 * vsqrt4result4 = average[smoothing4](signam4)////DYNAMIC ZONE ELASTICITY///----/////once periodx = 80once smoothingx = 2once smoothingx = max(1, smoothingx+1)once periodx = max(1, Periodx)// Coeficient, change if requiredonce coefx = 0.8// Signal linexx = std[Periodx](close)signamx = 1 + (-1* ( ((average[periodx](close) + (xx*2)) - close) / xx ) / 2)// Dynamic Zonessqx = square(signamx)meanx = average[periodx](signamx)vsqrtx = sqrt( average[periodx](sqx) - square(meanx) )topx = meanx + coefx * vsqrtxbtmx = meanx - coefx * vsqrtxresultx = average[smoothingx](signamx)//bollinger//////bollup = BollingerUp[20](close)//bolldown = BollingerDown[20](close)//bollbuy = close< bollup//bollsell = close > bolldown//buyboll = bollbuy//sellboll = bollsell//STOCHRSI/////lengthRSI = 5//14 //RSI period//lengthStoch = 5//14 //Stochastic period//myRSI = RSI[lengthRSI](close)//MinRSI = lowest[lengthStoch](myrsi)//MaxRSI = highest[lengthStoch](myrsi)//StochRSI = (myRSI-MinRSI) / (MaxRSI-MinRSI)*100//////////////////min1stochbuy = stochrsi =>90//min1stochsell = stochrsi =<10//timeframe (2 minutes,UPDATEONCLOSE)////DYNAMIC ZONE ELASTICITY///----///////period4 = 20//smoothing4 = 2//once smoothing4 = max(1, smoothing4+1)//once period4 = max(1, Period4)//// Coeficient, change if required//once coef4 = 0.8//// Signal line//x4 = std[Period4](close)//signam4 = 1 + (-1* ( ((average[period4](close) + (x4*2)) - close) / x4 ) / 2)//// Dynamic Zones//sq4 = square(signam4)//mean4 = average[period4](signam4)//vsqrt4 = sqrt( average[period4](sq4) - square(mean4) )//top4 = mean4 + coef4 * vsqrt4//btm4 = mean4 - coef4 * vsqrt4//result4 = average[smoothing4](signam4)////////////////////IF RESULT CROSSES UNDER BTM THENSNAP = BARINDEXENDIFIF RESULT CROSSES OVER BTM THENSNAP = 0ENDIFIF RESULT < BTM THENH1 = LOWEST[BARINDEX-SNAP](RESULT)ELSEH1 = 0ENDIFIF RESULT < BTM THENH1A = RESULT > H1ELSEH1A = 0ENDIFIF RESULT CROSSES OVER TOP THENSNAP1 = BARINDEXENDIFIF RESULT CROSSES UNDER TOP THENSNAP1 = 0ENDIFIF RESULT > TOP THENH2 = HIGHEST[BARINDEX-SNAP1](RESULT)ELSEH2 = 0ENDIFIF RESULT > TOP THENH2A = RESULT < H2ELSEH2A = 0ENDIF//IF RESULT < BTM AND RESULT > H1 THEN//DONTBUY = 1//ELSE//DONTBUY = 0//ENDIFif result2 crosses over btm2 thentestbuysignal2 = 1testsellsignal2 = 0endifif result2 crosses under top2 thentestsellsignal2 = 1testbuysignal2 = 0endifif testsellsignal2 = 1 and result2 crosses over top2 thentestbuysignal2 = 1testsellsignal2 = 0endifif testbuysignal2 = 1 and result2 crosses under btm2 thentestbuysignal2 = 0testsellsignal2 = 1endifif result4 crosses over btm4 thentestbuysignal4 = 1testsellsignal4 = 0endifif result4 crosses under top4 thentestsellsignal4 = 1testbuysignal4 = 0endifif testsellsignal4 = 1 and result4 crosses over top4 thentestbuysignal4 = 1testsellsignal4 = 0endifif testbuysignal4 = 1 and result4 crosses under btm4 thentestbuysignal4 = 0testsellsignal4 = 1endifif resultX crosses over btmx thentestbuysignal4x = 1testsellsignal4x = 0endifif resultx crosses under topx thentestsellsignal4x = 1testbuysignal4x = 0endifif testsellsignal4x = 1 and resultx crosses over topx thentestsellsignal4x = 0testbuysignal4x = 1endifif testbuysignal4x = 1 and resultx crosses under btmx thentestbuysignal4x = 0testsellsignal4x = 1endif//HAV1 = TESTBUYSIGNAL2 = 1 AND TESTBUYSIGNAL4 = 1 AND TESTBUYSIGNAL4X = 1//HAV2 = RESULT > RESULT[1]//HAV3 = HAV1 AND HAV2IF RESULT CROSSES UNDER TOP THENSIGNAL1 = 1SIGNAL2 = 0ENDIFIF RESULT CROSSES OVER BTM THENSIGNAL2 = 1SIGNAL1 = 0ENDIFIF RESULT CROSSES OVER TOP THENSIGNAL1 = 0SIGNAL2 = 1ENDIFIF RESULT CROSSES UNDER BTM THENSIGNAL2 = 0SIGNAL1 = 1ENDIFif signal2 = 1 and result crosses under btm thensignal2 = 0signal1 = 1endifif signal1 = 1 and result crosses over top thensignal1 = 0signal2 = 1endifif signal1 = 1 and result crosses over mean thensignal2 = 1signal1 = 0endifif signal2 = 1 and result crosses under mean thensignal1 = 1signal2 = 0endif//graph signal1//graph signal2C1B = RESULT2 CROSSES OVER BTM2 //AND testbuysignal4 = 1C1S = RESULT2 CROSSES UNDER TOP2 //AND testsellsignal4 = 1C2B = RESULT4 CROSSES OVER BTM4 //AND testbuysignal4x = 1C2S = RESULT4 CROSSES UNDER TOP4 //AND testsellsignal4X = 1C3B = RESULTX CROSSES OVER BTMXC3S = RESULTX CROSSES UNDER TOPX/// === CAN ADD MORE PERIODStimeframe (15 minutes)//SUPERTREND//super15 = Supertrend[1,10]super25 = SUPERTREND[2,11]super35 = SUPERTREND[3,12]//ATRSTOP = AverageTrueRange[14](close)//ATRSTOP1 = ATRSTOP*AAtimeframe (default)//SUPERTREND//super1 = Supertrend[1,10]super2 = SUPERTREND[2,11]super3 = SUPERTREND[3,12]superbuy = close> super1 and close>super2 and close>super3supersell = close<super1 and close<super2 and close<super3superbuy15 = close> super15 and close>super25 and close>super35supersell15 = close<super15 and close<super25 and close<super35//testsuper1 = close > super1//testsuper2 = close< super1superbuyx = superbuy and superbuy15supersellx = supersell and supersell15abc1 = signal2 = 1 and not h2aabc2 = c1b or c2babc3 = abc2 or c3bb1 = abc1 and abc3 AND CLOSE>SUPER3 AND not supersellxasc1 = signal1 = 1 and not h1aasc2 = c1s or c2sasc3 = asc2 or c3ss1 = asc1 and asc3 AND CLOSE<SUPER3 AND not superbuyxtestsuperbuy = b1 and superselltestsupersell = s1 and superbuy//graph testsuperbuy//graph testsupersell//graph c1b//graph c2b//graph c3b//graph c1s//graph c2s//graph c3s//b1 = (SIGNAL2 = 1 AND (C1B OR C2B )) AND NOT H2A//s1 = (SIGNAL1 = 1 AND (C1S OR C2S )) AND NOT H1A//GRAPH testsellsignal4//GRAPH C2B//GRAPH C1B////GRAPH RESULT4////GRAPH TOP//GRAPH BTM4////GRAPH SIGNAL1////GRAPH SIGNAL2////GRAPH B1////GRAPH S1//SET STOP LOSS SATR*AverageTrueRange[NATR](close)//SET TARGET PROFIT PATR*AverageTrueRange[NATR](close)if b1 and not daysForbiddenEntry thenbuy POSITIONSIZE contract at market//SET STOP LOSS 3*AverageTrueRange[14](close)//SET TARGET PROFIT 1*AverageTrueRange[14](close)set stop %loss 0.2//ATRSTOP1//set target %profit 0.8endifif s1 and not daysForbiddenEntry thensellshort POSITIONSIZE contract at market//SET STOP LOSS 3*AverageTrueRange[14](close)//SET TARGET PROFIT 2*AverageTrueRange[14](close)set stop %loss 0.2 //ATRSTOP1//set target %profit 0.8endifIF LONGONMARKET AND CLOSE crosses under SUPER3 THENSELL AT MARKETENDIFIF SHORTONMARKET AND CLOSE crosses over SUPER3 THENEXITSHORT AT MARKETENDIF//IF LONGONMARKET AND SIGNAL1 = 1 AND TESTSELLSIGNAL2 = 1 AND CLOSE < SUPER1 THEN//SELL AT MARKET//ENDIF//IF SHORTONMARKET AND SIGNAL2 = 1 AND TESTBUYSIGNAL2 = 1 AND CLOSE> SUPER1 THEN//EXITSHORT AT MARKET//ENDIF//BREAKEVEN/////if not onmarket then//atrtest = AverageTrueRange[14](close[0])//endif////startBreakeven = atrtest//PointsToKeep = 5////IF NOT ONMARKET THEN//breakevenLevel=0//ENDIF////IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN//breakevenLevel = tradeprice(1)+PointsToKeep*pipsize//ENDIF//IF SHORTONMARKET AND tradeprice(1)-close>startBreakeven*pipsize THEN//breakevenLevel = tradeprice(1)-PointsToKeep*pipsize//ENDIF//if not superbuy then//IF breakevenLevel>0 THEN//SELL AT breakevenLevel STOP//ENDIF//endif//if not supersell then//IF breakevenLevel>0 THEN//EXITSHORT AT breakevenLevel STOP//ENDIF//endif//closesuper = close crosses under super1//closesuper1 = close crosses under super2//closesuper2 = close crosses under super3////closesuper3 = close crosses over super1//closesuper4 = close crosses over super2//closesuper5 = close crosses over super3////////close1 = closesuper or closesuper1//close2 = close1 or closesuper2////close3 = closesuper3 or closesuper4//close4 = close3 or closesuper5////if longonmarket and positionperf<0.1 and close2 then//sell at market//endif////if longonmarket and positionperf<0.1 and close4 then//exitshort at market//endif//if longonmarket and result crosses under btm then//sell at market//endif//if shortonmarket and result crosses over top then//exitshort at market//endif06/29/2021 at 11:52 AM #17270906/29/2021 at 12:00 PM #172712Funny you should ask, after my post above, I give it a critical eye and I ditched it in favour of trying None’s version from higher up this thread.
I use Algos different than most anyway … if a trade goes into early profit, I then monitor progress and exit manually at some point, rarely leaving any Algo to exit all on it’s own! 🙂
06/29/2021 at 12:02 PM #172713Try this at line 149:
1H2 = HIGHEST[max(1,BARINDEX-SNAP1)](RESULT)and this at line 132:
1H1 = LOWEST[max(1,BARINDEX-SNAP)](RESULT)2 users thanked author for this post.
07/01/2021 at 9:45 AM #172871Funny you should ask, after my post above, I give it a critical eye and I ditched it in favour of trying None’s version from higher up this thread.
Hey Grahal, thanks for reminding me about this one – could be worth another look. Here’s an fresh take on it, backtest looks good, but unfortunately when I loaded it up got the dreaded div/0 rejection almost immediately.
There’s nothing out of the ordinary apart from the DZE code, and the only division there is x.
I’ve tried fixing this at lines 61,62 but it doesn’t help.
If anyone has a better way of stopping x from ever being zero I’d love to hear it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170// Definition of code parametersDEFPARAM CumulateOrders = false // Cumulating positions deactivatedDEFPARAM preloadbars = 10000positionsize = 0.5Ctime = time >=143000 and time <210000once tradetype = 1 // [1] long/short [2]long [3]shortTIMEFRAME(10 minute)mx = average[a,t](typicalprice)c11 = mx > mx[1]mx1 = average[p3,t](typicalprice)c12 = mx1 < mx1[1]indicator2 = SuperTrend[m2,n2]indicator2a = SAR[q1,w1,e1]c13 = (close > indicator2) or (close > indicator2a)c14 = (close < indicator2) or (close < indicator2a)TIMEFRAME(5 minute)indicator1 = SuperTrend[m,n]indicator1a = SAR[q,w,e]c1 = (close > indicator1) or (close > indicator1a)c2 = (close < indicator1) or (close < indicator1a)mx2 = average[p1,t1](typicalprice)c5 = mx2 > mx2[1]c6 = mx2 < mx2[1]//Stochastic RSI | indicatorlengthRSI = lr //RSI periodlengthStoch = ls //Stochastic periodsmoothK = sk //Smooth signal of stochastic RSIsmoothD = sd //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[p2,t2](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)if x=0 thenx=x[1]endifsignam = 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 slSET TARGET %PROFIT tpENDIFendif// 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 slsSET TARGET %PROFIT tpsENDIFendif//trailing stop functiononce trailingstop = 1if trailingstop thentrailingpercentlong = tsl // %trailingpercentshort = tss // %stepPercentlong = stlstepPercentshort = ststssensitivity=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//EXIT ZOMBIE TRADEEZT = 1if EZT thenIF longonmarket and barindex-tradeindex(1)>= b1 and close<positionprice thensell at marketendifIF shortonmarket and barindex-tradeindex(1)>= b2 and close>positionprice thenexitshort at marketendifendif07/01/2021 at 9:53 AM #17287307/01/2021 at 9:32 PM #172917try deleting lines 61 – 63, change line 60 to
1x = (std[Period](close)) + 0.1it’s a dead simple solution but seems to work so far, opened a trade today with no rejections.
2 users thanked author for this post.
07/02/2021 at 1:41 PM #172975I had a divide by zero stoppage this morning so I am trying below at Line 61 to see if it stops the stoppages! 🙂
1x = max(1,std[Period](close))Here’s a thought, has anybody ever sent a Technical Report (on any Algo) to PRT asking “why is my Algo stopping and giving a divide by zeror error message”?
I’m really fed up with divide by zero errors generally … why can’t PRT put some script in the backtest engine to detect the conditions which give divide by zero errors??
07/02/2021 at 3:30 PM #172987Yeah, mine got stopped as well. I’m now running it with
1x = std[Period](close) + 0.3as adding more than that starts to impact performance. Hasn’t been rejected … yet.
Agreed about PRT, shouldn’t be too difficult for them to at least point out where the problem is.
07/03/2021 at 9:01 AM #173033The + 0.3 version took a trade yesterday. Also
1234x = std[Period](close)if x=0 thenx=1endifseems to work. I’m hoping this might be a generic solution for div/0 rejections, to be inserted wherever you’ve got a problematic division ???
Could be worth trying.
1 user thanked author for this post.
07/03/2021 at 9:54 AM #173034Yeah my idea below worked also and took a trade yesterday and ended in profit!
Below is effectively the same as your code above, i.e. if 0 read 1.
1x = max(1,std[Period](close))Our solutions make me think the divide by zero errors are mainly due to missing bars (bars with no price movement) and PRT must have some script in their backtest engine to produce the same result … if 0 read 1 ??
Would we really want backtests to keep stopping and throw up divide by zero errors??
Maybe we just live with / work around by adding our solution (if 0 read 1) to our strategy codes if / when we get divide by zero errors? I have been doing this for a while anyway.
Trouble is with complex / many lines of codes it is difficult to determine where the divide by zero error originates, but then it would be just as difficult if the error was thrown up in backtest?
On the strength of above discussion with myself 🙂 , I will not send a Contact Form to PRT for the divide by zero error … unless anybody has any other thoughts?
07/03/2021 at 10:02 AM #173036Yes nonetheless, it’s a nice solution as it won’t affect subsequent calculations.
1 user thanked author for this post.
07/03/2021 at 10:08 AM #173037GraHal, your solution can affect calculations as it will never allow divisions by 0.1…0.9 (or smaller decimals) or negative numbers, while nonetheless‘ will only change 0’s.
1 user thanked author for this post.
07/03/2021 at 10:35 AM #173038The Master has spoken, the None’s have it then! 🙂
I’ll change mine to None’s for next week!
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on