///+------------------------------------------------------------------+ 
// FXi 4 Semafor
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color8 Blue
#property indicator_color7 FireBrick
#property indicator_color6 DodgerBlue
#property indicator_color5 Red
#property indicator_color4 DodgerBlue
#property indicator_color3 Magenta
#property indicator_color2 Black
#property indicator_color1 Black
//---- input parameters 
extern double Period1=5; 
extern double Period2=12; 
extern double Period3=34; 
extern double Period4=233;
extern int Width1 = 3;
extern int Width2 = 0;
extern int Width3 = 2;
extern int Width4 = 2;
int Stp=1;
int Dev=1; 
extern int Symbol_1_Kod=158;
extern int Symbol_20_Kod=159;
extern int Symbol_21_Kod=159;
extern int Symbol_3_Kod=82;
extern int Symbol_4_Kod=168;
extern int BarsCount = 3000;
//+----    mod of  TRO MODIFICATION ------------------------+ 
extern string _____           ="       Оповещение";
extern bool   ShowAlert    = false;
//---- buffers 
double FP_BuferUp[], FP_BuferDn[], NP_BuferUp[], NP_BuferDn[], HP_BuferUp[], HP_BuferDn[], VP_BuferUp[], VP_BuferDn[]; 
//+------------------------------------------------------------------+  
string symbol;  
int    digits, period, key  ; 
color tColor = Yellow ;
//+------------------------------------------------------------------+ 
int init()   { 
   period       = Period() ;     
   symbol       =  Symbol() ;
   digits       =  Digits ;  
   key=0;
   IndicatorDigits(5); 
//+------------------------------------------------------------------+   
   if (Period4>0)   {
     SetIndexBuffer(7,VP_BuferUp);   SetIndexStyle(7,DRAW_ARROW,EMPTY,Width4);   SetIndexArrow(7,Symbol_4_Kod);   SetIndexEmptyValue(7,0);  SetIndexLabel(7,"VSlowUp");
     SetIndexBuffer(6,VP_BuferDn);   SetIndexStyle(6,DRAW_ARROW,EMPTY,Width4);   SetIndexArrow(6,Symbol_4_Kod);   SetIndexEmptyValue(6,0);  SetIndexLabel(6,"VSlowDn");
   }
   if (Period3>0)   {
     SetIndexBuffer(5,HP_BuferUp);   SetIndexStyle(5,DRAW_ARROW,EMPTY,Width3);   SetIndexArrow(5,Symbol_3_Kod);   SetIndexEmptyValue(5,0);  SetIndexLabel(5,"SlowUp");
     SetIndexBuffer(4,HP_BuferDn);   SetIndexStyle(4,DRAW_ARROW,EMPTY,Width3);   SetIndexArrow(4,Symbol_3_Kod);   SetIndexEmptyValue(3,0);  SetIndexLabel(4,"SlowDn");
   }
   if (Period2>0)   {
     SetIndexBuffer(3,NP_BuferUp);   SetIndexStyle(3,DRAW_ARROW,EMPTY,Width2);   SetIndexArrow(3,Symbol_20_Kod);   SetIndexEmptyValue(3,0);  SetIndexLabel(3,"MiddleUp");
     SetIndexBuffer(2,NP_BuferDn);   SetIndexStyle(2,DRAW_ARROW,EMPTY,Width2);   SetIndexArrow(2,Symbol_21_Kod);   SetIndexEmptyValue(2,0);  SetIndexLabel(2,"MiddleDn");
   }
   if (Period1>0)   {
     SetIndexBuffer(1,FP_BuferUp);   SetIndexStyle(1,DRAW_ARROW,EMPTY,Width1);   SetIndexArrow(1,Symbol_1_Kod);   SetIndexEmptyValue(1,0);  SetIndexLabel(1,"FastUp");
     SetIndexBuffer(0,FP_BuferDn);   SetIndexStyle(0,DRAW_ARROW,EMPTY,Width1);   SetIndexArrow(0,Symbol_1_Kod);   SetIndexEmptyValue(0,0);  SetIndexLabel(0,"FastDn");
   }   
   return(0); 
  } 
