Trying to find the high of a MACD wave

 

First off, I'm really new to coding so I apologize if my code is messy and inefficient.  That said, my long-term goal is to create an EA that will identify and eventually trade divergence.  So, my only goal right now is just to create a script to do a tiny part.  When other variables line up, that's when the EA will look for divergence.  So the code below is what I hope will run once the other criteria is met.  What I'm trying to write is this...

- look backward in time and note the place where the MACD value is the higher than the current MACD value.  This much I seem to have done ok.

- from that place in time (ignoring all the peaks and valleys between there and the current time) I'd like to continue to look back for the highest point of that wave.  As you can see in the code, I'm trying to say, from the start point identified above, check the last 5 candles.  If any are higher than the identified candle, then start again another candle back.  Repeat this until the identified candle is higher than the previous 5 candles before it.

I don't know if this is the most efficient way to accomplish this.  And I apologize with the way that I'm explaining it.  I'm not sure how to properly refer to things yet.  Any help would be appreciated.  How do I identify that last swing high on the MACD?

double MACDValueCurrent = NormalizeDouble(iMACD(NULL,PERIOD_CURRENT,12,26,9,PRICE_CLOSE,MODE_MAIN,0),6);
double MACDValuePrevious; 
double MACDSameLevel; //this variable is the last time that the MACD value was at the same level as the current level.
int MACDSameLevelCandle; //this is the candle number that the MACD value arrived higher than the current MACD value.

void OnStart()
{
Alert("");
   
   //find the last time MACD was at the same level that it is currently at.
   for(int i=1; i<100; i++)
   {
   MACDValuePrevious = NormalizeDouble(iMACD(NULL,PERIOD_CURRENT,12,26,9,PRICE_CLOSE,MODE_MAIN,i),6);     
     
      if(MACDValueCurrent < MACDValuePrevious)
      {
      MACDSameLevel = MACDValuePrevious; 
      MACDSameLevelCandle = i;     
      break;        
      } 
   } 
   
   //from above level, find the last time MACD had a swing high. I'm doing this to ignore any peaks or valleys
   //between the current MACD level and this level.
   
   double MACDSwingHigh;
   double MACDDivLevel; //MACD divergence level (not necessarily divergence, but it's what I'm looking for so it's what
                        //I will call this level.
   
   for(int i = MACDSameLevelCandle; i<100; i++)
   {
   MACDDivLevel = iMACD(NULL,PERIOD_CURRENT,12,26,9,PRICE_CLOSE,MODE_MAIN,i);
   
   // I thought I was saying, if MACDDivLevel is higher than the last 5 candles, stop looping and note the candle number
   // as well as the level that the MACD is highest.  Not getting that result.
      if(MACDDivLevel > iMACD(NULL,PERIOD_CURRENT,12,26,9,PRICE_CLOSE,MODE_MAIN,i-1)&&
         MACDDivLevel > iMACD(NULL,PERIOD_CURRENT,12,26,9,PRICE_CLOSE,MODE_MAIN,i-3)&&
         MACDDivLevel > iMACD(NULL,PERIOD_CURRENT,12,26,9,PRICE_CLOSE,MODE_MAIN,i-4)&&
         MACDDivLevel > iMACD(NULL,PERIOD_CURRENT,12,26,9,PRICE_CLOSE,MODE_MAIN,i-5))
         {
         MACDSwingHigh = MACDDivLevel;
         MACDSameLevelCandle = i;
         break;
         }
   }
Alert("MACD same level candle " + MACDSameLevelCandle);           
Alert("MACD swing high = " + MACDSwingHigh);
}