
#property indicator_separate_window

#property indicator_buffers 3

#property indicator_minimum 0
#property indicator_maximum 100
#define  BUY   1
#define  SELL -1
#property indicator_color1  Yellow
#property indicator_color2  Blue
#property indicator_color3  Red
#property indicator_width2 2
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
extern int FastMA=12;
extern int SlowMA=24;
extern int Crosses=50;
extern bool ShowAllDots=true;
extern bool AlertOn=false;
bool TurnedUp = false;
bool TurnedDown = false;
// buffers
double Values[];           // values
double ExtremumsUp[];        // extremums
double ExtremumsDown[];        // extremums



int init()
{
 IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
IndicatorBuffers(3);
   SetIndexBuffer(0, Values);
   SetIndexBuffer(1, ExtremumsUp);
    SetIndexBuffer(2, ExtremumsDown);

   // buffers settings
   SetIndexStyle(0, DRAW_LINE);     // values are drawn as a solid line

   SetIndexStyle(1, DRAW_ARROW);    // extremums are drawn as circles
   SetIndexArrow(1, 159);
    SetIndexStyle(2, DRAW_ARROW);    // extremums are drawn as circles
   SetIndexArrow(2, 159);
   
  

   return(0);
}

int start()
{
 int LastSignal,LastAlert;
   int i, limit, counted_bars=IndicatorCounted();
   if (counted_bars == 0)  
    limit = Bars - counted_bars;
   if (counted_bars > 0)  
    limit = Bars - counted_bars;
  
 for ( i = limit; i >=0; i-- ) 
   {
      Values[i] = iCustom(Symbol(),0,"Cycle_KROUFR_version",FastMA,SlowMA,Crosses ,0, i);
   }
      
 for ( i = limit; i >=0; i-- ) 
  
   {
      ExtremumsUp[i ] = EMPTY_VALUE;
      ExtremumsDown[i ] = EMPTY_VALUE;

      // extremum condition -- simple case
      if ((Values[i] > Values[i + 1]) && (Values[i + 1] < Values[i + 2]))
      {
         // if available
         ExtremumsUp[i + 1] = Values[i + 1];
         continue;
      }
       if ((Values[i] < Values[i + 1]) && (Values[i + 1] > Values[i + 2]))
      {
         // if available
         ExtremumsDown[i + 1] = Values[i + 1];
         continue;
      }
      // extremum condition -- complicated case
      if (Values[i + 1] == Values[i + 1] && Values[i] != Values[i + 1])
      {
         // potential one is available

         int index = i + 1;
         bool found = false;
         while (index < Bars && Values[index] != EMPTY_VALUE)
         {
            if (Values[i + 1] != Values[index])
            {
               // got the end
               found = true;
               break;
            }
            
            index++;
         }

         if (!found)
         {
            // not got the end
            continue;
         }
         
        if(ShowAllDots==false){
        if (i!=0){
         if ((Values[i] > Values[i + 1]) && (Values[i + 1] < Values[i + 2]) &&  LastSignal!=BUY)
         {
          LastSignal=BUY;
            
            ExtremumsUp[i + 1] = Values[i + 1];
             if  (AlertOn   && i==1 && !TurnedUp )
           { 
            Alert(WindowExpertName()+" "+"BUY"+" "+Symbol()," ",Period()+" "+"minute charts");
                       TurnedUp = true;
            TurnedDown = false;
     }
         }  
        if ((Values[i] < Values[i + 1]) && (Values[i + 1] > Values[i + 2])   && LastSignal!=SELL)
         {
              LastSignal=SELL;
            ExtremumsDown[i + 1] = Values[i + 1];
            if  (AlertOn  &&  i==1 &&!TurnedDown )
           { 
            Alert(WindowExpertName()+" "+"SELL"+" "+Symbol()," ",Period()+" "+"minute charts");
                      
             TurnedUp =false ;
            TurnedDown = true;
     }  
         }
         }
         }
         else if (ShowAllDots==true){
         if (i!=0){
               if ((Values[i] > Values[i + 1]) && (Values[i + 1] < Values[i + 2]))
         {
         
            
            ExtremumsUp[i + 1] = Values[i + 1];
             if  (AlertOn   && i==1 && !TurnedUp )
           { 
            Alert(WindowExpertName()+" "+"BUY"+" "+Symbol()," ",Period()+" "+"minute charts");
                       TurnedUp = true;
            TurnedDown = false;
     }
         }  
        if ((Values[i] < Values[i + 1]) && (Values[i + 1] > Values[i + 2]))
         {
            
            ExtremumsDown[i + 1] = Values[i + 1];
            if  (AlertOn  &&  i==1 &&!TurnedDown )
           { 
            Alert(WindowExpertName()+" "+"SELL"+" "+Symbol()," ",Period()+" "+"minute charts");
                      
             TurnedUp =false ;
            TurnedDown = true;
     }  }
         }
         
         }  
      }
   }
   
   return(0);
}