Download MetaTrader 5
To add comments, please log in or register
Sell products right in MetaTrader. Millions of traders are waiting for this!
nguyen
9
nguyen 2014.06.24 16:22 

Hi guys, i'm new here, and i have a problem hoping that i can seek some guidance from you guys.

 i have an EA that will enter trade based on MACD. the condition is quite simple, when the signal line, the MACD line is above or below 0 line, and the MACD histogram is ascending or descending, it will buy or sell accordingly. but i encounter a problem that sometime the EA gave false positive o true negative... so i was hoping that you guys could look at my code and tell me where did i went wrong, in hope that i can find a way to fix it.

 

string CheckMACD (string trend) 
{
   double alpha = 2.0 / (9 + 1.0);
        double alpha_1 = 1.0 - alpha;
        double MACDLine[6];
        double SignalLine[6];
        double Histogram[6];
        int i;
        string bCheck,sCheck, msg;
        RefreshRates();
   for(i=0; i < 5; i++)
   {
      MACDLine[i] = iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,i+1) - iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,i+1); 
   }
   for(i = 0; i < 4; i++)
   {
      SignalLine[i] = alpha*MACDLine[i] + alpha_1*SignalLine[i+1];
   }
   for(i = 0; i < 4; i++)
   {
      Histogram[i] = MACDLine[i] - SignalLine[i];
   }
   if (trend == "up")
   {
      for (i = 0; i < 3; i ++)
      {
         if ( MACDLine[0] > 0 && SignalLine[0] > 0)
         {
            if (Histogram[i] > 0 && Histogram[i] > Histogram[i+1])
            {
               bCheck = "good";
               
            }
            else {
               bCheck = "fail";
               msg = Symbol() + " Trade rejected. Reason bad MACD: " + NormalizeDouble(Histogram[2],6) + "; " +NormalizeDouble(Histogram[1],6) + "; " +NormalizeDouble(Histogram[0],6);
               Alert(msg);
               SendNotification(msg);
               break;
            }
         } else {
            msg = Symbol() + " Trade rejected. Reason MACD Line & Signal Line below 0";
               Alert(msg);
               SendNotification(msg);
         }
      }
      return (bCheck);   
   }
   if (trend == "down")
   {
      for (i = 0; i < 3; i ++)
      {
         if (MACDLine[0] < 0 && SignalLine[0] < 0)
         {
            if (Histogram[i] < 0 && Histogram[i] < Histogram[i+1])
            {
               sCheck = "good";
            }
            else
            {
               sCheck = "fail";
               msg = Symbol() + " Trade rejected. Reason bad MACD: " + NormalizeDouble(Histogram[2],6) + "; " + NormalizeDouble(Histogram[1],6) + "; " + NormalizeDouble(Histogram[0],6);
               Alert(msg);
               SendNotification(msg);
               break;
            }
         }
         else {
            msg = Symbol() + " Trade rejected. Reason Signal Line & MACD Line above 0";
               Alert(msg);
               SendNotification(msg);
         }
      }
      return (sCheck);   
   }
   return (0);
}

 some time it reject trades while the chart looks like this (it supposed to accept the signal and enter a trade)

false positive MACD 

fasle positive output

 

or sometime it accept trade which it should have been rejected

true negative MACD 

 

 i'm really glad if you guys could take a look and tell me where did i go wrong, thanks!!! 

luis
23
luis 2014.06.26 09:07  

Some comments ...

1) why not use iMACD() instead of using several calls to iMA()?

2) Your first graph doesn't comply with your constrains, because MACD and Signal lines are both < 0 and Histogram is both > 0 (instead of <0) and decreasing

Nothing strange that you get a "trade rejected"

nguyen
9
nguyen 2014.07.02 02:52  
string CheckMACD (string trend) 
{
        double MACDLine[6];
        double SignalLine[6];
        double Histogram[6];
        int i;
        string bCheck,sCheck, msg;
        RefreshRates();
   for(i=0; i < 5; i++)
   {
      MACDLine[i] = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i); 
   }
   for(i = 0; i < 4; i++)
   {
      SignalLine[i] = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,i);
   }
   for(i = 0; i < 4; i++)
   {
      Histogram[i] = MACDLine[i] - SignalLine[i];
   }
   if (trend == "up")
   {
      for (i = 0; i < 3; i ++)
      {
         if ( MACDLine[0] > 0 && SignalLine[0] > 0)
         {
            if (Histogram[i] > 0 && Histogram[i] > Histogram[i+1])
            {
               bCheck = "good";
               
            }
            else {
               bCheck = "fail";
               msg = Symbol() + " Trade rejected. Reason bad MACD: " + NormalizeDouble(Histogram[2],6) + "; " +NormalizeDouble(Histogram[1],6) + "; " +NormalizeDouble(Histogram[0],6);
               Alert(msg);
               SendNotification(msg);
               break;
            }
         } else {
            msg = Symbol() + " Trade rejected. Reason MACD Line & Signal Line below 0";
               Alert(msg);
               SendNotification(msg);
         }
      }
      return (bCheck);   
   }
   if (trend == "down")
   {
      for (i = 0; i < 3; i ++)
      {
         if (MACDLine[0] < 0 && SignalLine[0] < 0)
         {
            if (Histogram[i] < 0 && Histogram[i] < Histogram[i+1])
            {
               sCheck = "good";
            }
            else
            {
               sCheck = "fail";
               msg = Symbol() + " Trade rejected. Reason bad MACD: " + NormalizeDouble(Histogram[2],6) + "; " + NormalizeDouble(Histogram[1],6) + "; " + NormalizeDouble(Histogram[0],6);
               Alert(msg);
               SendNotification(msg);
               break;
            }
         }
         else {
            msg = Symbol() + " Trade rejected. Reason Signal Line & MACD Line above 0";
               Alert(msg);
               SendNotification(msg);
         }
      }
      return (sCheck);   
   }
   return (0);
}
Thanks luis js!!! I Changed my code into this and it seems to have solve the issue. i will continue to monitor it and come back later if the problem still occurs, thanks guys!!
/
To add comments, please log in or register