# Help!! i have been struggling with arrays

128

Hello i have been struggling with arrays, in this case i need to look to rsi in bar number 5,6 or 7 but it does a array out of rage, can you figure out what am i doing wrong,

Thanks.

Here is  the code

--------

double RSI[];

extern int RSIPer = 14;//RSI Period

extern int Limite_Der_a_Izq_Inicial= 0;

extern int Limite_Der_a_Izq_Final= 0; // Real time

int counted_bars;

int limit;

int i;

int OnCalculate(const int rates_total,

const int prev_calculated,

const datetime &time[],

const double &open[],

const double &high[],

const double &low[],

const double &close[],

const long &tick_volume[],

const long &volume[],

{

counted_bars = IndicatorCounted();

ArrayResize(RSI,Bars);

if(counted_bars < 0) return(-1);

if(counted_bars > 0) counted_bars--;

if(Limite_Der_a_Izq_Inicial != 0)

{

limit = Limite_Der_a_Izq_Inicial;

}

else

{

limit = Bars - counted_bars -1;

}

for(i=Limite_Der_a_Izq_Final;i<limit;i++)

{

RSI[i]  = iCustom(NULL,0,"RSI",0,i);

Comment(RSI[i+2]); // it works with i and i+1, but i+2.... i+n it does not!!

}

return(rates_total);

Moderator
10411

Let's say that there are 100 bars

`     limit = Bars - counted_bars -1;`

when counted_bars==0

limit=100-0-1

limit=99

`for(i=Limite_Der_a_Izq_Final;i<limit;i++)`

The maximum value of i in the loop is

limit -1

99-1

98 is the maximum value of i

If there are 100 bars, the largest index is 99

`Comment(RSI[i+2]); // it works with i and i+1, but i+2.... i+n it does not!!  `

So when the maximum value of i is reached in the loop

i=98

i+1=99

i+2=100

There is no index 100, so array out of range

15350

```int counted = IndicatorCounted();
int lookback = ... // iMA(period) has look back of period.
// buffer[i+2] has look back of 2
// use maximum of all.
for(int iBar = Bars - 1 - MathMax(lookback, counted); iBar >= 0; --iBar) ...
```
Always count down, repainting indicators are useless.
1101

When you create an array that needs, let's say 10 spaces, you use the MyArray[10] when you create it.  But computer programs start counting array indexes at 0, so for MyArray, the correct indexes would be 0-9, which is why Gum said limit-1.  If you try using an index that is outside of that array's range, in this case, less than 0 or greater than 9, you get the error you are talking about.
128

JD4:
When you create an array that needs, let's say 10 spaces, you use the MyArray[10] when you create it.  But computer programs start counting array indexes at 0, so for MyArray, the correct indexes would be 0-9, which is why Gum said limit-1.  If you try using an index that is outside of that array's range, in this case, less than 0 or greater than 9, you get the error you are talking about.
Thank you very much!!
128

WHRoeder:
Always count down, repainting indicators are useless.
Now it works  !!, it was really helpful, Thanks!!!
128

GumRai:

Let's say that there are 100 bars

when counted_bars==0

limit=100-0-1

limit=99

The maximum value of i in the loop is

limit -1

99-1

98 is the maximum value of i

If there are 100 bars, the largest index is 99

So when the maximum value of i is reached in the loop

i=98

i+1=99

i+2=100

There is no index 100, so array out of range.