Division by Zero - Odd errors in Daily time frame but not Weekly

I am confused as to why this code doesn't work in its simplest form but does in more complicated situations. This code is part of a (much larger) Commentary, that works except for this section. All this section of code is intended to show is what percentage of the period's total range, the close to low value equals.
Example High=250, Low=100, Close=125, therefore the Range =150, Close to Low (Bounce) = 25, which in this case would translate to percentage of the total range of 16.7% (variable named as Range_Ratio.)
I haven't included any code for the output as that all works fine, when the code runs and gets that far.

If I get the commentary to just output the H,L,C, Range and Bounce data, everything is correct.

The code immediately below fails with a 505 Division by zero error.

Bounce = (C-L);
Range = (H-L);
Range_Ratio = (Bounce / Range)*100;

However this code runs with no problems. The only change being the calculation for the Range.

Bounce = (C-L);
Range = ATR(1);
Range_Ratio = (Bounce / Range)*100;

If I want to see the Range_Ratio for the previous week (M to F), I have used the following code and that doesn't fail, it works perfectly, even though the calculation is the same style as the first set of code that does fail.

WH1=TimeFrameGetPrice("H",inWeekly,-1);

WL1=TimeFrameGetPrice("L",inWeekly,-1);

WC1=TimeFrameGetPrice("C",inWeekly,-1);

Bounce_W=(WC1-WL1);
Range_W=(WH1-WL1);
Range_Ratio_W=(Bounce_W/Range_W)*100;

If I use similar code for a new type of Daily (but using "inDaily" code to collect the H,L,C), it fails on 505 again.

H0=TimeFrameGetPrice("H",inDaily,0);

L0=TimeFrameGetPrice("L",inDaily,0);

C0=TimeFrameGetPrice("C",inDaily,0);

Bounce=(C0-L0);
Range=(H0-L0);
Range_Ratio=(Bounce/Range)*100;

If I change the Range=(H0-L0); to Range=ATR(1); it all runs fine.

I am at a loss as to why the simplest method fails and others work. If I hard code the data for just one day, overwriting the data from the array, I don't get any errors on that set of data. That would rather suggest that somehow the data from the array is becoming zero, at some point, but when it is shown as an output, it is all correct.

I am totally confused. Any suggestions, please?

Many thanks in advance.

You will get division by zero if denominator is zero. In your case Range variable is apparently zero (High is equal Low - doji).

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

Also it is advised to search the forum before creating a topic that is already answered multiple times: https://forum.amibroker.com/search?q=Division%20by%20zero

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.