MQL - add feature to Macd

Hello,

I’ve been using this custom Macd for some time and it’s my favorite macd to use, it’s a line macd but I can only have it with it’s calculations applied to the close of the candle and I would rather have it applied to Median price, High + Low / 2 (HL/2), I only know a little basic stuff on MQL and have no idea how to add this option. I was hoping someone here could do it for me since I’ve been lucky in the past. Thanks.

//+-----------------------------------------------------------------------+
//|                                      Line Macd - No Hist.mq4          |
//|                                      Copyright © 2005, David W. Thomas|
//|                                      mailto:[email protected]           |
//+-----------------------------------------------------------------------+
// This is the correct computation and display of MACD.
#property copyright "Copyright © 2005, David W. Thomas"
#property link      "mailto:[email protected]"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 White
//---- input parameters
extern int FastMAPeriod = 12;
extern int SlowMAPeriod = 26;
extern int SignalMAPeriod = 9;
//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
//---- variables
double alpha = 0;
double alpha_1 = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorDigits(Digits + 1);
   //---- indicators
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, MACDLineBuffer);
   SetIndexDrawBegin(0, SlowMAPeriod);
   SetIndexStyle(1, DRAW_LINE, STYLE_DOT);
   SetIndexBuffer(1, SignalLineBuffer);
   SetIndexDrawBegin(1, SlowMAPeriod + SignalMAPeriod);
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("NMACD(" + FastMAPeriod+"," + SlowMAPeriod + "," + SignalMAPeriod + ")");
   SetIndexLabel(0, "NMACD");
   SetIndexLabel(1, "Signal");
   //----
	  alpha = 2.0 / (SignalMAPeriod + 1.0);
	  alpha_1 = 1.0 - alpha;
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   //----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars = IndicatorCounted();
   //---- check for possible errors
   if(counted_bars < 0) 
       return(-1);
   //---- last counted bar will be recounted
   if(counted_bars > 0) 
       counted_bars--;
   limit = Bars - counted_bars;
//----
   for(int i = limit; i >= 0; i--)
     {
       MACDLineBuffer[i] = iMA(NULL, 0, FastMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i) - 
                           iMA(NULL, 0, SlowMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i);
       SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1];
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

you can change your code by replacing PRICE_CLOSE with PRICE_MEDIAN.

after you changed it in the meta editor (and saved it under a different name, otherwise you lose your original code) you should recompile and it should work fine.

good luck!

//±----------------------------------------------------------------------+
//| Line Macd - No Hist.mq4 |
//| Copyright © 2005, David W. Thomas|
//| mailto:[email protected] |
//±----------------------------------------------------------------------+
// This is the correct computation and display of MACD.
#property copyright “Copyright © 2005, David W. Thomas”
#property link "mailto:[email protected]"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 White
//---- input parameters
extern int FastMAPeriod = 12;
extern int SlowMAPeriod = 26;
extern int SignalMAPeriod = 9;
//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
//---- variables
double alpha = 0;
double alpha_1 = 0;

//±-----------------------------------------------------------------+
//| Custom indicator initialization function |
//±-----------------------------------------------------------------+
int init()
{
IndicatorDigits(Digits + 1);
//---- indicators
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, MACDLineBuffer);
SetIndexDrawBegin(0, SlowMAPeriod);
SetIndexStyle(1, DRAW_LINE, STYLE_DOT);
SetIndexBuffer(1, SignalLineBuffer);
SetIndexDrawBegin(1, SlowMAPeriod + SignalMAPeriod);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName(“NMACD(” + FastMAPeriod+"," + SlowMAPeriod + “,” + SignalMAPeriod + “)”);
SetIndexLabel(0, “NMACD”);
SetIndexLabel(1, “Signal”);
//----
alpha = 2.0 / (SignalMAPeriod + 1.0);
alpha_1 = 1.0 - alpha;
//----
return(0);
}
//±-----------------------------------------------------------------+
//| Custor indicator deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//±-----------------------------------------------------------------+
//| Custom indicator iteration function |
//±-----------------------------------------------------------------+
int start()
{
int limit;
int counted_bars = IndicatorCounted();
//---- check for possible errors
if(counted_bars < 0)
return(-1);
//---- last counted bar will be recounted
if(counted_bars > 0)
counted_bars–;
limit = Bars - counted_bars;
//----
for(int i = limit; i >= 0; i–)
{
MACDLineBuffer[i] = iMA(NULL, 0, FastMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i) -
iMA(NULL, 0, SlowMAPeriod, 0, MODE_EMA, PRICE_CLOSE, i);
SignalLineBuffer[i] = alphaMACDLineBuffer[i] + alpha_1SignalLineBuffer[i+1];
}
//----
return(0);
}
//±-----------------------------------------------------------------+

Read more: 301 Moved Permanently