MQL5

マルチタイムフレームMACD for MT5

今回はマルチタイムフレーム対応のMACDです。これまでと違ってヒストグラムですが、これまで紹介してきたコードをほぼそのまま流用できます。コードの説明はRSIのほうを参照してください。

ソースコード

#property copyright   "© shanch, 2020"
#property link        "shanch2@gmail.com"
#property description "MTFMACD"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_type1   DRAW_HISTOGRAM
#property indicator_type2   DRAW_LINE

#property indicator_color1  clrSilver
#property indicator_color2  clrRed

#property indicator_width1  1
#property indicator_width2  1

#property indicator_style1  STYLE_SOLID
#property indicator_style2  STYLE_DOT

input ENUM_TIMEFRAMES  TimeFrame = PERIOD_H1;
input int FastPeriod = 12;
input int SlowPeriod = 26;
input int SignalPeriod = 9;

int hMACD;
double BufMACD[];
double BufSignal[];
double BufTemp[];

int OnInit()
{
   SetIndexBuffer(0, BufMACD, INDICATOR_DATA);
   SetIndexBuffer(1, BufSignal, INDICATOR_DATA);
   
   ArraySetAsSeries(BufMACD, true);
   ArraySetAsSeries(BufSignal, true);

   ArraySetAsSeries(BufTemp, true);
   
   hMACD = iMACD(NULL, TimeFrame, FastPeriod, SlowPeriod, SignalPeriod, PRICE_CLOSE);

   if(hMACD == INVALID_HANDLE)
   {
      return(INIT_FAILED);
   }
   
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) { IndicatorRelease(hMACD);}

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[])
  {
   ArraySetAsSeries(time,true);
   
   int limit = rates_total - prev_calculated;
   if(limit == 0) limit= 1;
   
   int shift = 0;
   
   for(int i=0; i<limit; i++)
   {
      shift=iBarShift(_Symbol,TimeFrame,time[i]);
      
      if(CopyBuffer(hMACD, 0, shift, 1, BufTemp) <= 0)
      {
         Print("Getting MACD failed! Error",GetLastError());
         return(0);
      }
      else
      {
          BufMACD[i]=BufTemp[0];
      }
      if(CopyBuffer(hMACD, 1, shift, 1, BufTemp) <= 0)
      {
         Print("Getting MACD Signal failed! Error",GetLastError());
         return(0);
      }
      else
      {
          BufSignal[i]=BufTemp[0];
      }
   }

   for(int i=1; i<rates_total; i++)
   {
      if(iBarShift(NULL, TimeFrame, time[i]) > 0)
      {
         break;
      }
      BufMACD[i]=BufMACD[0];
      BufSignal[i]=BufSignal[0];
   }

   return(rates_total);
  }