Download MetaTrader 5
To add comments, please log in or register
Find out the latest news!
minimax2000 2016.03.18 03:55 
iMAOnArray returns wrong value when it is working on array passed as function's argument.

The code below shows that iMAOnArray returns diffrent result for argument array and local array though they are equal.  It seems that iMAOnArray caches array data on first call of PrintArrayMA.

loop0:  array_ma=1.000000  /  a_ma=1.000000

loop1:  array_ma=1.000000  /  a_ma=2.000000

loop2:  array_ma=1.000000  /  a_ma=3.000000

Are there any workaround for this problem? 


int OnCalculate(...)
        double array[10];
        ArraySetAsSeries(array, true);
        ArrayInitialize(array, 0);
        for (int i = 0; i < 3; i++)
                array[0] = i+1;
                PrintArrayMA(i, array);         // buggy only when passed as function's argument

void PrintArrayMA(int i, const double &array[])
        double a[];
        ArraySetAsSeries(a, true);
        ArrayCopy(a, array);
        double array_ma = iMAOnArray(array, 0, 1, 0, MODE_SMA, 0);
        double a_ma     = iMAOnArray(a,     0, 1, 0, MODE_SMA, 0);
        if (ArrayCompare(array, a) == 0)
                PrintFormat("loop%d:  array_ma=%lf  /  a_ma=%lf", i, array_ma, a_ma);   // array_ma and a_ma should be the same


To add comments, please log in or register