Detecting Untested Swing Highs and Lows in MQL5

 

I’m working on a function to detect untested swing highs and lows in my EA. So far, I’ve written two functions that find recent swing points by identifying highs and lows over a set number of bars:

double FindHighs()
{
   double highestHigh = 0;
   for(int i = 0; i < 
        xBars; i++)
   {
      double high = iHigh(_Symbol, Timeframe, i);

      if(i > barsN && iHighest(_Symbol, Timeframe, MODE_HIGH, barsN * 2 + 1, i - barsN) == i)
      {
         if(high > highestHigh)
         {
            return high;
         }
         highestHigh = MathMax(high, highestHigh);
      }
   }
   return -1;
}

double FindLows()
{
   double lowestLow = DBL_MAX;
   for(int i = 0; i < xBars; i++)
   {
      double low = iLow(_Symbol, Timeframe, i);

      if(i > barsN && iLowest(_Symbol, Timeframe, MODE_LOW, barsN * 2 + 1, i - barsN) == i)
      {
         if(low < lowestLow)
         {
            return low;
         }
         lowestLow = MathMin(low, lowestLow);
      }
   }
   return -1;
}
Note that
xBars variable is x number of bars to the left.
barsN variable is the number of bars to the left and right of the swing.

While these functions are able to detect swing points, they don’t account for whether these levels have been retested by the price after their formation. I want to refine this logic so that it only identifies untested swing highs and lows.

Any suggestions on how to approach this or improve the detection of untested levels?

Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
Returns the number of bars count in the history for a specified symbol and period. There are 2 variants of functions calls. Request all of the...