while array help loop

 

Goodmorning everyone, I am new to programming, I have been looking for the solution to the "array out of range" problem of the following lines of code for a week ... you can help me?

int OnInit() {



  ArraySetAsSeries(arrayPriceData, true);

  int nrBarreMemorizzate = CopyRates(_Symbol,_Period,0,10,arrayPriceData);

  int stop=100;

  int start=1;

  double CLOP [];

    

  while (start < stop) {

  CLOP [start] = arrayPriceData[start].close - arrayPriceData[start].open;

  start = start+1;

  Print("Array " +(string)start + " ok");

  }

I would like to create an array containing the above calculation and populate it with a while loop

 

Hi and good morning,

the CLOP array is only declared, but as an empty array. So you have to declare it with a fixed size e.g.:

double CLOP[100];

or you have to use the ArrayResize-function to put elements into the array:

double CLOP[];

ArrayResize(CLOP, 100);

I hope, this helps you.

Best regards

 
Federico Ciani: Goodmorning everyone, I am new to programming, I have been looking for the solution to the "array out of range" problem of the following lines of code for a week ... you can help me?

I would like to create an array containing the above calculation and populate it with a while loop

  1. Your code is incomplete! You don't show the declaration of the "arrayPriceData" array.
  2. You have not specified if this is an EA or Indicator.
  3. You should not be doing those operations such as CopyRates in the OnInit(). OnInit() is used for initial setup and initialisation and there is no guarantee that the terminal is connected to the trade server or that the data is available at the at time. Those operations should be carried out in the OnTick() for EAs or in the OnCaluculate() for Indicators.
  4. The array CLOP is declared as a Dynamic array, but you have not set a size for it, neither statically nor dynamically via ArrayResize().
  5. You are using an maximum array max index of 100 (hard coded), but have completely ignored the result of the CopyRates in the nrBarreMemorizzate which could be less than the 100.
  6. Arrays start at index 0, not index 1.
Reason: