//+------------------------------------------------------------------+
//|                                             Super-signals_v3.mq4 |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 11
#property indicator_color1 clrRed
#property indicator_color2 clrLime
#property indicator_color3 clrRed
#property indicator_color4 clrLime
#property indicator_color5 clrNONE
#property indicator_color6 clrNONE
#property indicator_color7 clrNONE
#property indicator_color8 clrGray
#property indicator_color9 clrRed
#property indicator_color10 clrLime
#property indicator_color11 clrGray
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 1
#property indicator_width6 1
#property indicator_width7 1
#property indicator_width8 1

extern int  dist1           = 72;
extern int  shift1          = 36;
extern int  dist2           = 288;
extern int  shift2          = 144;
extern bool autoAdjust      = true;//false
extern bool alertsOn        = false;
extern bool alertsMessage   = false;
extern bool alertsSound     = false;
extern bool alertsEmail     = false;
extern string soundFile     ="alert2.wav";
 bool Basso     = true;
double b1[],b2[],b3[],b4[],b5[],b6[],b7[],b8[];
double Up[],Dn[],Price[];
int    TimeFrame;

//---------------------------

int init()
{
   TimeFrame = MathMax(TimeFrame,_Period);
   
   if (autoAdjust)
      {
         if (TimeFrame==5)  {dist1=72;shift1=36;dist2=288;shift2=144;}
         else if (TimeFrame==15) {dist1=24;shift1=12;dist2=96;shift2=48;}
         else if (TimeFrame==30) {dist1=24;shift1=12;dist2=48;shift2=24;}
         else {dist1=24;shift1=12;dist2=24;shift2=12;}
      }
   
   IndicatorBuffers(11);
   SetIndexBuffer(0,b1); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,164);
   SetIndexBuffer(1,b2); SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,164);
   SetIndexBuffer(2,b3); SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,164);
   SetIndexBuffer(3,b4); SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,164);
   SetIndexBuffer(4,b5); SetIndexLabel(4,"Upper weak channel");
   SetIndexBuffer(5,b6); SetIndexLabel(5,"Lower weak channel");
   SetIndexBuffer(6,b7); SetIndexLabel(6,"Upper Main channel");
   SetIndexBuffer(7,b8); SetIndexLabel(7,"Lower Main channel");
   SetIndexBuffer(8,Up); SetIndexLabel(8,"Upper");
   SetIndexBuffer(9,Dn); SetIndexLabel(9,"Lower");
  SetIndexBuffer(10,Price); SetIndexLabel(10,"Price");
   
   IndicatorShortName(timeFrameToString(TimeFrame)+" Super-signals ("+dist1+","+dist2+")");
   return(0);
}

//----------------------------

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit,hhb1,llb1,hhb2,llb2;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           limit=MathMax(limit,dist2);

   for (i=limit;i>=0;i--)
      {
         hhb1 = Highest(NULL,0,MODE_HIGH,dist1,i-shift1);
         llb1 = Lowest(NULL,0,MODE_LOW,dist1,i-shift1);
         hhb2 = Highest(NULL,0,MODE_HIGH,dist2,i-shift2);
         llb2 = Lowest(NULL,0,MODE_LOW,dist2,i-shift2);
        

         b1[i] = EMPTY_VALUE;
         b2[i] = EMPTY_VALUE;
         b3[i] = EMPTY_VALUE;
         b4[i] = EMPTY_VALUE;
         b5[i] = High[hhb1];
         b6[i] = Low[llb1];
         b7[i] = High[hhb2];
         b8[i] = Low[llb2];
      
         if (i==hhb1) b1[i]=High[hhb1];
         if (i==llb1) b2[i]=Low[llb1];
         if (i==hhb2) b3[i]=High[hhb2];
         if (i==llb2) b4[i]=Low[llb2] ;
         
         
         Up[i]=Close[i]/Point-b5[i]/Point;
         
          Dn[i]=Close[i]/Point-b6[i]/Point;
          
          
        Price[i]=  Close[i]/Point-(b5[i]/Point+b6[i]/Point)/2;
      }

//-------------------------------------------------------------
 
   if (alertsOn)
      {
         if (b1[1] != EMPTY_VALUE && b3[1] != EMPTY_VALUE) if(Basso)doAlert(" @ 1 hour channel");
         
         if (b2[1] != EMPTY_VALUE && b4[1] != EMPTY_VALUE) doAlert(" @ 1 hour channel");
         if (b1[1] != EMPTY_VALUE && b3[1] == EMPTY_VALUE) if(Basso)doAlert(" @ 15 min channel");
         if (b2[1] != EMPTY_VALUE && b4[1] == EMPTY_VALUE) doAlert(" @ 15 min channel");
      }
   return(0);
}

//-------------------------------------------------------------------------------------

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          message = timeFrameToString(TimeFrame)+" Super-signals "+Symbol()+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"Super-signals "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//-------------------------------------------------------------------------------------

string sTfTable[] = {"M1","M2","M3","M5","M10","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,2,3,5,10,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//-------------------------------------------------------------------------------------