zero devide

 

Hi,

Im running into a problem i can't figure out.

I try to make the Williams' percentage range indicator but the below part of the code gives a zero devide error all the time but when the /1 part is left out, the code works fine and the given values are according to the code. Alle the used arrays are doubles.

Does anybody has an idea of what's happening here?

thanks!


for(i=0;i<span-period_wpr;i++){
   one_wpr[i]=1/((one_price[ArrayMaximum(one_price,period_wpr,i)]-one_price[ArrayMinimum(one_price,period_wpr,i)]));
   }
for(i=0;i<span-period_wpr;i++){
   one_wpr[i]=((one_price[ArrayMaximum(one_price,period_wpr,i)]-one_price[ArrayMinimum(one_price,period_wpr,i)]));
   }
 

I suggest you find another way to code this. You never want to be dividing by any quantity that has any chance of being 0. I know that is not a direct answer to your question, but the fact is that if you find a more appropriate way to code this your problem will likely just go away.

 

I assume at the beginning there is only bar to be analyzed. In this case ArrayMinimum == ArrayMaximum is true, so ArrayMax-ArrayMin will be 0.

Possible solutions is to insert a if statement like:

for(i=0;i<span-period_wpr;i++){
   if( (one_price[ArrayMaximum(one_price,period_wpr,i)] != one_price[ArrayMinimum(one_price,period_wpr,i)]  ){
     one_wpr[i]=1/((one_price[ArrayMaximum(one_price,period_wpr,i)]-one_price[ArrayMinimum(one_price,period_wpr,i)]));
   }
}

If you strore the result of ArrayMax and ArrayMin instead of calling that functions twice you can also get nearly the same performance

 
zzuegg:

I assume at the beginning there is only bar to be analyzed. In this case ArrayMinimum == ArrayMaximum is true, so ArrayMax-ArrayMin will be 0.

Possible solutions is to insert a if statement like:

If you strore the result of ArrayMax and ArrayMin instead of calling that functions twice you can also get nearly the same performance


Thanks zzuegg! It's working now.
Reason: