English

## Help!! i have been struggling with arrays

Back to topics list To post a new topic, please log in or register
 7 walomx 2015.07.26 07:27   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 Periodextern 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[],                const int &spread[]){        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
 Moderator3514 GumRai 2015.07.26 07:54 #   Let's say that there are 100 bars`     limit = Bars - counted_bars -1;` when counted_bars==0limit=100-0-1limit=99 `for(i=Limite_Der_a_Izq_Final;i
 13126 WHRoeder 2015.07.26 13:47 #   ```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.
 313 JD4 2015.07.26 18:59 #   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.
 7 walomx 2015.07.27 18:50 #   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!!
 7 walomx 2015.07.27 19:00 #   WHRoeder: Always count down, repainting indicators are useless. Now it works  !!, it was really helpful, Thanks!!!
 7 walomx 2015.07.27 19:03 #   GumRai: Let's say that there are 100 bars when counted_bars==0limit=100-0-1limit=99  The maximum value of i in the loop islimit -199-1 98 is the maximum value of iIf there are 100 bars, the largest index is 99 So when the maximum value of i is reached in the loopi=98i+1=99i+2=100There is no index 100, so array out of range.  Thanks for your help !!!
 313 JD4 2015.07.27 19:14 #   walomx: Thank you very much!! Welcome.  It is a small point and people who are not well versed in programming often miss this small but important item.

To add comments, please log in or register