//+------------------------------------------------------------------+ 
int deinit()   {    return(0);   } 
//+------------------------------------------------------------------+ 
int start()   { 
//+------------------------------------------------------------------+    
   if (Period1>0) CountZZ(FP_BuferUp,FP_BuferDn,Period1,Dev,Stp);
   if (Period2>0) CountZZ(NP_BuferUp,NP_BuferDn,Period2,Dev,Stp);
   if (Period3>0) CountZZ(HP_BuferUp,HP_BuferDn,Period3,Dev,Stp);
   if (Period4>0) CountZZ(VP_BuferUp,VP_BuferDn,Period4,Dev,Stp);
//+------------------------------------------------------------------+  
   if ( ShowAlert && key!=Time[0])    {     
     if( VP_BuferUp[1] != 0 )   Alert(" Старший Светик на  " + symbol+"   "+ Period()+ "   на цене  "+ DoubleToStr(Close[0] ,digits));
     if( VP_BuferDn[1] != 0 )   Alert(" Старший Светик на  " + symbol+"   "+ Period()+ "   на цене  "+ DoubleToStr(Close[0] ,digits));
   }
   key=Time[0];   
//+------------------------------------------------------------------+  
   return(0);
}
//+------------------------------------------------------------------+   
int CountZZ( double& ExtMapBuffer[], double& ExtMapBuffer2[], int ExtDepth, int ExtDeviation, int ExtBackstep )
  {
   int    shift, back,lasthighpos,lastlowpos, limit;
   double val,res;
   double curlow,curhigh,lasthigh,lastlow;
   limit=Bars-ExtDepth;
   if(limit>BarsCount) limit=BarsCount;   
   for(shift=limit; shift>=0; shift--)
     {
      val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(val==lastlow) val=00;
      else 
        { 
         lastlow=val; 
         if((Low[shift]-val)>(ExtDeviation*Point)) val=00;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer[shift+back];
               if((res!=0)&&(res>val)) ExtMapBuffer[shift+back]=00; 
              }
           }
        } 
        
          ExtMapBuffer[shift]=val;
      //--- high
      val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(val==lasthigh) val=00;
      else 
        {
         lasthigh=val;
         if((val-High[shift])>(ExtDeviation*Point)) val=00;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=ExtMapBuffer2[shift+back];
               if((res!=0)&&(res<val)) ExtMapBuffer2[shift+back]=00; 
              } 
           }
        }
      ExtMapBuffer2[shift]=val;
     }
   // final cutting 
   lasthigh=-1; lasthighpos=-1;
   lastlow=-1;  lastlowpos=-1;

   for(shift=limit; shift>=0; shift--)
     {
      curlow=ExtMapBuffer[shift];
      curhigh=ExtMapBuffer2[shift];
      if((curlow==0)&&(curhigh==0)) continue;
      //---
      if(curhigh!=0)
        {
         if(lasthigh>0) 
           {
            if(lasthigh<curhigh) ExtMapBuffer2[lasthighpos]=0;
            else ExtMapBuffer2[shift]=0;
           }
         //---
         if(lasthigh<curhigh || lasthigh<0)
           {
            lasthigh=curhigh;
            lasthighpos=shift;
           }
         lastlow=-1;
        }
      //----
      if(curlow!=0)
        {
         if(lastlow>0)
           {
            if(lastlow>curlow) ExtMapBuffer[lastlowpos]=0;
            else ExtMapBuffer[shift]=0;
           }
         //---
         if((curlow<lastlow)||(lastlow<0))
           {
            lastlow=curlow;
            lastlowpos=shift;
           } 
         lasthigh=-1;
        }
     }
  
   for(shift=limit; shift>=0; shift--)
     {
      if(shift>=limit) ExtMapBuffer[shift]=00;
      else
        {
         res=ExtMapBuffer2[shift];
         if(res!=00) ExtMapBuffer2[shift]=res;
        }
     }
   return(0);
 }
//+------------------------------------------------------------------+   
