動作イメージ
コード
MQL4・MQL5どちらでもコンパイルできます。
RCI_3Lines.mq4
#property copyright "okap_goldman"
#property version "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots 3
#property indicator_minimum -100
#property indicator_maximum 100
#property indicator_level1 0
#property indicator_level2 80
#property indicator_level3 -80
input int RCI_Period1 = 9;
input int RCI_Period2 = 26;
input int RCI_Period3 = 52;
double rciBuffer1[];
double rciBuffer2[];
double rciBuffer3[];
#property indicator_label1 "RCI Short"
#property indicator_type1 DRAW_LINE
#property indicator_color1 White
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_label2 "RCI Middle"
#property indicator_type2 DRAW_LINE
#property indicator_color2 Red
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
#property indicator_label3 "RCI Long"
#property indicator_type3 DRAW_LINE
#property indicator_color3 DeepSkyBlue
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, rciBuffer1, INDICATOR_DATA);
SetIndexBuffer(1, rciBuffer2, INDICATOR_DATA);
SetIndexBuffer(2, rciBuffer3, INDICATOR_DATA);
ArraySetAsSeries(rciBuffer1, true);
ArraySetAsSeries(rciBuffer2, true);
ArraySetAsSeries(rciBuffer3, true);
IndicatorSetInteger(INDICATOR_DIGITS,2);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
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 begin = rates_total - prev_calculated;
ArraySetAsSeries(close, true);
for(int i = begin - 1; i >= 0; i--)
{
rciBuffer1[i] = CalculateRCI(close, i, RCI_Period1);
rciBuffer2[i] = CalculateRCI(close, i, RCI_Period2);
rciBuffer3[i] = CalculateRCI(close, i, RCI_Period3);
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Calculate RCI |
//+------------------------------------------------------------------+
double CalculateRCI(const double &price[], int shift, int period)
{
if(shift + period > ArraySize(price))
return(0);
double rankSum = 0;
for(int i = 0; i < period; i++)
{
int rank = 1;
for(int j = 0; j < period; j++)
{
if(i != j && price[shift + i] < price[shift + j])
rank++;
}
rankSum += MathPow(rank - (i + 1), 2);
}
double rci = (1 - 6 * rankSum / (period * (MathPow(period, 2) - 1))) * 100;
return(rci);
}