【MQL4勉強プログラミング】インジケーターの計算に使用するバッファーの個数を指定する方法

IndicatorCounted()関数は、インジケーターの確定値が計算されたバー(=現在のバー以外)の本数を取得するために使用します。

IndicatorCounted()関数は、以下のように定義されています。

int IndicatorCounted();

IndicatorCounted()関数には引数がないので、()内には何も記述しません。

戻り値

インジケーターの確定値が計算されたバーの本数が返されます。

注意点

既に計算されたバーの多くは、インジケーターの値を再計算する必要はありません。

IndicatorCounted()関数は、インジケーターの値の計算を最適化するために使用します。

具体例

インジケーターをチャートに適用してから2ティック目以降は、現在のバーについてのみインジケーターの値を計算するようにするためには、以下のように記述します。


#property strict
……
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
{
  int limit = Bars - IndicatorCounted();
  
  for(int i = limit - 1; i >= 0; i--)
     {
       double MA = iMA(NULL,0,25,0,MODE_SMA,PRICE_CLOSE,i);
     }
  
  return(rates_total);
}