//+------------------------------------------------------------------+ //| MACD_Histogram.mq5 | //| Copyright 2015, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Pablo Rego" #property link "https://www.mql5.com" #property description "Moving Average Convergence/Divergence with Histogram" #property version "1.00" #property indicator_separate_window #property indicator_buffers 5 #property indicator_plots 3 #include // Plot MACD #property indicator_label1 "MACD" #property indicator_type1 DRAW_LINE #property indicator_color1 clrBlue #property indicator_style1 STYLE_SOLID #property indicator_width1 2 // Plot Signal #property indicator_label2 "Signal" #property indicator_type2 DRAW_LINE #property indicator_color2 clrRed #property indicator_style2 STYLE_SOLID #property indicator_width2 1 // Plot Histogram #property indicator_label3 "Histogram" #property indicator_type3 DRAW_HISTOGRAM #property indicator_color3 clrAqua #property indicator_style3 STYLE_SOLID #property indicator_width3 4 //--- input parameters input int InpFastEMA=12; // Fast EMA period input int InpSlowEMA=26; // Slow EMA period input int InpSignalSMA=9; // Signal SMA period input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price //--- indicator buffers double ExtMacdBuffer[]; double ExtSignalBuffer[]; double ExtFastMaBuffer[]; double ExtSlowMaBuffer[]; double ExtHistogBuffer[]; //--- MA handles int ExtFastMaHandle; int ExtSlowMaHandle; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,ExtMacdBuffer,INDICATOR_DATA); SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA); SetIndexBuffer(2,ExtHistogBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(3,ExtFastMaBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(4,ExtSlowMaBuffer,INDICATOR_CALCULATIONS); //--- sets first bar from what index will be drawn PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpSignalSMA-1); //--- name for Dindicator subwindow label IndicatorSetString(INDICATOR_SHORTNAME,"MACD("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalSMA)+")"); //--- 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 &tick_volume[], const long &volume[], const int &spread[]) { //--- check for data if(rates_totalrates_total || prev_calculated<0) to_copy=rates_total; else { to_copy=rates_total-prev_calculated; if(prev_calculated>0) to_copy++; } //--- get Fast EMA buffer if(IsStopped()) return(0); //Checking for stop flag if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0) { Print("Getting fast EMA is failed! Error",GetLastError()); return(0); } //--- get SlowSMA buffer if(IsStopped()) return(0); //Checking for stop flag if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0) { Print("Getting slow SMA is failed! Error",GetLastError()); return(0); } //--- int limit; if(prev_calculated==0) limit=0; else limit=prev_calculated-1; //--- calculate MACD for(int i=limit;i