double PercentchangeBuy = (CurrentpriceBuy - Lowest)/(Lowest+DBL_MIN); // This causes the error double PercentchangeSell = (Highest - CurrentpriceSell)/(Highest+DBL_MIN); // This causes the error
Hello guys,
I am running into a zero divide error because my formula is based on the percentage change from the lowest/highest the last x bars and due to the nature of this it gives me a zero divide error. I am not sure how to solve this, if anybody could provide some insight it would be much appreciated.
My code is below.
use this sample below
//Use the function like this in your code when you divide //Example of usage; Division(xxx,xxx); //---- double Division(double numerator,double denominator) { if(denominator == 0) return(0); else return(numerator/denominator); }
Also, you may cast all the integer divisions (unsafe) in your code to floating-point divisions (safe).
void OnStart() { int a = 1; int b = 0; Print( a / b ); // zero divide error Print( a / (double)b ); // the result is infinity }
Or use division through a function
'
double safeDiv(double x, double y) { return (y != 0) ? x / y : (double)"nan"; }
While everybody is providing to solutions to "disable the division by 0", a better alternative is for the programmer
to find out why it's dividing by zero and prevent it, because this to me looks like an edge case/scenario that could affect
the strategy/script and should not happen at all.
While everybody is providing to solutions to "disable the division by 0", a better alternative is for the programmer
to find out why it's dividing by zero and prevent it, because this to me looks like an edge case/scenario that could affect
the strategy/script and should not happen at all.
most of us arent mathmaticians, so workarounds and patches are what we are stuck with, so if you can show a more reliable patch to either of the above, then please post it for us to see. Or post links where we can invent better coding/matmatical skills better suited for coding scenarios.
most of us arent mathmaticians, so workarounds and patches are what we are stuck with, so if you can show a more reliable patch to either of the above, then please post it for us to see. Or post links where we can invent better coding/matmatical skills better suited for coding scenarios.
is becoming zero and see if that could affect the logic, and IF it doesn't a simple MathMax, if condition or one of the patches posted here would solve it.
My issue is with the blind patching, OP doesn't know why something is becoming zero and how that is affecting the state of the entire script.
I am running into a zero divide error because my formula is based on the percentage change from the lowest/highest the last x bars and due to the nature of this it gives me a zero divide error. I am not sure how to solve this, if anybody could provide some insight it would be much appreciated.
Your code below is wrong and maybe returning 0 as the correct answer - iLowest/iHighest return an Integer index value not a double price.
Learn to check the proper use of called functions and use the returned values appropriately.
Make sure the terminal has the amount of data availabe that you are querying before you query it.
Catch potential zero divides before they happen.
double Lowest = iLowest(Symbol(),0,MODE_LOW,7,0); double Highest = iHighest(Symbol(),0,MODE_HIGH,7,0);

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello guys,
I am running into a zero divide error because my formula is based on the percentage change from the lowest/highest the last x bars and due to the nature of this it gives me a zero divide error. I am not sure how to solve this, if anybody could provide some insight it would be much appreciated.
My code is below.