【MQL4テクニック】RCIを使用する方法。iRCI関数のコピペで使用できます。

MQL4テクニック

RCIは非常に人気なインジケータです。しかし残念ながらMT4に標準で用意されているインジケータではないため、i○○関数として使用することが出来ません。

殆どの場合はRCIの外部インジケータをiCustom関数で呼び出すような形で使用すると思いますが、それでは不便ですので今回はiRCI関数を用意しました。

使用方法はiRSI関数やiCCI関数と違いはありません。是非ご活用ください!

iRCI関数

double iRCI(string symbol, int timeframe, int period, int shift){   

   double close[];
   ArrayResize(close, period); 

   for (int i = 0; i < period; i++) {
      close[i] = iClose(symbol, timeframe, shift + i);
   }
   ArraySort(close, WHOLE_ARRAY, 0, MODE_DESCEND);

   double d = 0;
   for (int i = 0; i < period; i++) {
      int rank = ArrayBsearch(close, iClose(symbol, timeframe, shift + i), WHOLE_ARRAY, 0, MODE_DESCEND);
      d += MathPow(i - rank, 2);
   }
   
   double rci = (1 - 6 * d / (period * (MathPow(period, 2) - 1))) * 100;
   return rci;
}

サンプルコード

#property copyright "Codinal Systems"
#property link      "https://codinal-systems.com/"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 1


double rci[];

int OnInit(){
   
   SetIndexBuffer(0, rci);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1, clrAqua);
   
   return(INIT_SUCCEEDED);
}


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[])
{
   for(int i = 0; i < rates_total; i++){
      rci[i] = iRCI(Symbol(), Period(), 14, i);
   }
   return(rates_total);
}


double iRCI(string symbol, int timeframe, int period, int shift){   

   double close[];
   ArrayResize(close, period); 

   for (int i = 0; i < period; i++) {
      close[i] = iClose(symbol, timeframe, shift + i);
   }
   ArraySort(close, WHOLE_ARRAY, 0, MODE_DESCEND);

   double d = 0;
   for (int i = 0; i < period; i++) {
      int rank = ArrayBsearch(close, iClose(symbol, timeframe, shift + i), WHOLE_ARRAY, 0, MODE_DESCEND);
      d += MathPow(i - rank, 2);
   }
   
   double rci = (1 - 6 * d / (period * (MathPow(period, 2) - 1))) * 100;
   return rci;
}
POINT

こちらの記事で悩みが解決されない場合は、
MQL4 User Community に参加してお気軽にご質問ください。

MQL4初心者の方や開発に行き詰った方の悩みを、 様々なソフトウェアやツールを開発してきた経験豊富なエンジニアたちが解決いたします。