iMA + CopyBuffer Wrong way around?

 

I feel stupid.

The Copy Buffer is the wrong way around.. But why? 
In Copy Buffer.png Attachment , you can see  the Moving Average for the second last candle , should be [2].
In the top left corner , you see the comment. The first, which is the same value, is the array position [0]. 

Code:

int OnInit(){
    ArrayResize(ama,4);
    ArrayResize(ama_off,4);
    
    ma = iMA(_Symbol, PERIOD_M5, 6, 0, MODE_SMA, PRICE_CLOSE);
    ma_off = iMA(_Symbol, PERIOD_M5, 6, 6, MODE_SMA, PRICE_CLOSE);

   return(INIT_SUCCEEDED);
  }
void OnTick() {

   int bars = iBars(_Symbol,timeframe); // With every new bar
   if(barsTotal != bars ){
         barsTotal = bars; 

    CopyBuffer(ma,0,0,3,ama);
    CopyBuffer(ma_off,0,0,3,ama_off);      
 
         }
          
      Comment("\n Sma        ", ama[0],    "\t Sma+1      ", ama[1], "\t Sma+2      ", ama[2],
                     "\n sma offset ",ama_off[0], "\t sma off +1 ",ama_off[1], "\t sma off +1 ",ama_off[2],
                     "\n Reset == ", reset);       

      }
   
     
  }
Files:
CopyBuffer.png  55 kb
 
RundesZweieck:

I feel stupid.

The Copy Buffer is the wrong way around.. But why? 
In Copy Buffer.png Attachment , you can see  the Moving Average for the second last candle , should be [2].
In the top left corner , you see the comment. The first, which is the same value, is the array position [0]. 

Code:

Have you ever used MT4?

The order of data is reversed between MT4 and MT5.

You need to apply ArraySetAsSeries(ama, true) to get the same order as MT4.

 
Nagisa Unada #:
ArraySetAsSeries
Hi !
Yes ... Is the complete Candle / Bar Reverse or just the Array index (Array[n,..,3,2,1,0]) ? 
And is it just for the Copy Buffer or for all arrays?

I am confused...
 
RundesZweieck #:
Hi !
Yes ... Is the complete Candle / Bar Reverse or just the Array index (Array[n,..,3,2,1,0]) ? 
And is it just for the Copy Buffer or for all arrays?

I am confused...

The order of the array indexes is different.

MT4 array[n], .... array[2], array[1], array[0]

MT5 array[0], array[1], array[2], .... array[n].

This order can be changed with the ArraySetAsSeries function.

Try the following program.

double ama[];
double ama_off[];
int  ma, ma_off;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   ArrayResize(ama, 3);
   ArrayResize(ama_off, 3);
   ArraySetAsSeries(ama, true);
   ArraySetAsSeries(ama_off, false);

   ma     = iMA(_Symbol, PERIOD_M5, 6, 0, MODE_SMA, PRICE_CLOSE);
   ma_off = iMA(_Symbol, PERIOD_M5, 6, 0, MODE_SMA, PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   CopyBuffer(ma,     0, 0, 3, ama);
   CopyBuffer(ma_off, 0, 0, 3, ama_off);

   Comment("\n ama[2]      ", ama[2],     "\t ama[1]       ", ama[1],     "\t ama[0]      ", ama[0],
           "\n ama_off[0] ", ama_off[0], "\t ama_off[1] ", ama_off[1], "\t ama_off[2] ", ama_off[2]);
}
//+------------------------------------------------------------------+

ArraySetAsSeries(ama, true) is applied to ama[]. All other conditions are the same.

ama[]       ----> ama[2],       ama[1],        ama[0]

ama_off[] ----> ama_off[0], ama_off[1], ama_off[2]