#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 4
//--- plot DIF
#property indicator_label1 "DIF"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrSilver
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot DEA
#property indicator_label2 "DEA"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrYellow
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- plot macd hist+
#property indicator_label3 "Macd+"
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_color3 clrRed
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- plot macd hist-
#property indicator_label4 "Macd-"
#property indicator_type4 DRAW_HISTOGRAM
#property indicator_color4 clrAqua
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
input int FastEMA = 12;
input int SlowEMA = 26;
input int MACDEMA = 9;
//--- indicator buffers
double DIFBuffer[];
double DEABuffer[];
double MacdHistBuffer[];
double MacdHistBuffer1[];
double w=0;
double w1=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,DIFBuffer);
SetIndexBuffer(1,DEABuffer);
SetIndexBuffer(2,MacdHistBuffer);
SetIndexBuffer(3,MacdHistBuffer1);
SetIndexEmptyValue(2,EMPTY_VALUE);
SetIndexEmptyValue(3,EMPTY_VALUE);
for(int i=0; i<4; i++)
SetIndexDrawBegin(i,SlowEMA+MACDEMA);
IndicatorDigits(Digits);
IndicatorShortName("MACD("+(string)FastEMA+","+(string)SlowEMA+","+(string)MACDEMA+")");
if(FastEMA<0 || SlowEMA<0 || MACDEMA<0)
return(INIT_FAILED);
w = 2.0/(MACDEMA + 1);
w1= 1.0-w;
//---
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 i,limit=0;
if(rates_total<=0) return(0);
if(prev_calculated<=0) limit=rates_total-1;
else limit=rates_total-prev_calculated+1;
double hst=0.0;
for(i=limit; i>=0; i--)
{
if(i==rates_total-1) continue;
DIFBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
DEABuffer=w*DIFBuffer+w1*DEABuffer[i+1];
hst = 2.0*(DIFBuffer-DEABuffer);
if(hst>=0)
{
MacdHistBuffer=hst;
MacdHistBuffer1=EMPTY_VALUE;
}
else
{
MacdHistBuffer1=hst;
MacdHistBuffer=EMPTY_VALUE;
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+ |