見出し画像

トレンドの強さを示すImpulseMACDインジケーターMT4版

TradingView

macdLine = ta.ema(close, 12) - ta.ema(close, 26)
signalLine = ta.ema(macdLine, 9)
histogram = macdLine - signalLine

MT4版

//+------------------------------------------------------------------+
//|                                         ImpulseMACDIndicator.mq4 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Blue    // MACD Line
#property indicator_color2 Red     // Signal Line
#property indicator_color3 Green   // Histogram (positive values)
#property indicator_color4 Maroon  // Histogram (negative values)

// 入力パラメーター
input int FastEMA = 12;   // 短期EMA期間
input int SlowEMA = 26;   // 長期EMA期間
input int SignalEMA = 9;  // シグナルライン期間

// インジケーターバッファ
double MACDLineBuffer[];
double SignalLineBuffer[];
double HistogramBuffer[];
double HistogramColorBuffer[];

// 計算用の一時的な配列
double FastEMABuffer[];
double SlowEMABuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   // インジケーターバッファのマッピング
   SetIndexBuffer(0, MACDLineBuffer);
   SetIndexBuffer(1, SignalLineBuffer);
   SetIndexBuffer(2, HistogramBuffer);
   SetIndexBuffer(3, HistogramColorBuffer);
   
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   SetIndexStyle(3, DRAW_NONE);
   
   SetIndexLabel(0, "MACD Line");
   SetIndexLabel(1, "Signal Line");
   SetIndexLabel(2, "Impulse MACD Histogram");
   SetIndexLabel(3, "Histogram Color (hidden)");
   
   // インジケーター名の設定
   IndicatorShortName("Impulse MACD(" + IntegerToString(FastEMA) + "," + IntegerToString(SlowEMA) + "," + IntegerToString(SignalEMA) + ")");
   
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration 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 limit;
   if(prev_calculated == 0)
     {
      limit = rates_total - MathMax(FastEMA, SlowEMA) - 1;
      ArrayInitialize(MACDLineBuffer, 0.0);
      ArrayInitialize(SignalLineBuffer, 0.0);
      ArrayInitialize(HistogramBuffer, 0.0);
      ArrayInitialize(HistogramColorBuffer, 0.0);
     }
   else
     {
      limit = rates_total - prev_calculated;
     }
   
   // 一時的な配列のサイズを設定
   ArrayResize(FastEMABuffer, rates_total);
   ArrayResize(SlowEMABuffer, rates_total);
   
   // EMAの計算
   for(int i = limit; i >= 0; i--)
     {
      FastEMABuffer[i] = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      SlowEMABuffer[i] = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
     }
   
   // MACDラインの計算
   for(int i = limit; i >= 0; i--)
     {
      MACDLineBuffer[i] = FastEMABuffer[i] - SlowEMABuffer[i];
     }
   
   // シグナルラインの計算
   for(int i = rates_total - 1; i >= 0; i--)
     {
      SignalLineBuffer[i] = iMAOnArray(MACDLineBuffer, 0, SignalEMA, 0, MODE_EMA, i);
     }
   
   // ヒストグラム(Impulse MACD)の計算
   for(int i = limit; i >= 0; i--)
     {
      HistogramBuffer[i] = MACDLineBuffer[i] - SignalLineBuffer[i];
      
      // ヒストグラムの色を設定(正の値は緑、負の値は赤褐色)
      HistogramColorBuffer[i] = HistogramBuffer[i] >= 0 ? 1 : -1;
     }
   
   // 次回の呼び出しのために計算済みバーの数を返す
   return(rates_total);
  }
//+------------------------------------------------------------------+

いいなと思ったら応援しよう!

ホソノP
よろしければサポートお願いします! いただいたサポートはクリエイターとしての活動費に使わせていただきます!