Anyone else get the dreaded div by zero for this? Apart from the indicators, the only lines I can see with division by anything are
averecentmael=close-(maerec/nn)
averecentmfel=close+(mferec/nn)
averecentmaes=close+(maerec/n)
averecentmfes=close-(mferec/n)
but n and nn have permanent values.
🤔🤔🤔???
PaulParticipant
Master
always that error!
It’s either in that section or in the vectorial part.
How about adding before
if range[0]>0 and range[1]>0 and range[2]>0 and range[3]>0 and range[4]>0 and range[5]>0 then
maybe up to 8/9/10, to include vectorial. Not very nice but it makes sure when the calculation starts every bar has a range.
Otherwise use min(x,x) for certain parts?
So, where would i add that line exactly? at the top of the //mfe & mae section?
PaulParticipant
Master
Something like this,
I thought I tried it in the past and in that situation it didn’t work, but I’ve got no other solution.
vectorial seems ok and also Stochastic RSI
ps you get check the time of the error and see if in the chart test=1
TIMEFRAME(default)
// mfe & mae
if range[0]>0 and range[1]>0 and range[2]>0 and range[3]>0 and range[4]>0 then
n = 4
nn= 1
if method=1 then
maerec=0
mferec=0
for i=1 to n
maerec=maerec+(high[i]-open[i])
mferec=mferec+(open[i]-low[i])
next
for j=1 to nn
maerec=maerec+(open[j]-low[j])
mferec=mferec+(high[j]-open[j])
next
averecentmael=close-(maerec/nn)
averecentmfel=close+(mferec/nn)
averecentmaes=close+(maerec/n)
averecentmfes=close-(mferec/n)
avgl=(averecentmael+averecentmfel)/2
avgs=(averecentmaes+averecentmfes)/2
elsif method=2 then
maerec=0
mferec=0
maerec2=0
mferec2=0
for i=1 to n
maerec=maerec+(high[i]-open[i])
mferec=mferec+(open[i]-low[i])
next
for j=1 to nn
maerec2=maerec2+(open[j]-low[j])
mferec2=mferec2+(high[j]-open[j])
next
averecentmael=close-(maerec2/nn)
averecentmfel=close+(mferec2/nn)
averecentmaes=close+(maerec/n)
averecentmfes=close-(mferec/n)
avgl=(averecentmael+averecentmfel)
avgs=(averecentmaes+averecentmfes)
endif
endif
if range[0]=0 or range[1]=0 or range[2]=0 or range[3]=0 or range[4]=0 then
test=1
else
test=0
endif
graph test
Hi guys!
What is this error? I dn’t see it when I backtest! How does it affects the code?
it doesn’t show up in backtest, that is what’s so infuriating. Just when you think you’ve done the hard work and everything is sweet, you launch it but it wont run live because ‘division by zero’. They don’t tell you where the problem is and it’s almost never apparent in the code because the values are changing all the time. Try doing a search, there’s dozens of threads on the subject but I don’t think anyone’s found a cure.
For me it is definitely worse in v11. Algos that ran fine in 10.3 are now getting stopped for this regularly.
Yeah, ive had a bunch of div 0 and similar errors recently. Very frustrating but a bit of hard work and help from the forum has solved them all.
Fact is there will be something attempting to div/0 and a well placed MAX(<something above zero that doesn’t break your system>, <offending code>) will fix most of them.
graph all your variables/returning values, find the zero and it should be possible to fix.
You can also try using If variable <> 0 then <do dodgy code> Endifs also.
its a pain but it is what it is.
PaulParticipant
Master
I don’t think it’s the solution above/below works though in my post, because it only solves to have a value before dividing it. And the error appears to be the other way around.
Problem is a zero is never used as far I can tell.
Anyway another way to have it programmed, instead testing and coding each bar individually, like this below. Should only take trades if the last xx bars have range and not have open=close=high=low.
// mfe & mae
count = 0
For zz = 1 to xx do
IF range[zz]=0 THEN
count= 1
ENDIF
next
if count=0 then
n = 4
nn= 1
wild guess, but what if with live data feed i.e. there’s no tick/bar data is not the same as the backtest data?
Something like backtestdata has open=low=high=close and running live the bar is simply skipped or the other way around.
Maybe such bar is processed another way but not visible and maybe the error is broader then the error message itself gives indication.
Thanks Paul, I’ll try that and see if it runs 🤞
@nonetheless, can you please upload a .itf file? As I don’t see the error I’d like to see what’s the difference… thanks 😉
Have you change something regarding the previous version? I don’t see any changes!
at the moment I have it in demo with Paul’s suggestion above but hasn’t taken a trade yet. When you say, ‘I don’t see the error’, do you mean you dont get division by zero rejection?
Yes exactly! Can you upload the version with the implementation of Paul’s suggestion?
it’s in my demo account that’s not open just now. but it’s exactly the same as v4.5 atr but add
count = 0
For zz = 1 to 5 do
IF range[zz]=0 THEN
count= 1
ENDIF
next
if count=0 then
under the // mfe & mae section, with another endif at the bottom. There’s no other changes.