ProRealCode - Trading & Coding with ProRealTime™
Thanks Grahal, I’ll play around with it, load a bunch of variations in demo and see if anything sticks.
@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?
//-------------------------------------------------------------------------
// Main code : Dj-3min Dyn/super v1.2
//-------------------------------------------------------------------------
defparam cumulateorders = false
//defparam preloadbars = 10000
DEFPARAM FLATBEFORE = 233000
DEFPARAM FLATAFTER = 210000
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
once positionsize = 0.2
//DYNAMIC ZONE ELASTICITY///----/////
once period = 200
once 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)
//DYNAMIC ZONE ELASTICITY///----/////
once period2 = 20
once smoothing2 = 2
once smoothing2 = max(1, smoothing2+1)
once period2 = max(1, Period2)
// Coeficient, change if required
once coef2 = 0.8
// Signal line
x2 = std[Period2](close)
signam2 = 1 + (-1* ( ((average[period2](close) + (x2*2)) - close) / x2 ) / 2)
// Dynamic Zones
sq2 = square(signam2)
mean2 = average[period2](signam2)
vsqrt2 = sqrt( average[period2](sq2) - square(mean2) )
top2 = mean2 + coef2 * vsqrt2
btm2 = mean2 - coef2 * vsqrt2
result2 = average[smoothing2](signam2)
////DYNAMIC ZONE ELASTICITY///----/////
once period4 = 40
once 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)
////DYNAMIC ZONE ELASTICITY///----/////
once periodx = 80
once smoothingx = 2
once smoothingx = max(1, smoothingx+1)
once periodx = max(1, Periodx)
// Coeficient, change if required
once coefx = 0.8
// Signal line
xx = std[Periodx](close)
signamx = 1 + (-1* ( ((average[periodx](close) + (xx*2)) - close) / xx ) / 2)
// Dynamic Zones
sqx = square(signamx)
meanx = average[periodx](signamx)
vsqrtx = sqrt( average[periodx](sqx) - square(meanx) )
topx = meanx + coefx * vsqrtx
btmx = meanx - coefx * vsqrtx
resultx = 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 THEN
SNAP = BARINDEX
ENDIF
IF RESULT CROSSES OVER BTM THEN
SNAP = 0
ENDIF
IF RESULT < BTM THEN
H1 = LOWEST[BARINDEX-SNAP](RESULT)
ELSE
H1 = 0
ENDIF
IF RESULT < BTM THEN
H1A = RESULT > H1
ELSE
H1A = 0
ENDIF
IF RESULT CROSSES OVER TOP THEN
SNAP1 = BARINDEX
ENDIF
IF RESULT CROSSES UNDER TOP THEN
SNAP1 = 0
ENDIF
IF RESULT > TOP THEN
H2 = HIGHEST[BARINDEX-SNAP1](RESULT)
ELSE
H2 = 0
ENDIF
IF RESULT > TOP THEN
H2A = RESULT < H2
ELSE
H2A = 0
ENDIF
//IF RESULT < BTM AND RESULT > H1 THEN
//DONTBUY = 1
//ELSE
//DONTBUY = 0
//ENDIF
if result2 crosses over btm2 then
testbuysignal2 = 1
testsellsignal2 = 0
endif
if result2 crosses under top2 then
testsellsignal2 = 1
testbuysignal2 = 0
endif
if testsellsignal2 = 1 and result2 crosses over top2 then
testbuysignal2 = 1
testsellsignal2 = 0
endif
if testbuysignal2 = 1 and result2 crosses under btm2 then
testbuysignal2 = 0
testsellsignal2 = 1
endif
if result4 crosses over btm4 then
testbuysignal4 = 1
testsellsignal4 = 0
endif
if result4 crosses under top4 then
testsellsignal4 = 1
testbuysignal4 = 0
endif
if testsellsignal4 = 1 and result4 crosses over top4 then
testbuysignal4 = 1
testsellsignal4 = 0
endif
if testbuysignal4 = 1 and result4 crosses under btm4 then
testbuysignal4 = 0
testsellsignal4 = 1
endif
if resultX crosses over btmx then
testbuysignal4x = 1
testsellsignal4x = 0
endif
if resultx crosses under topx then
testsellsignal4x = 1
testbuysignal4x = 0
endif
if testsellsignal4x = 1 and resultx crosses over topx then
testsellsignal4x = 0
testbuysignal4x = 1
endif
if testbuysignal4x = 1 and resultx crosses under btmx then
testbuysignal4x = 0
testsellsignal4x = 1
endif
//HAV1 = TESTBUYSIGNAL2 = 1 AND TESTBUYSIGNAL4 = 1 AND TESTBUYSIGNAL4X = 1
//HAV2 = RESULT > RESULT[1]
//HAV3 = HAV1 AND HAV2
IF RESULT CROSSES UNDER TOP THEN
SIGNAL1 = 1
SIGNAL2 = 0
ENDIF
IF RESULT CROSSES OVER BTM THEN
SIGNAL2 = 1
SIGNAL1 = 0
ENDIF
IF RESULT CROSSES OVER TOP THEN
SIGNAL1 = 0
SIGNAL2 = 1
ENDIF
IF RESULT CROSSES UNDER BTM THEN
SIGNAL2 = 0
SIGNAL1 = 1
ENDIF
if signal2 = 1 and result crosses under btm then
signal2 = 0
signal1 = 1
endif
if signal1 = 1 and result crosses over top then
signal1 = 0
signal2 = 1
endif
if signal1 = 1 and result crosses over mean then
signal2 = 1
signal1 = 0
endif
if signal2 = 1 and result crosses under mean then
signal1 = 1
signal2 = 0
endif
//graph signal1
//graph signal2
C1B = RESULT2 CROSSES OVER BTM2 //AND testbuysignal4 = 1
C1S = RESULT2 CROSSES UNDER TOP2 //AND testsellsignal4 = 1
C2B = RESULT4 CROSSES OVER BTM4 //AND testbuysignal4x = 1
C2S = RESULT4 CROSSES UNDER TOP4 //AND testsellsignal4X = 1
C3B = RESULTX CROSSES OVER BTMX
C3S = RESULTX CROSSES UNDER TOPX
/// === CAN ADD MORE PERIODS
timeframe (15 minutes)
//SUPERTREND//
super15 = Supertrend[1,10]
super25 = SUPERTREND[2,11]
super35 = SUPERTREND[3,12]
//ATRSTOP = AverageTrueRange[14](close)
//ATRSTOP1 = ATRSTOP*AA
timeframe (default)
//SUPERTREND//
super1 = Supertrend[1,10]
super2 = SUPERTREND[2,11]
super3 = SUPERTREND[3,12]
superbuy = close> super1 and close>super2 and close>super3
supersell = close<super1 and close<super2 and close<super3
superbuy15 = close> super15 and close>super25 and close>super35
supersell15 = close<super15 and close<super25 and close<super35
//testsuper1 = close > super1
//testsuper2 = close< super1
superbuyx = superbuy and superbuy15
supersellx = supersell and supersell15
abc1 = signal2 = 1 and not h2a
abc2 = c1b or c2b
abc3 = abc2 or c3b
b1 = abc1 and abc3 AND CLOSE>SUPER3 AND not supersellx
asc1 = signal1 = 1 and not h1a
asc2 = c1s or c2s
asc3 = asc2 or c3s
s1 = asc1 and asc3 AND CLOSE<SUPER3 AND not superbuyx
testsuperbuy = b1 and supersell
testsupersell = 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 then
buy 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.8
endif
if s1 and not daysForbiddenEntry then
sellshort 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.8
endif
IF LONGONMARKET AND CLOSE crosses under SUPER3 THEN
SELL AT MARKET
ENDIF
IF SHORTONMARKET AND CLOSE crosses over SUPER3 THEN
EXITSHORT AT MARKET
ENDIF
//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
//endif
Do u find this algo intressting? Look at attached backtest
Funny 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! 🙂
Try this at line 149:
H2 = HIGHEST[max(1,BARINDEX-SNAP1)](RESULT)
and this at line 132:
H1 = LOWEST[max(1,BARINDEX-SNAP)](RESULT)
Funny 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
// Definition of code parameters
DEFPARAM CumulateOrders = false // Cumulating positions deactivated
DEFPARAM preloadbars = 10000
positionsize = 0.5
Ctime = time >=143000 and time <210000
once tradetype = 1 // [1] long/short [2]long [3]short
TIMEFRAME(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 | indicator
lengthRSI = lr //RSI period
lengthStoch = ls //Stochastic period
smoothK = sk //Smooth signal of stochastic RSI
smoothD = sd //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[p2,t2](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)
if x=0 then
x=x[1]
endif
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 sl
SET TARGET %PROFIT tp
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 sls
SET TARGET %PROFIT tps
ENDIF
endif
//trailing stop function
once trailingstop = 1
if trailingstop then
trailingpercentlong = tsl // %
trailingpercentshort = tss // %
stepPercentlong = stl
stepPercentshort = sts
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
//EXIT ZOMBIE TRADE
EZT = 1
if EZT then
IF longonmarket and barindex-tradeindex(1)>= b1 and close<positionprice then
sell at market
endif
IF shortonmarket and barindex-tradeindex(1)>= b2 and close>positionprice then
exitshort at market
endif
endif
try deleting lines 61 – 63, change line 60 to
x = (std[Period](close)) + 0.1
it’s a dead simple solution but seems to work so far, opened a trade today with no rejections.
I had a divide by zero stoppage this morning so I am trying below at Line 61 to see if it stops the stoppages! 🙂
x = 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??
Yeah, mine got stopped as well. I’m now running it with
x = std[Period](close) + 0.3
as 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.
The + 0.3 version took a trade yesterday. Also
x = std[Period](close)
if x=0 then
x=1
endif
seems 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.
Yeah 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.
x = 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?
Yes nonetheless, it’s a nice solution as it won’t affect subsequent calculations.
GraHal, 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.
The Master has spoken, the None’s have it then! 🙂
I’ll change mine to None’s for next week!
Dynamic Zone Elasticity, division by zero
This topic contains 57 replies,
has 10 voices, and was last updated by deletedaccount051022
4 years, 6 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 01/08/2021 |
| Status: | Active |
| Attachments: | 14 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.