iMAOnArrayに関して疑問に思ったこと。理由は分かっていないけど。

MQL4のiMAOnArray関数で分からないことがある。
単に自分の使い方が間違っているだけだろうが。

メタエディターのNavigatorのDictionaryからiMAOnArrayの説明を引用したのが下記。

double iMAOnArray( double array, int total, int period, int ma_shift, int ma_method, int shift)

Calculation of the Moving Average on data stored in a numeric array. Unlike iMA(...), the iMAOnArray function does not take data by symbol name, timeframe, the applied price. The price data must be previously prepared. The indicator is calculated from left to right. To access to the array elements as to a series array (i.e., from right to left), one has to use the ArraySetAsSeries function.
Parameters:
array - Array with data.
total - The number of items to be counted. 0 means whole array.
period - Averaging period for calculation.
ma_shift - MA shift
ma_method - MA method. It can be any of the Moving Average method enumeration value.
shift - Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).

これを見ると、array[]にはデータ配列を入れればいいので、終値を表すCloseをそのまま代入しても良いのかと思って次のように書いた。

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Gold

//Indicator buffer
double SmaBuf[];
double EmaBuf[];
double CloseBuf[];

extern int MA_Period = 20;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0, SmaBuf);
   SetIndexBuffer(1, EmaBuf);
   SetIndexBuffer(2, CloseBuf);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit = Bars - IndicatorCounted();
   if(limit == Bars){
      limit = limit - (MA_Period - 1);
   }
    
   for(i=limit-1; 0<=i; i--){
   //Close配列をそのまま使用する。これでは上手くいかない。
      CloseBuf[i] = iMAOnArray(Close, 0, MA_Period+30, 0, MODE_SMA,i);
   }
   
   return(0);
  }
//+------------------------------------------------------------------+

結果、何も表示されない。

Closeをそのまま使用するのではなく、一度CloseBufというバッファーに格納してからiMAOnArray関数を使用したら表示された。

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Gold

//Indicator buffer
double SmaBuf[];
double EmaBuf[];
double CloseBuf[];

extern int MA_Period = 20;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0, SmaBuf);
   SetIndexBuffer(1, EmaBuf);
   SetIndexBuffer(2, CloseBuf);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit = Bars - IndicatorCounted();
   if(limit == Bars){
      limit = limit - (MA_Period - 1);
   }
   
   for(int i=limit-1; 0<=i; i--){
      //一度、Bufferに格納する。
      CloseBuf[i]  = Close[i];
   }
   
   for(i=limit-1; 0<=i; i--){
      CloseBuf[i] = iMAOnArray(CloseBuf, 0, MA_Period+30, 0, MODE_SMA,i);
   }
   
   return(0);
  }
//+------------------------------------------------------------------+

Closeもデータの配列だから変わらないと思ったのだが・・・。