Is there a better (simpler) way to code PullBack ?

 

Dear Members

I am trying following code for Pullback LONG, but feels a bit complicated. Also they give false signals when in RANGE.

Can someone suggest a correct and simple way to find pullbacks?

bool CMACDCross::Pullback_Long(const double pPB_Level)
  {
    bool Rule1 = false, Rule2 = false, Rule3 = false, Rule4 = false, Rule5 = false, Rule6 = false;
  //--- at least one Bullish Engulfing in Previous 'n' Bars
    for(int i = 1; i < 10; i++)
      {
        Pattern_M5.Check_Pattern(i,CANDLE_BULLISH_ENGULFING);
        Rule1 = true;
      }
  //--- Satisfy pullback rule ?
    if(M5_Rates[1].open < M5_Rates[2].open)
      Rule2 = true;
  //--- Satisfy maximum engulfing rule ?
    if(M5_Rates[1].close < M5_Rates[5].open)
      Rule3 = true;
  //--- Satisfy swing high and ATR validation ?
    if(M5_Rates[1].close < iHighest(m_WSymbol,m_WTimeFrame,MODE_OPEN,5))    // Highest of 'OPEN' Price
      Rule4 = true;
  //--- Cleans up some invalid signals
    if((M5_Lowest.Price < pPB_Level) &&    // Lowest of 9 bars, atleast touched pPB_Level &&
       ((M5_Rates[3].low > pPB_Level) ||   // previous 3 bars have stayed above pPB_Level
        (M5_Rates[2].low > pPB_Level) ||
        (M5_Rates[1].low > pPB_Level)))
      Rule5 = true;
  //--- Make sure no more than 1 of previous 3 candle close below pPB_Level
    if((M5_Rates[1].close > pPB_Level) &&
       ((M5_Rates[3].close > pPB_Level) || (M5_Rates[2].close > pPB_Level)))
      Rule6 = true;
  //--- Check for a VALID Pullback_Long, if all Rules are true
    if((Rule1 == true) && (Rule2 == true) && ((Rule3 == true) || (Rule4 == true)) && (Rule5 == true) && (Rule6 == true))
      return(true);
    else
      return(false);
  } // END Of method Pullback_S3Long()
 
  1. Your code
        bool Rule1 = false;
        if(M5_Rates[1].open < M5_Rates[2].open)
          Rule2 = true;
    Simplified
        bool Rule1 = M5_Rates[1].open < M5_Rates[2].open;

  2. Your code
        if((Rule1 == true) && (Rule2 == true) && ((Rule3 == true) || (Rule4 == true)) && (Rule5 == true) && (Rule6 == true))
          return(true);
        else
          return(false);
    Simplified
        return Rule1 && Rule2 && (Rule3 || Rule4) && Rule5 && Rule6;

    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool).

  3.     for(int i = 1; i < 10; i++)
          {
            Pattern_M5.Check_Pattern(i,CANDLE_BULLISH_ENGULFING);
            Rule1 = true;
          }
    Why is there a loop here? Why are you always setting Rule1 true nine (9) times?
 
William Roeder:

Thanks William,

about bool variable, I was confuse to use Simplified way, but now it is clear :)

the for..loop it to check if at least one Bullish Candle in previous 9 bars, as EMA sync and MACD Crossing may not sync with exactly previous bar being Bulling Engulfing. This code is run on IsNewBarM5, so will check previous 9 bars on each run.

 
Pull Back
Pull Back
  • www.mql5.com
"Pull Back" EA Trades when there is a pull back from the trend of 2 Moving Averages has Trailing Stop Loss &Take Profit works on all time frames major forex pairs and stocks NASDAQ.