How to code a MACD in an expert advisor that uses EMA instead of SMA for the signal line

1.9k Views Asked by At
#include <MovingAverages.mqh>

input int fast = 7;
input int slow = 26;
input int signallength =9;
input int closeoropen = 1; //close=0, open=1
input int movingavglength =114;
double ema, macd, signal;
double ind_buffer1[26], ind_buffer2[26];
bool sellsignaldone  = false;
bool buysignaldone = false;

void OnTick()
  { 
   ema = iMA(NULL,0,movingavglength,0,MODE_EMA,PRICE_CLOSE,1);

//---- macd counted in the 1-st buffer
   for(int i=0; i<slow-1; i++)
      ind_buffer1[i]=iMA(NULL,0,fast,0,MODE_EMA,closeoropen,i)-iMA(NULL,0,slow,0,MODE_EMA,closeoropen,i);
//---- signal line counted in the 2-nd buffer
   for(int i=0; i<slow-1; i++)
      ind_buffer2[i]=iMAOnArray(ind_buffer1,0,signallength,0,MODE_EMA,i);

    macd = ind_buffer1[1];
    signal = ind_buffer2[1];
   
   if (macd > signal && macd<0 && Close[1]>ema && !buysignaldone && OrdersTotal()==0) {
      buysignaldone = true;
      OrderSend(NULL, OP_BUY,1/Ask, Ask, 3, Ask-(Ask*0.004), Ask+(Ask*0.008), "Buy Order",0,0,clrGreen);
      
   }
   if (macd < signal) {
      buysignaldone = false;
   }
   
   if (macd < signal && macd>0 && Close[1]<ema && !sellsignaldone && OrdersTotal()==0) {
      sellsignaldone = true;
      OrderSend(NULL, OP_SELL,1/Bid, Bid, 3, Bid+(Bid*0.004), Bid-(Bid*0.008), "Sell Order",0,0,clrRed);
   }
   
   if (macd > signal) {
      sellsignaldone = false;
   }
   
  }

You can't use iMACD for this. I'm not sure what I've done wrong but the signals are in the wrong place

enter image description here

Crossover is here

enter image description here

2

There are 2 best solutions below

0
On

I got a much better result using iOsMA, but it's still not identical to the indicator.

#property strict

#include <MovingAverages.mqh>

input int fast = 7;
input int slow = 26;
input int signallength =9;
input int closeoropen = 1; //close=0, open=1
input int movingavglength =114;
double sma1, sma2, ema, macd, fastema, slowema, signal;
double ind_buffer1[26], ind_buffer2[26];
bool sellsignaldone  = false;
bool buysignaldone = false;

void OnTick()
  { 
    signal = iOsMA(NULL,0,fast,slow,signallength,closeoropen,1);
    fastema = iMA(NULL,0,fast,0,MODE_EMA,closeoropen,1);
    slowema = iMA(NULL,0,slow,0,MODE_EMA,closeoropen,1);
    macd = fastema-slowema;
    ema = iMA(NULL,0,movingavglength,0,MODE_EMA,PRICE_CLOSE,1);

   if (signal>0 && macd<0 && Close[1]>ema && !buysignaldone && OrdersTotal()==0) {
      buysignaldone = true;
      OrderSend(NULL, OP_BUY,1/Ask, Ask, 3, Ask-(Ask*0.004), Ask+(Ask*0.008), "Buy Order",0,0,clrGreen);
      
   }
   if (macd < signal) {
      buysignaldone = false;
   }
   
   if (signal<0 && macd>0 && Close[1]<ema && !sellsignaldone && OrdersTotal()==0) {
      sellsignaldone = true;
      OrderSend(NULL, OP_SELL,1/Bid, Bid, 3, Bid+(Bid*0.004), Bid-(Bid*0.008), "Sell Order",0,0,clrRed);
   }
   
   if (macd > signal) {
      sellsignaldone = false;
   }
   
  }
0
On

You can also modify the MACD indicator in the indicator folder, simply adding on the bottom the "ExponentialMAOnBuffer(...);" and "call" the second indicator in the expert using the iCustom function linked to the number of the index of the indicator. (es. for MACD (0,ExtMacdBuffer);(1,ExtSignalBuffer);) the ea code now is more clear!