//+------------------------------------------------------------------+
//|                                                   Divergence.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int    candles       = 10;         // How many candles look back
input int    start_from    = 1;         // Where the search start

bool   bdivergence       = false;
bool   sdivergence       = false; 
double highest, lowest;
string name1, name2;

//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
void OnTick()
  {
    
      // Check the last x candles highest shift
      int highindex = iHighest(NULL,0,MODE_HIGH,candles,start_from);
      if( highindex != -1 )
          highest = High[highindex];

      // Check the last x candles lowest shift
      int lowindex  = iLowest(NULL,0,MODE_LOW,candles,start_from);
      if( lowindex != -1 )
          lowest=Low[lowindex];

      // Look for LL label trendline, buysignal
      name1 = "LL" + TimeToString(Time[lowindex]);
      if( ObjectFind(1,name1) == 0)
         {bdivergence = true;}
      else
         {bdivergence = false;}

      // Look for HL label trendline, sellsignal
      name2 = "HL" + TimeToString(Time[highindex]);
      if( ObjectFind(1,name2) == 0)
         {sdivergence = true;}
      else
         {sdivergence = false;}
        
      Comment("Buysignal : " + IntegerToString(bdivergence),
              "\nSellsignal : " + IntegerToString(sdivergence));

}
   
// Custom indicator
double getdivergence()
  {
   return(iCustom(NULL,0,"Divergence",0,1));
  }
