Divide by zero protections
Forums › ProRealTime English forum › ProRealTime platform support › Divide by zero protections
- This topic has 12 replies, 4 voices, and was last updated 1 year ago by Nicolas.
-
-
06/12/2023 at 8:12 PM #216028
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’?
06/12/2023 at 8:47 PM #216030There 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
06/12/2023 at 8:54 PM #216031Hi,
It is about this piece of code:
(Close-Open)/(Open[1]-Close[1])>1
The first sum or the second sum can be zero
06/12/2023 at 9:22 PM #21603306/12/2023 at 9:29 PM #216034Hi,
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…
1 user thanked author for this post.
06/13/2023 at 8:09 PM #216168Hi Grahal,
I tested
1If (Close <> Open AND Close[1] <> Open[1]) AND (Close–Open)/(Open[1]–Close[1])>1 Thenbut it is not working
06/13/2023 at 8:12 PM #21616906/13/2023 at 8:14 PM #216170Hi 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?
06/13/2023 at 8:19 PM #216171Hi Grahal,
I mean the I still get the ‘dividend by zero’ error message.
06/13/2023 at 8:20 PM #21617206/13/2023 at 8:24 PM #21617306/13/2023 at 8:27 PM #21617506/14/2023 at 10:12 AM #216203 -
AuthorPosts