How can I avoid trading on Divergence!!!

 

Hello Friends

I am creating an EA based on Trending Strategy and it is working fine while in trend. However, when it reached divergence it start taking order most of which result in loss.

any guide how to handle this, will highly appreciated  or a recommended book to learn professional MQL4 programming.

I have attached the codes for BUY Section as well as the screen shot for trades in Divergence marked with vertical line.

Look forward to guidance for a solution to this problem.

//+------------------------
// FUNCTION to DEFINE TREND
//+------------------------
bool Is_UpTrend()
{
  bool isUpTrend;
  if((getADX_Main(1) > 10) && (getADX_PlusDI(1) > 10))                     // ADX Main & PlusDI are above ADX TrendStart Threshold
      if((getMA_21(1) > getMA_55(1)) && (getMA_21(1) > getMA_200(1)))      // 21SMA is ABOVE 55EMA & 21EMA is ABOVE 200EMA
        if(getMA_Slope21(indexFrom,1) > 0.10)                              // 21SMA is Slopping Upward (14 bars slope)
          if(iMACD(NULL,PERIOD_M15,8,21,5,PRICE_CLOSE,MODE_MAIN,1) > 0.00025)
            if(getBB_BandWidth(1) > 0.10)
            {
              isUpTrend = true;
            }
            else { isUpTrend = false; }
  return(isUpTrend);
}

//+---------------------------
// Code for Buy in the main EA
//+---------------------------
  if((getBB_NormalizedVolume(1) >= 5) && (getBB_BandWidth(1) > 10) && (getBB_BandWidth(1) <= 50) &&
     (!IsDoji() == true)) // Previous Candle is not of Trend Reversal Pattern
  {
    //+-------------------------------------------------------------------------------------------------------------------------+
    //| BUY/LONG Market TRENDING UpWard
    //+-------------------------------------------------------------------------------------------------------------------------+
    if(Is_UpTrend() == true)
    {
      // i2 Closed below 21EMA/9EMA and i1 Closed above 21EMA/9EMA
      if(((Low[2] < getMA_21(2))    && (Close[1] > getMA_21(1))) ||
         ((Low[2] < getMA_9(2)) && (Close[1] > getMA_9(1))))
        if(getADX_PlusDI(1) > getADX_MinusDI(1))
          if((getBB_VolumeIntensity(1) > -0.02) && (getBB_VolumeIntensity(1) < 0.25))
              if(iRSI(NULL,PERIOD_M15,14,PRICE_CLOSE,1) <= 80)      // Not in OverBought Zone
                // with the following line, I am loosing many possible good trades, so want to remove 
                if(iAC(NULL,PERIOD_M15,2) < iAC(NULL,PERIOD_M15,1)) // Bill Williams' Accelerator/Decelerator oscillator
                open_Order(0,"Trending");
    } //END of Trade BUY Section
Reason: