Sample Code of MACD Line Crosses MACD Signal Line

 

Hi everyone,

Is there any sample code of a MACD Line crosses the Signal Line?

I use the following code from the documenation but this is not the code that I was expecting:

if(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0) > iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0)) 

Please see attached image of what I mean.

Thanks in advance for any help.

 
jaypabs:

Hi everyone,

Is there any sample code of a MACD Line crosses the Signal Line?

I use the following code from the documenation but this is not the code that I was expecting:

Please see attached image of what I mean.

Thanks in advance for any help.

If you upload the MACD and template here, it will be easier to answer. Your Screen Shape is not the standard MACD.


@ jaypabs

jaypabs
jaypabs
  • 2021.04.16
  • www.mql5.com
Trader's profile
 

Hi,

I'm new to MQL4 and would like to start with the sample from documentation. So I don't have any template for now. Just looking for code that can detect a crossover of the MACD Line and Signal Line.

>> Your Screen Shape is not the standard MACD.

So is it not possible to create a crossover with MACD Line and Signal Line in meta trader 4 or 5?

Thanks for the reply.

Mehmet Bastem:

If you upload the MACD and template here, it will be easier to answer. Your Screen Shape is not the standard MACD.


@ jaypabs

 

I think what you're thinking is probably something like this.

for (i = limit; i >= 0; i--)
{
   double curr_macd   = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN,   i);
   double curr_signal = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, i);
   double prev_macd   = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN,   i + 1);
   double prev_signal = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, i + 1);
      
   if (curr_macd > curr_signal && prev_macd < prev_signal && curr_macd < 0)
      //Upward trend signal

   if (curr_macd < curr_signal && prev_macd > prev_signal && curr_macd > 0)
      //Downward trend signal
}


 
Very nice @Nagisa Unada
@JayPabs: I wonder if you OR anyone still uses this code, I would like more information about it, thanks
 
Nagisa Unada:

I think what you're thinking is probably something like this.



The requested standard is the MacdHistogram, not the MACD. These codes cannot get the same result.

The requested standard is the MacdHistogram, not the MACD. These codes cannot get the same result. The MACDHistogram is the code attached.


#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Green
//---- input parameters
extern int FastMAPeriod = 12;
extern int SlowMAPeriod = 26;
extern int SignalMAPeriod = 9;
//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
double HistogramBuffer[];
//---- 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);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   SetIndexBuffer(2, HistogramBuffer);
   SetIndexDrawBegin(2, SlowMAPeriod + SignalMAPeriod);
   //---- name for DataWindow and indicator subwindow label
   IndicatorShortName("NMACD(" + FastMAPeriod+"," + SlowMAPeriod + "," + SignalMAPeriod + ")");
   SetIndexLabel(0, "NMACD");
   SetIndexLabel(1, "Signal");
   SetIndexLabel(2, "Histogr");   
   //----
          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];
       HistogramBuffer[i] = MACDLineBuffer[i] - SignalLineBuffer[i];
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
 
Mehmet Bastem:

The requested standard is the MacdHistogram, not the MACD. These codes cannot get the same result.

The requested standard is the MacdHistogram, not the MACD. These codes cannot get the same result. The MACDHistogram is the code attached.

This is exactly what I mean. Thank you very much.

BTW, how do I call this custom indicator?

I want to trigger a buy when the MACD line crosses Signal line and it must be below histogram.

Thanks in advance for the help.

 
Mehmet Bastem:

The requested standard is the MacdHistogram, not the MACD. These codes cannot get the same result.

You calculate the signal line with EMA and I calculate it with SMA. That's the only difference. 

The question is how to detect the crossing of the MACD line and the signal line, so in this case the histogram is not needed.

I am not doing anything wrong.

 
Mosleur Rahaman Riad:
Very nice @Nagisa Unada
@JayPabs: I wonder if you OR anyone still uses this code, I would like more information about it, thanks
I'm tring to understand the code of @Nagisa Unada. But since I'm new to MQL4, I'm still studying his code. But I believe that's the correct code that I want plus the indicator code of @Mehmet Bastem.
 
Nagisa Unada:

You calculate the signal line with EMA and I calculate it with SMA. That's the only difference. 

The question is how to detect the crossing of the MACD line and the signal line, so in this case the histogram is not needed.

I am not doing anything wrong.

By studying more about MQL4, I came to realize that the MACD indicator can be program to look like the one from the screenshot I attached above.

And yes, you are right. You just use SMA rather than EMA.

I'm still wondering what is the code to determine if MACD line and Signal Line is below histogram line or zero line.

I've would be very glad if you can help me on this.

Thanks

 

We don't usually make decisions based on whether MACD line is above or below the histogram. How do you determine when the line overlaps the histogram?

Usually, we judge the MACD line by whether it is above or below the zero level.

double curr_macd   = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN,   1);
double curr_signal = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 1);
double prev_macd   = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN,   2 );
double prev_signal = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 2 );
double histogram   = iOsMA(NULL, 0, 12, 26, 9, PRICE_CLOSE, 1);          //Histogram if you need
      
if (curr_macd > curr_signal && prev_macd < prev_signal && curr_macd < 0)
   //buy signal   

if (curr_macd < curr_signal && prev_macd > prev_signal && curr_macd > 0)
   //sell signal
Reason: