Division by zero Stopping Trading System
Forums › ProRealTime English forum › ProOrder support › Division by zero Stopping Trading System
- This topic has 9 replies, 6 voices, and was last updated 2 years ago by Dollar.
-
-
07/18/2020 at 12:37 PM #139550
Hi, this system code below is causing a “Division by Zero” system stop out issue twice since being launched last week.
Is anyone able to find the line of code that is causing the problem? It tested fine and well on back test (Wall Street DFB).
Cheers, much appreciated for any help.
Renko Machine Learner123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451//-------------------------------------------------------------------------//Main code : Bard Renko ML3 pLoss (CLR)(i)//Cycle Limit + Resetperiod//First Fully Working ML3 System//Long Boxsize = ValueX//Short Boxsize = ValueY//Point Loss = ValueZ//Heuristic = 3. Check//ElsIf HeuristicsAlgo3 = 1 Then -- Check//Corrected to Mode3 in ML3 -- Check//https://www.prorealcode.com/topic/machine-learning-in-proorder/page/3/#post-121130//-------------------------------------------------------------------------//https://www.prorealcode.com/topic/why-is-backtesting-so-unreliable/#post-110889//definition of code parametersdefparam cumulateorders = false // cumulating positions deactivateddefparam preloadbars = 1000//Money Management//Capital = 10000 + strategyprofit //Current profit made by the closed trades of the running strategy.N = 1//10*Capital/Close //30*Capital/Close //30//Settings 1 & 2/////////////////////////////////////////////////////////////startingvalue = 50 //50 20 40 5, 100, 10 //boxsizemaxvalue = 250 //250 50 20, 300, 150 //maximum allowed valueincrement = 20 //10, 5, 20,minvalue = 20 //15, minimum allowed valuemaxincrement = 10 //20, 5, 10 limit of no of increments either up or downreps = 2 //1 number of trades to use for analysis //2ResetPeriod = 0.5 //0.5 = same as if no Reset system. Specify no of months to reset optimisation/////////////////////////////////////////////////////////////startingvalue2 = 50 //50 20 40 5, 100, 10 //boxsizemaxvalue2 = 250 //250 20, 300, 150 //maximum allowed valueincrement2 = 20 //20, 5 10minvalue2 = 20 //20 15, minimum allowed valuemaxincrement2 = 10 //5, 10 limit of no of increments either up or downreps2 = 2 //2, 1 number of trades to use for analysis //2ResetPeriod2 = 0.5 //0.5 Specify no of months to reset optimisation/////////////////////////////////////////////////////////////startingvalue3 = 50 //50, 5, 100, 10 // pLossmaxvalue3 = 100 //100 20, 300, 150 //maximum allowed valueincrement3 = 5 //5, 20, 10minvalue3 = 5 //5, minimum allowed valuemaxincrement3 = 8 //8, 5, 10 limit of no of increments either up or downreps3 = 2 //2, 1 number of trades to use for analysis //2ResetPeriod3 = 0.5 //0.5 Specify no of months to reset optimisation/////////////////////////////////////////////////////////////HeuristicsCycleLimit = 3once HeuristicsCycle = 0once HeuristicsAlgo1 = 1once HeuristicsAlgo2 = 0once HeuristicsAlgo3 = 0If HeuristicsCycle >= HeuristicsCycleLimit ThenIf HeuristicsAlgo1 = 1 ThenHeuristicsAlgo1 = 0HeuristicsAlgo2 = 1HeuristicsAlgo3 = 0ElsIf HeuristicsAlgo2 = 1 ThenHeuristicsAlgo1 = 0HeuristicsAlgo2 = 0HeuristicsAlgo3 = 1ElsIf HeuristicsAlgo3 = 1 ThenHeuristicsAlgo1 = 1HeuristicsAlgo2 = 0HeuristicsAlgo3 = 0EndIfHeuristicsCycle = 0EndIfIf HeuristicsAlgo1 = 1 Then//heuristics algorithm 1 startif (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) thenoptimise = optimise + 1endifonce monthinit = monthonce yearinit = yearIf (year = yearinit and month = (monthinit + ResetPeriod)) or (year = (yearinit + 1) and ((12 - monthinit) + month = ResetPeriod)) ThenValueX = StartingValueWinCountB = 0StratAvgB = 0BestA = 0BestB = 0monthinit = monthyearinit = yearEndIfonce valuex = startingvalueonce pincpos = 1 //positive increment positiononce nincpos = 1 //negative increment positiononce optimise = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)once mode1 = 1 //switches between negative and positive increments//once wincountb = 3 //initialize best win count//graph wincountb coloured (0,0,0) as "wincountb"//once stratavgb = 4353 //initialize best avg strategy profit//graph stratavgb coloured (0,0,0) as "stratavgb"if optimise = reps thenwincounta = 0 //initialize current win countstratavga = 0 //initialize current avg strategy profitheuristicscycle = heuristicscycle + 1for i = 1 to reps doif positionperf(i) > 0 thenwincounta = wincounta + 1 //increment current wincountendifstratavga = stratavga + (((positionperf(i)*countofposition[i]*close)*-1)*-1)nextstratavga = stratavga/reps //calculate current avg strategy profit//graph (positionperf(1)*countofposition[1]*Close)*-1 as "posperf1"//graph (positionperf(2)*countofposition[2]*Close)*-1 as "posperf2"//graph stratavga*-1 as "stratavga"//once besta = 300//graph besta coloured (0,0,0) as "besta"if stratavga >= stratavgb thenstratavgb = stratavga //update best strategy profitbesta = valuexendif//once bestb = 300//graph bestb coloured (0,0,0) as "bestb"if wincounta >= wincountb thenwincountb = wincounta //update best win countbestb = valuexendifif wincounta > wincountb and stratavga > stratavgb thenmode1 = 0elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 1 thenvaluex = valuex - (increment*nincpos)nincpos = nincpos + 1mode1 = 2elsif wincounta >= wincountb or stratavga >= stratavgb and mode1 = 1 thenvaluex = valuex + (increment*pincpos)pincpos = pincpos + 1mode1 = 1elsif wincounta < wincountb and stratavga < stratavgb and mode1 = 2 thenvaluex = valuex + (increment*pincpos)pincpos = pincpos + 1mode1 = 1elsif wincounta >= wincountb or stratavga >= stratavgb and mode1 = 2 thenvaluex = valuex - (increment*nincpos)nincpos = nincpos + 1mode1 = 2endifif nincpos > maxincrement or pincpos > maxincrement thenif besta = bestb thenvaluex = bestaelseif reps >= 10 thenweightedscore = 10elseweightedscore = round((reps/100)*100)endifvaluex = round(((besta*(20-weightedscore)) + (bestb*weightedscore))/20) //lower reps = less weight assigned to win%endifnincpos = 1pincpos = 1elsif valuex > maxvalue thenvaluex = maxvalueelsif valuex < minvalue thenvaluex = minvalueendifoptimise = 0endif// heuristics algorithm 1 endelsif heuristicsalgo2 = 1 then// heuristics algorithm 2 startif (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) thenoptimise2 = optimise2 + 1endifonce monthinit2 = monthonce yearinit2 = yearIf (year = yearinit2 and month = (monthinit2 + ResetPeriod2)) or (year = (yearinit2 + 1) and ((12 - monthinit2) + month = ResetPeriod2)) ThenValueY = StartingValue2WinCountB2 = 0StratAvgB2 = 0BestA2 = 0BestB2 = 0monthinit2 = monthyearinit2 = yearEndIfonce valuey = startingvalue2once pincpos2 = 1 //positive increment2 positiononce nincpos2 = 1 //negative increment2 positiononce optimise2 = 0 //initialize heuristicks engine counter (must be incremented at position start or exit)once mode2 = 1 //switches between negative and positive increments//once wincountb2 = 3 //initialize best win count//graph wincountb2 coloured (0,0,0) as "wincountb2"//once stratavgb2 = 4353 //initialize best avg strategy profit//graph stratavgb2 coloured (0,0,0) as "stratavgb2"if optimise2 = reps2 thenwincounta2 = 0 //initialize current win countstratavga2 = 0 //initialize current avg strategy profitheuristicscycle = heuristicscycle + 1for i2 = 1 to reps2 doif positionperf(i2) > 0 thenwincounta2 = wincounta2 + 1 //increment current wincountendifstratavga2 = stratavga2 + (((positionperf(i2)*countofposition[i2]*close)*-1)*-1)nextstratavga2 = stratavga2/reps2 //calculate current avg strategy profit//graph (positionperf(1)*countofposition[1]*Close)*-1 as "posperf1-2"//graph (positionperf(2)*countofposition[2]*Close)*-1 as "posperf2-2"//graph stratavga2*-1 as "stratavga2"//once besta2 = 300//graph besta2 coloured (0,0,0) as "besta2"if stratavga2 >= stratavgb2 thenstratavgb2 = stratavga2 //update best strategy profitbesta2 = valueyendif//once bestb2 = 300//graph bestb2 coloured (0,0,0) as "bestb2"if wincounta2 >= wincountb2 thenwincountb2 = wincounta2 //update best win countbestb2 = valueyendifif wincounta2 > wincountb2 and stratavga2 > stratavgb2 thenmode2 = 0elsif wincounta2 < wincountb2 and stratavga2 < stratavgb2 and mode2 = 1 thenvaluey = valuey - (increment2*nincpos2)nincpos2 = nincpos2 + 1mode2 = 2elsif wincounta2 >= wincountb2 or stratavga2 >= stratavgb2 and mode2 = 1 thenvaluey = valuey + (increment2*pincpos2)pincpos2 = pincpos2 + 1mode2 = 1elsif wincounta2 < wincountb2 and stratavga2 < stratavgb2 and mode2 = 2 thenvaluey = valuey + (increment2*pincpos2)pincpos2 = pincpos2 + 1mode2 = 1elsif wincounta2 >= wincountb2 or stratavga2 >= stratavgb2 and mode2 = 2 thenvaluey = valuey - (increment2*nincpos2)nincpos2 = nincpos2 + 1mode2 = 2endifif nincpos2 > maxincrement2 or pincpos2 > maxincrement2 thenif besta2 = bestb2 thenvaluey = besta2elseif reps2 >= 10 thenweightedscore2 = 10elseweightedscore2 = round((reps2/100)*100)endifvaluey = round(((besta2*(20-weightedscore2)) + (bestb2*weightedscore2))/20) //lower reps = less weight assigned to win%endifnincpos2 = 1pincpos2 = 1elsif valuey > maxvalue2 thenvaluey = maxvalue2elsif valuey < minvalue2 thenvaluey = minvalue2endifoptimise2 = 0endif// heuristics algorithm 2 end//endifelsif heuristicsalgo3 = 1 then// Heuristics Algorithm 3 Start//If HeuristicsAlgo3 = 1 ThenIf (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) Thenoptimise3 = optimise3 + 1EndIfonce monthinit3 = monthonce yearinit3 = yearIf (year = yearinit3 and month = (monthinit3 + ResetPeriod3)) or (year = (yearinit3 + 1) and ((12 - monthinit3) + month = ResetPeriod3)) ThenValueZ = StartingValue3WinCountB3 = 0StratAvgB3 = 0BestA3 = 0BestB3 = 0monthinit3 = monthyearinit3 = yearEndIfonce ValueZ = StartingValue3 // N = £/Tickonce PIncPos3 = 1 //Positive increment2 Positiononce NIncPos3 = 1 //Negative increment2 Positiononce optimise3 = 0 //Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)once Mode3 = 1 //Switches between negative and positive increments//once WinCountB3 = 3 //Initialize Best Win Count//GRAPH WinCountB3 coloured (0,0,0) AS "WinCountB2"//once StratAvgB3 = 4353 //Initialize Best Avg St3ategy Profit//GRAPH StratAvgB3 coloured (0,0,0) AS "StratAvgB3"If optimise3 = Reps3 ThenWinCountA3 = 0 //Initialize current Win CountStratAvgA3 = 0 //Initialize current Avg Strategy ProfitHeuristicsCycle = HeuristicsCycle + 1For i3 = 1 to Reps3 DoIf positionperf(i3) > 0 ThenWinCountA3 = WinCountA3 + 1 //Increment Current WinCountEndIfStratAvgA3 = StratAvgA3 + (((PositionPerf(i3)*countofposition[i3]*Close)*-1)*-1)NextStratAvgA3 = StratAvgA3/Reps3 //Calculate Current Avg Strategy Profit//Graph (PositionPerf(1)*countofposition[1]*Close)*-1 as "PosPerf1-2"//Graph (PositionPerf(2)*countofposition[2]*Close)*-1 as "PosPerf2-2"///////////// 3?//Graph StratAvgA3*-1 as "StratAvgA3"//once BestA3 = 300//GRAPH BestA3 coloured (0,0,0) AS "BestA3"If StratAvgA3 >= StratAvgB3 ThenStratAvgB3 = StratAvgA3 //Update Best Strategy ProfitBestA3 = ValueZEndIf//once BestB3 = 300//GRAPH BestB3 coloured (0,0,0) AS "BestB3"If WinCountA3 >= WinCountB3 ThenWinCountB3 = WinCountA3 //Update Best Win CountBestB3 = ValueZEndIfIf WinCountA3 > WinCountB3 and StratAvgA3 > StratAvgB3 ThenMode3 = 0 //Strategy is performing well with current variable, do not optimiseElsIf WinCountA3 < WinCountB3 and StratAvgA3 < StratAvgB3 and Mode3 = 2 ThenValueZ = ValueZ + (Increment3*PIncPos3)PIncPos3 = PIncPos3 + 1Mode3 = 1 //For strategy performing worse Next cycle optimise with positive incrementsElsIf WinCountA3 < WinCountB3 and StratAvgA3 < StratAvgB3 and Mode3 = 1 ThenValueZ = ValueZ - (Increment3*NIncPos3)NIncPos3 = NIncPos3 + 1Mode3 = 2 //For strategy performing worse Next cycle optimise with negative incrementsElsIf WinCountA3 >= WinCountB3 or StratAvgA3 >= StratAvgB3 and Mode3 = 1 ThenValueZ = ValueZ + (Increment3*PIncPos3)PIncPos3 = PIncPos3 + 1Mode3 = 1 //For strategy performing better Next cycle again optimise with positive incrementsElsIf WinCountA3 >= WinCountB3 or StratAvgA3 >= StratAvgB3 and Mode3 = 2 ThenValueZ = ValueZ - (Increment3*NIncPos3)NIncPos3 = NIncPos3 + 1Mode3 = 2 //For strategy performing better Next cycle again optimise with negative incrementsEndIfIf NIncPos3 > MaxIncrement3 or PIncPos > MaxIncrement3 ThenIf BestA3 = BestB3 ThenValueZ = BestA3ElseIf reps3 >= 10 ThenWeightedScore3 = 10ElseWeightedScore3 = round((reps3/100)*100)EndIfValueZ = round(((BestA3*(20-WeightedScore3)) + (BestB3*WeightedScore3))/20) //Lower Reps = Less weight assigned to Win%EndIfNIncPos3 = 1PIncPos3 = 1ElsIf ValueZ > MaxValue3 ThenValueZ = MaxValue3ElsIf ValueZ < MinValue3 ThenValueZ = MinValue3EndIFoptimise3 = 0EndIf// Heuristics Algorithm 3 EndEndif//BoxSizeL = ValueXBoxSizeS = ValueY//renkomaxl = round(close / boxsizel) * boxsizelrenkominl = renkomaxl - boxsizelrenkomaxs = round(close / boxsizes) * boxsizesrenkomins = renkomaxs - boxsizes//if high > renkomaxl + boxsizel thenrenkomaxl = renkomaxl + boxsizelrenkominl = renkominl + boxsizelendifif low < renkomins - boxsizes thenrenkomaxs = renkomaxs - boxsizesoptimisationrenkomins = renkomins - boxsizesendif// Conditions to enter long positionsBuy N CONTRACT at renkoMaxL + boxSizeL stop// Conditions to enter short positionsSellshort N CONTRACT at renkoMinS - boxSizeS stop//if percentage then//set stop %loss 0.5//set target %profit 2//elseSET STOP pLOSS ValueZSET TARGET PPROFIT 500//endif////graphonprice renkomaxl + boxsizel coloured(0,200,0) as "renkomax"//graphonprice renkomins - boxsizes coloured(200,0,0) as "renkomin"graph ValueX coloured(0,255,0)graph ValueY coloured(255,0,0)graph ValueZ coloured(0,0,255)07/18/2020 at 1:04 PM #139552What TF are you running?
The error messages (divide by zero etc) may not always be accurate.
If you are running on 1 sec TF then maybe the code above occasionally does not fully execute in 1 second and so the divide by zero error code is thrown up?
Unless I can quickly spot the cause (for divide by zero etc) I generally start the System back up again and think … “life is too short … I’m not going to get frustrated looking for a problem that may not even exist in the code !!??” 🙂
PS
Good to see you back … I thought you may have moved on to pastures new! 🙂
07/18/2020 at 2:22 PM #139560Hey GraHal, cheers, likewise and thanks for the help, right, it’s a daily TF and it was stopped out within 24 hours of putting it back up after the first stopped out the day before.
You know when Juanj said (regarding his ML post topic), words to the effect that this was just “the tip of the iceberg,” well, because I had to take time off from sitting at the desk and 50″ trading screen because of an old back injury flare up (compressed nerve), I went and listened to a lot of Kraftwerk, Stranglers and The Undertones and read everything I could on ML. Please read this phd thesis paper, it will blow your mind:
The pdf download link is in blue, top right.
I’m learning to use RapidMiner presently but Gabri (Gabriele) is saying that Python is pretty good too. The hardest and critical part of ML is data preparation. I’m using Excel, and have already programmed all the indicators used in the research paper but would like to add more complex indicators like ones by John Ehlers, so I posted an “Excel collaboration” idea in the forums: https://www.prorealcode.com/topic/collaboration-to-code-indicators-into-excel-file/
I’m also thinking of maybe getting either a Trading View subscription or Interactive Brokers account because they both let you download O/H/L/C/V and indicator values to Excel which you’ll need. The idea is that the you’ll need to de-trend the noise out of the time series and use volatility data too. Luke Rose’s research paper only used generic indicators though but got incredible 30 and 200 day trend prediction results using Random Forests. Then again he was using the Nasdaq stocks which go up generally, I want to see how accurate it is for mean reverting currencies.
Let me know what you think?
07/27/2020 at 6:31 PM #14023207/27/2020 at 7:18 PM #140237Try GRAPHing
- optimise
- optimise2
- optimise3
- heuristicscycle
I didn’t, but at first glance it looks like some of them could be the problem.
1 user thanked author for this post.
07/28/2020 at 7:28 AM #140254I made a simple test that shows back test cannot alert your strategy has division zero issue, you can refer to here, https://www.prorealcode.com/topic/proorder-exception-line-number/#post-140253
On the same post, I also shared my experience to pay attention to the timing when PRT notify you the division zero error, i.e. the date and time, and use it as your input for debugging. In your case, maybe you can graph all the division and pay attention which variable give you zero on the particular timing or nearby timing in case some delay in sending the notification, but at least it narrow down your debugging range.
1 user thanked author for this post.
07/28/2020 at 10:05 AM #140281Hi I’m not exactly a pro coder and it may be something to do with the insert PRT code but every time you use “increment” or “maxincrement” it sees the “rem” and turns it into a remark.
The earliest lines of this is lines 33 and 35.
I’ve not used anything like that in my codes so not sure if that’s what happens in the real PRT.
1 user thanked author for this post.
07/30/2020 at 9:28 PM #140521Cheers @robertogozzi !
I’ll take a look this weekend.07/31/2020 at 12:04 AM #14052908/15/2022 at 3:39 PM #199087The division by zero may occur on lines 412 or 415
Passing the both lines as remarks as below, you will avoid the division by zero. But the results will be slightly different as you will be in position for longer periods.
412 //renkomaxl <span class=”crayon-o”>=</span> <span class=”crayon-st”>round</span>(<span class=”crayon-st”>close</span> <span class=”crayon-o”>/</span> boxsizel) <span class=”crayon-o”>*</span> boxsizel
415 //renkomaxs <span class=”crayon-o”>=</span> <span class=”crayon-st”>round</span>(<span class=”crayon-st”>close</span> <span class=”crayon-o”>/</span> boxsizes) <span class=”crayon-o”>*</span> boxsizes
-
AuthorPosts
Find exclusive trading pro-tools on