Hidden Divergence with EMA filter

 
int hiddenDivergence(int BarsShift)                                        //BarsShift is the shift in bars
   {
   double currLowRSI = iRSI(NULL,0,14,0,iLowest(NULL,0,3,10,0));           //Current RSI (Lowest in the last 10 bars)
   double currHighRSI = iRSI(NULL,0,14,0,iHighest(NULL,0,3,10,0));         //Current RSI (Highest in the last 10 bars)
   double lowestRSI = iRSI(NULL,0,14,0,iLowest(NULL,0,1,BarsShift,0));     //Lowest RSI in last x Bars
   double highestRSI = iRSI(NULL,0,14,0,iHighest(NULL,0,2,BarsShift,0));   //Highest RSI in last x Bars
   
   double currPriceLow = iLow(NULL,0,iLowest(NULL,0,1,10,0));              //Lowest price in the last 10 bars
   double currPriceHigh = iHigh(NULL,0,iHighest(NULL,0,2,10,0));           //Highest price in the last 10 bars
   double lowestPrice = iLow(NULL,0,iLowest(NULL,0,1,BarsShift,0));        //Lowest price in last x Bars
   double highestPrice = iHigh(NULL,0,iHighest(NULL,0,2,BarsShift,0));     //Highest price in last x Bars

   double valueMA = iMA(NULL,0,200,0,1,0,0);                               //Value of the 200 EMA
   
      if(Ask > valueMA && currPriceLow > lowestPrice && lowestRSI > currLowRSI)
      {
         Print("Hidden Bullish Divergence");
         return (0);
      }
      else if(Ask < valueMA && currPriceHigh < highestPrice && highestRSI < currHighRSI)
      { 
         Print("Hidden Bearish Divergence");
         return (1);
      }
      else
      {
         Print("No Divergence");
         return (2);
      }
   }

I coded a function where it checks if a certain symbol has a hidden bullish or bearish divergence. It is filtered with a 200 EMA wherein it only looks for bullish hidden divergence if price is above EMA and looks for bearish hidden divergence if price is below EMA.


What do you think behind the logic of my codes? Is there a way to optimize it?

A New Approach to Interpreting Classic and Hidden Divergence. Part II
A New Approach to Interpreting Classic and Hidden Divergence. Part II
  • www.mql5.com
The article provides a critical examination of regular divergence and efficiency of various indicators. In addition, it contains filtering options for an increased analysis accuracy and features description of non-standard solutions. As a result, we will create a new tool for solving the technical task.
 
crisp99:

I coded a function where it checks if a certain symbol has a hidden bullish or bearish divergence. It is filtered with a 200 EMA wherein it only looks for bullish hidden divergence if price is above EMA and looks for bearish hidden divergence if price is below EMA.


What do you think behind the logic of my codes? Is there a way to optimize it?

I think it's fine. 👍

If you want, you can create an enum and set the function return value to a type of that enum. This way, instead of checking for 0, 1, or 2, you can check for a programmer-friendly value.

Example:

enum HIDDEN_DIVERGENCE_TYPE {
  NO_HIDDEN_DIVERGENCE,
  HIDDEN_BULLISH_DIVERGENCE,
  HIDDEN_BEARISH_DIVERGENCE
};

HIDDEN_DIVERGENCE_TYPE hiddenDivergence(int BarsShift)                                        //BarsShift is the shift in bars
   {
   double currLowRSI = iRSI(NULL,0,14,0,iLowest(NULL,0,3,10,0));           //Current RSI (Lowest in the last 10 bars)
   double currHighRSI = iRSI(NULL,0,14,0,iHighest(NULL,0,3,10,0));         //Current RSI (Highest in the last 10 bars)
   double lowestRSI = iRSI(NULL,0,14,0,iLowest(NULL,0,1,BarsShift,0));     //Lowest RSI in last x Bars
   double highestRSI = iRSI(NULL,0,14,0,iHighest(NULL,0,2,BarsShift,0));   //Highest RSI in last x Bars
   
   double currPriceLow = iLow(NULL,0,iLowest(NULL,0,1,10,0));              //Lowest price in the last 10 bars
   double currPriceHigh = iHigh(NULL,0,iHighest(NULL,0,2,10,0));           //Highest price in the last 10 bars
   double lowestPrice = iLow(NULL,0,iLowest(NULL,0,1,BarsShift,0));        //Lowest price in last x Bars
   double highestPrice = iHigh(NULL,0,iHighest(NULL,0,2,BarsShift,0));     //Highest price in last x Bars

   double valueMA = iMA(NULL,0,200,0,1,0,0);                               //Value of the 200 EMA
   
      if(Ask > valueMA && currPriceLow > lowestPrice && lowestRSI > currLowRSI)
      {
         Print("Hidden Bullish Divergence");
         return HIDDEN_BULLISH_DIVERGENCE;
      }
      else if(Ask < valueMA && currPriceHigh < highestPrice && highestRSI < currHighRSI)
      { 
         Print("Hidden Bearish Divergence");
         return HIDDEN_BEARISH_DIVERGENCE;
      }
      else
      {
         Print("No Divergence");
         return NO_HIDDEN_DIVERGENCE;
      }
   }

Side note: your iHighest/iLowest and iMA calls check for the bar that's currently open. If this is what you want, that's fine, but it's worth noting as the results can change because the bar has yet to close.

Enumerations - Integer Types - Data Types - Language Basics - MQL4 Reference
Enumerations - Integer Types - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Enumerations - Integer Types - Data Types - Language Basics - MQL4 Reference