Divide by zero bug
-
EA works fine on live trading but I couldn't back test it. MT4 journal says :
2017.06.14 18:19:03.285 2017.04.18 05:05:00 ZigZag Strategy v1 USDCHF,M30: zero divide in 'ZigZag Strategy v1.mq4' (3696,105)I peeked into the EA code and found this at exactly 3969'th row.
else if (mode=="equityRisk") {size=((value/100)*AccountEquity())/(sl*((TickValue/ticksize)*point)*PipValue(symbol));}This is from here:
double DynamicLots(string mode="balance", double value=0, double sl=0, string align="align", double RJFR_initial_lots=0) { double size=0; string symbol=GetSymbol(); double LotStep=MarketInfo(symbol,MODE_LOTSTEP); double LotSize=MarketInfo(symbol,MODE_LOTSIZE); double MinLots=MarketInfo(symbol,MODE_MINLOT); double MaxLots=MarketInfo(symbol,MODE_MAXLOT); double TickValue=MarketInfo(symbol,MODE_TICKVALUE); double point=MarketInfo(symbol,MODE_POINT); double ticksize=MarketInfo(symbol,MODE_TICKSIZE); double margin_required=MarketInfo(symbol,MODE_MARGINREQUIRED); if (mode=="fixed" || mode=="lots") {size=value;} else if (mode=="block-equity") {size=(value/100)*AccountEquity()/margin_required;} else if (mode=="block-balance") {size=(value/100)*AccountBalance()/margin_required;} else if (mode=="block-freemargin") {size=(value/100)*AccountFreeMargin()/margin_required;} else if (mode=="equity") {size=(value/100)*AccountEquity()/(LotSize*TickValue);} else if (mode=="balance") {size=(value/100)*AccountBalance()/(LotSize*TickValue);} else if (mode=="freemargin") {size=(value/100)*AccountFreeMargin()/(LotSize*TickValue);} else if (mode=="equityRisk") {size=((value/100)*AccountEquity())/(sl*((TickValue/ticksize)*point)*PipValue(symbol));} else if (mode=="balanceRisk") {size=((value/100)*AccountBalance())/(sl*((TickValue/ticksize)*point)*PipValue(symbol));} else if (mode=="freemarginRisk") {size=((value/100)*AccountFreeMargin())/(sl*((TickValue/ticksize)*point)*PipValue(symbol));} else if (mode=="fixedRisk") {size=(value)/(sl*((TickValue/ticksize)*point)*PipValue(symbol));} else if (mode=="fixedRatio" || mode=="RJFR")Apparently, "sl" is set to 0 and then used as denominator on equity risk, balancerisk, freemarginrisk, and fixedrisk.
What is the fix for this? Should I put sl=0.0001 instead or what do I do? is this a fundamental flaw in the code? What should I do?
EDIT:
I turned off blocks and found that the error is coming from the BUY/SELL PENDING block. Kindly fix this asap.
-
Hello. The solution to this error is here: http://fxdreema.com/forum/topic/4712/how-to-fix-critical-runtime-error-503-in-ontick-function/5
-
With this particular option you must have some SL, because the lot size is calculated from SL. The bigger the SL, the lower the lot size. But with SL = 0 you can't calculate lot size... it should be infinity or something like that. Maybe you need some other MM.
-
@fxDreema My sl isn't zero. I noticed that whatever I sl i put, this line of code always appear. SL=0 is always there. try to check your codes. I even tried setting it to something else like 2 or 5 or 0.001 and still nothing.
-
When I put zero into Formula block EA stops too. Is it bug in EA or is it "mathematical bug" because it shouldn't divide by zero ?
-
Well, here is some small example - https://fxdreema.com/shared/O7I9C8GQc I don't have problems with this one

-
It is with Custom Price level - Candle - Parameter Candle High or Low - Candle ID 1, which means SL in the minimum or maximum of the previous candle.
-
@fxDreema It is with Custom Price level - Candle - Parameter Candle High or Low - Candle ID 1, which means SL in the minimum or maximum of the previous candle.
-
For those who still have the issue - there is something I realized: As this app runs in the cloud, sometime there might be hickups... like the connection is not stable enough and so on... this could lead to issues whilst the creation of some blocks. I had the same zero-divide issue and therefore I re-created the buy and sell blocks, afterwards they worked as supposed. of course the SL cannot be 0, but I was looking for the lowest or highes candle of 10 after an EMA cross, hence I knew that the SL was never 0. It could also have been an issue with the highest/lowest function, that provided maybe a 0 as SL - but this function was also part of the buy / sell block. So again, I deleted them and re-created them, and all of a sudded the EA worked as supposed - no zero-divide anymore. No functionallity was changed... sometimes that happens - was not the first time that it took me hours to find an issues and the last solutions was to re-create the block, just to find that it could have worked just fine. I blame it on the connections losses that appear sometimes and I make mayself aware of the blocks that I was creating during that time. ... maybe that solves your issues as well.
-
Another observation: if the SL is too close, you might get an spread issue ... like if the spread is bigger than you SL, than this might cause a zero-divide. Hence you might want to add the spread to you SL ...
-
@MMBInvest Thank you vermy much for sharing.