# Problem with division?

188

Hi All

I am trying to create a ROC indicator, but for some reason, it doesn't get displayed, the code is below

double ROC, CurrentClose, PrevClose;
double RateOfChange[];
CurrentClose = iClose(Symbol(), 0, i);
PrevClose = iClose(Symbol(), 0, i + n_Period);
ROC = CurrentClose - PrevClose;
RateOfChange[i] = 100 * ROC / PrevClose;

As long as I comment out RateOfChange = 100 * ROC / PrevClose;, it starts showing something, for example, I can change it and display ROC.

What is the problem here?

Moderator
20375

luckyvictor:

Hi All

I am trying to create a ROC indicator, but for some reason, it doesn't get displayed, the code is below

As long as I comment out RateOfChange = 100 * ROC / PrevClose;, it starts showing something, for example, I can change it and display ROC.

What is the problem here?

Always check the Experts tab for errors.

In this case you will probably see a zero divide error.

PrevClose = iClose(Symbol(), 0, i + n_Period);

Make sure that this doesn't give an array out of range error.

188

Keith Watford:

Always check the Experts tab for errors.

In this case you will probably see a zero divide error.

Make sure that this doesn't give an array out of range error.

Thanks Keith for your suggestion, will look into it when I am in front of my computer.

Actually I did two things, when I create it as an indicator for its own, i use a for loop that start from historical bar (number >0) then gradually work to 0, it didn't give me problem of out of range or divide by zero, and I get it plotted on chart.

I then use this same formula in another indicator, which works together with MA, and in here, I started from 0 the latest bar and work back into history, and I don't get the plot.

Do you think this for loop itself may cause problem as well?
Moderator
20375

PrevClose = iClose(Symbol(), 0, i + n_Period);

We don't know what values you are using for i or n_Period.

But  i + n_Period must be less than rates_total or you will get the array out of range error.

20210

188

Turns out it is the problem of the for loop, and causes out of range

Thanks for everyone help