Cannot Declare Array

To add comments, please log in or register
koranged
448
koranged  
double Limit=Bars-1;
double DR[Limit];

for(i=Bars-1; i>=0; i--)
   {
   DR[i]=Close[i]-Open[i]; // daily return
   Alert("Daily return of bar ", i, " = ",DR[i]);
   }

The above returns the error "invalid index value" in the highlighted position. 

double DR[];
int i;

for(i=Bars-1; i>=0; i--)
   {
   DR[i]=Close[i]-Open[i]; // daily return
   Alert("Daily return of bar ", i, " = ",DR[i]);
   }

The above cannot load in the terminal due to the array being 'out of range'


This is the first time I have had to declare/define a user-defined array so I apologise if this is ultra-basic to the reader, but I can't see what I'm doing wrong. 

In the first situation, I have declared the number of elements (as is shown in the manual: https://book.mql4.com/variables/arrays)

In the second situation, I have left the number of elements undeclared, and 'i' is out of range. How can this be when the value of 'i' is dependant upon Bars-1?

Arrays - Variables - MQL4 Tutorial
Arrays - Variables - MQL4 Tutorial
  • book.mql4.com
A large part of information processed by application programs is contained in arrays. Concept of Arrays Array is an arranged set of values of one-type variables that have a common name. Arrays can be one-dimensional and multidimensional. The maximum admissible amount of dimensions in an array is four. Arrays of any data types are allowed. Array...
Carl Schreiber
Moderator
8823
Carl Schreiber  

Either you use a fixed array:

double DR[100];

OR you have to ste the lenght before you use it:

double DR[];
ArraySize(DR,100);
ArrayResize(DR,100);
koranged
448
koranged  
Carl Schreiber:

Either you use a fixed array:

OR you have to ste the lenght before you use it:

Using a fixed array is what I tried in the first box:

double Limit=Bars-1;
double DR[Limit];

for(i=Bars-1; i>=0; i--)
   {
   DR[i]=Close[i]-Open[i]; // daily return
   Alert("Daily return of bar ", i, " = ",DR[i]);
   }

Are you saying that I cannot use a variable in the above, but only a constant?

ArraySize(DR,100);

The above returns the error 'wrong parameters count'

koranged
448
koranged  
It appears that 'ArraySize' simply returns the number of elements in an array, rather than allowing you to define the required number of elements with a variable.
Anthony Garot
3251
Anthony Garot  
koranged:
It appears that 'ArraySize' simply returns the number of elements in an array, rather than allowing you to define the required number of elements with a variable.

Yes, that's correct. ArraySize() does not allocate.

For dynamically allocating an array, you want ArrayResize().

Alain Verleyen
38646
Alain Verleyen  
koranged:

The above returns the error "invalid index value" in the highlighted position. 

The above cannot load in the terminal due to the array being 'out of range'


This is the first time I have had to declare/define a user-defined array so I apologise if this is ultra-basic to the reader, but I can't see what I'm doing wrong. 

In the first situation, I have declared the number of elements (as is shown in the manual: https://book.mql4.com/variables/arrays)

In the second situation, I have left the number of elements undeclared, and 'i' is out of range. How can this be when the value of 'i' is dependant upon Bars-1?

Could you please post in the right section, don't you see that each time we have to move your topic to mql4 section ?
koranged
448
koranged  
Anthony Garot:

Yes, that's correct. ArraySize() does not allocate.

For dynamically allocating an array, you want ArrayResize().

Thanks.

koranged
448
koranged  
Alain Verleyen:
Could you please post in the right section, don't you see that each time we have to move your topic to mql4 section ?

No I hadn't noticed. 

Alain Verleyen
38646
Alain Verleyen  
koranged:

No I hadn't noticed. 

Ok, so now you know.
To add comments, please log in or register