【解説】【MQL5 community】 MACD_Histogram_MC : 短期と中期の指数平滑移動平均(EMA)がどの程度離れているか、 どの程度近づいたかを分析し、売買シグナルを見極める手法。 通常のMACD と同じ計算法。 特にここでは、MACDとMACDシグナルとの差をヒストグラム化したもの。 またヒストグラムは ZeroLag MACD と同様に, 特に価格が上昇している場合は, 緑色, 逆に下落している場合は, 赤色.
【計算法】
MACD=n期間のEMA−m期間のEMA
MACDシグナル(k)=直近k期間のMACDの合計 ÷ k
(注) EMA (指数移動平均、Exponential Moving Average):
(1)「SmoothFactor=2/(1+期間)」を求める。
(2)現在の値・価格に、SmoothFactorをかけたものと、(i-1)期のこの移動平均と(1-SmoothFactor)をかけたものの和を指数移動平均とする。
【シグナル】
一般にMACDがMACDシグナルを下から上に抜けたら買いシグナル、 上から下に抜けたら売りシグナル。
//+------------------------------------------------------------------+ //| MACD Histogram MC.mq5 | //| Copyright ゥ 2010, AK20 | //| traderak20@gmail.com | //| | //| Based on: | //| MACD.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2010, traderak20@gmail.com" #property description "Moving Average Convergence/Divergence, Histogram, Multi-color" #property version "v02b" #include <MovingAverages.mqh> #property indicator_separate_window #property indicator_buffers 6 #property indicator_plots 3 //--- indicator plots #property indicator_type1 DRAW_LINE #property indicator_type2 DRAW_LINE #property indicator_type3 DRAW_COLOR_HISTOGRAM #property indicator_color1 Blue #property indicator_color2 Red #property indicator_color3 Green,Red,Blue #property indicator_width1 1 #property indicator_width2 1 #property indicator_width3 1 #property indicator_label1 "MACD" #property indicator_label2 "Signal" #property indicator_label3 "Histogram" //--- enum variables enum colorswitch // use single or multi-color display of Histogram { MultiColor=0, SingleColor=1 }; enum maswitch // use SMA or EMA when calculating Signal { Simple=MODE_SMA, Exponential=MODE_EMA }; //--- input parameters input int InpFastEMA=12; // Fast EMA period input int InpSlowEMA=26; // Slow EMA period input int InpSignalMA=9; // Signal MA period input maswitch InpAppliedSignalMA=MODE_SMA; // Applied MA method for signal line input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price input colorswitch InpUseMultiColor=MultiColor; // Use multi-color or single-color histogram //--- indicator buffers double ExtMacdBuffer[]; double ExtSignalBuffer[]; double ExtHistogramBuffer[]; double ExtHistogramColorBuffer[]; double ExtFastMaBuffer[]; double ExtSlowMaBuffer[]; //--- indicator handles int ExtFastMaHandle; int ExtSlowMaHandle; //--- turn on/off error messages bool ShowErrorMessages=false; // turn on/off error messages for debugging //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA); SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA); SetIndexBuffer(2,ExtHistogramBuffer,INDICATOR_DATA); SetIndexBuffer(3,ExtHistogramColorBuffer,INDICATOR_COLOR_INDEX); SetIndexBuffer(4,ExtFastMaBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(5,ExtSlowMaBuffer,INDICATOR_CALCULATIONS); //--- sets first bar from what index will be drawn PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpSlowEMA-1); PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpSlowEMA+InpSignalMA-1); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpSlowEMA+InpSignalMA-1); //--- name for indicator IndicatorSetString(INDICATOR_SHORTNAME,"MACD("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalMA)+")"); //--- get MA handles ExtFastMaHandle=iMA(NULL,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice); ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice); //--- initialization done } //+------------------------------------------------------------------+ //| Moving Averages Convergence/Divergence | //+------------------------------------------------------------------+ 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 &TickVolume[], const long &Volume[], const int &Spread[]) { //--- set arrays as series, most recent entry at index [0] ArraySetAsSeries(ExtMacdBuffer,true); ArraySetAsSeries(ExtSignalBuffer,true); ArraySetAsSeries(ExtHistogramBuffer,true); ArraySetAsSeries(ExtHistogramColorBuffer,true); ArraySetAsSeries(ExtFastMaBuffer,true); ArraySetAsSeries(ExtSlowMaBuffer,true); //--- check for data if(rates_total<InpSlowEMA+InpSignalMA) return(0); //--- not all data may be calculated int calculated; calculated=BarsCalculated(ExtFastMaHandle); if(calculated<rates_total) { if(ShowErrorMessages) Print("Not all data of ExtFastMaHandle has been calculated (",calculated,"bars ). Error",GetLastError()); return(0); } calculated=BarsCalculated(ExtSlowMaHandle); if(calculated<rates_total) { if(ShowErrorMessages) Print("Not all data of ExtSlowMaHandle has been calculated (",calculated,"bars ). Error",GetLastError()); return(0); } //--- calculate how many bars need to be recalculated int to_copy; if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total; else { to_copy=rates_total-prev_calculated; if(prev_calculated>0) to_copy++; } //--- get fast MA buffer values if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) { if(ShowErrorMessages) Print("Getting fast EMA failed! Error",GetLastError()); return(0); } //--- get slow MA buffer values if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0) { if(ShowErrorMessages) Print("Getting slow SMA failed! Error",GetLastError()); return(0); } //--- set limit for which bars need to be (re)calculated int limit; if(prev_calculated==0 || prev_calculated<0 || prev_calculated>rates_total) limit=rates_total-1; else limit=rates_total-prev_calculated; //--- calculate MACD buffer for(int i=limit;i>=0;i--) ExtMacdBuffer[i]=ExtFastMaBuffer[i]-ExtSlowMaBuffer[i]; //--- calculate Signal buffer if(InpAppliedSignalMA==0) SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalMA,ExtMacdBuffer,ExtSignalBuffer); if(InpAppliedSignalMA==1) ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpSignalMA,ExtMacdBuffer,ExtSignalBuffer); //--- calculate Histogram buffer for(int i=limit;i>=0;i--) ExtHistogramBuffer[i]=ExtMacdBuffer[i]-ExtSignalBuffer[i]; //--- set color Histogram for(int i=limit;i>=0;i--) { if(InpUseMultiColor==MultiColor && iExtHistogramBuffer[i+1]) ExtHistogramColorBuffer[i]=0; if(ExtHistogramBuffer[i]<ExtHistogramBuffer[i+1]) ExtHistogramColorBuffer[i]=1; if(ExtHistogramBuffer[i]==ExtHistogramBuffer[i+1]) ExtHistogramColorBuffer[i]=2; } if(InpUseMultiColor==SingleColor) ExtHistogramColorBuffer[i]=0; } //--- return value of rates_total, will be used as prev_calculated in next call return(rates_total); } //+------------------------------------------------------------------+
【表示結果】
Back to Meta Trader