Problem when working with Array....

 

Guys:

Lately, i work with an EA which uses iMA value.

The common way of using iMA value is as below:

   ma_0 = iMA(NULL, 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, 0);
   ma_1 = iMA(NULL, 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, 1);
   ma_2 = iMA(NULL, 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, 2);

and then, we can compare those value, for example, to determine a trend.....

but, i try to work with iMA in a different manner by using Arrays.

below is my code for iMA value restoring:

   double MAvalue[];
   for(int i = 0; i <= Bars - 1; i++)
   {
      MAvalue[i] = iMA(NULL, 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, i);
   }

notice that the MAvalue[] is declared in a global level, and the for loop is used in the start().

According to my understanding, MAvalue[0] should be equal to ma_0.

but when i exported those values, they are different.

below is the results at the first bar which EA is lauched:

bars = 562;

ma_0 = 1.3756;

MAvalue[0] = 0;

MAvalue[1] = 0;

length = 0; this one is the return of ArraySize(MAvalue)

i = 562;

could anybody explain the result?

regards

saji

 
why does the size of array MAvalue is 0?
 

This is not valid . . . please read the Documentation & Book . . .

double MAvalue[];
 
RaptorUK:

This is not valid . . . please read the Documentation & Book . . .


Is the size of an one-dimentional array must be specified when it is declared?

like,

double MAvalue[]--------not valid, but

double MAvalue[100]------valid

is that correct?

 
saji:

Is the size of an one-dimentional array must be specified when it is declared?

like,

double MAvalue[]--------not valid, but

double MAvalue[100]------valid

is that correct?

https://book.mql4.com/variables/arrays


Array Declaration and Access to Array Elements


Before using an array in a program, it must be declared. An array can be declared like a variable on the global and local level. Accordingly, values of global array elements are available to the whole program, values of a local one - only to the function, in which it is declared. An array cannot be declared on the level of a client terminal, that is why global variables of client terminal cannot be gathered into an array. Array elements values can be of any type. Values of all array elements are of the same type, namely of the type indicated at array declaration. When declaring an array, data type, array name and number of elements of each dimension must be specified:

 
saji:

double MAvalue[100]------valid

is that correct?

Correct.

But don't mix up Indicator Buffers and Arrays . . . they are similar but at the same time . . . different.

 
RaptorUK:

Correct.

But don't mix up Indicator Buffers and Arrays . . . they are similar but at the same time . . . different.

dear RaptorUK:

Still got one problem....

if "double MAvalue[]" is not valid, then why the editor didn't report error? is that because the terminal consider MAvalue[] as a variable?

but if that, in the for loop, the MAvalue[i] should also be considered as a variable. But it is not declared, why the editor didn't report error?

could you please explain it for me?

Thank you in advance. I checked the book, but it is not referred.

saji

 
saji:

dear RaptorUK:

Still got one problem....

if "double MAvalue[]" is not valid, then why the editor didn't report error? is that because the terminal consider MAvalue[] as a variable?

but if that, in the for loop, the MAvalue[i] should also be considered as a variable. But it is not declared, why the editor didn't report error?

could you please explain it for me?

OK, let me re-phrase what I said above . . .

double MAvalue[];

is valid . . . the Compiler does not report it as an error . . . but what it means is an Array of 0 size, i.e. 0 elements, so if you try to print MAvalue[i] you will get 0 . . . . so essentially it is valid but it is useless . . so effectively invalid.

Try this instead . . .

double MAvalue[];

ArrayResize(MAvalue, Bars);  // <--- add this
 
   for(int i = 0; i <= Bars - 1; i++)
   {
      MAvalue[i] = iMA(NULL, 0, MA_Period, 0, MODE_SMA, PRICE_CLOSE, i);
   }

https://docs.mql4.com/array/ArrayResize

By the way . . . read the book here: https://book.mql4.com/variables/arrays