Hi,
When I was testing my code, then it suddenly stops and the following message pops up:
‘The trading system has stopped due to a divide by zero while checking the last candlestick. You can add protections to your code to prevent division by zero. Test this addition by backtesting the system first’
What are these ‘protections’?
There is no ‘Protections’, we have to make sure there is never a zero or negative value due to a division.
A common reason is due to an absence of a bar due to nil price change since last bar … this happens a lot of short duration Timeframes. I’ve had the problem loads! 🙁
If you want to post your code then maybe one of us may spot where you may get a divide by zero error.
Below may help where you have a divison in your code …
/min(1,xyz) //in place of /xyz
Hi,
It is about this piece of code:
(Close-Open)/(Open[1]-Close[1])>1
The first sum or the second sum can be zero
Might this work in some way, or at least spark further ideas for you?
If (Close <> Open AND Close[1] <> Open[1]) AND (Close-Open)/(Open[1]-Close[1])>1 Then
JSParticipant
Senior
Hi,
In addition to GraHal’s solution…
It’s a division by zero so it’s about the second term (Open[1]-Close[1])…
One solution may be… (don’t know if it’s the most elegant solution)
X / Y > 1
X = (Close – Open)
Y = (Open[1] – Close[1])
If Y = 0 then
Y = 1
EndIf
Instead of dividing by zero, you now divide by 1 what does the least harm…
Does ‘not working’ mean …
- not trading
OR
- still getting divide by zero error?
Hi JS,
I tested this solution:
X / Y > 1
X = (Close – Open)
Y = (Open[1] – Close[1])
If Y = 0 then
Y = 1
EndIf
but my code is part of a big ‘if’ statement so I assume I cannot use ‘endif’ in the middle of the big ‘if’ statement. Or am I wrong?
Hi Grahal,
I mean the I still get the ‘dividend by zero’ error message.
Try …
C1 = (Close–Open) > 1*(Open[1]–Close[1])
Change your formula with:
(Close-Open )/ max(ticksize,(Open[1]-Close[1])) > 1
to make sure you divide by a value at least equal to ticksize (which is not zero).