//+------------------------------------------------------------------+
//|                                             Super-signals_v3.mq4 |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 16

#property indicator_color13 clrRed
#property indicator_color14 clrLime
#property indicator_color15 clrRed
#property indicator_color16 clrLime

#property indicator_width13 2
#property indicator_width14 2
#property indicator_width15 4
#property indicator_width16 4

enum enColorOnma
{
   cc_onfast,   // Change color on fast channel
   cc_onslow,   // Change color on slow channel
};

extern enColorOnma       colorOnma                   = cc_onslow;      // Ribbon colors on:
extern int  PeriodWeakChannel = 24;
extern int  PeriodMainChannel = 96;
input int                arrwidth1    = 2;               // Short arrows   width
input color              arr_down_color_1       = clrRed;  //Short arrows up  color
input int               code_down_1    = 234;               // Code for short up arrows(164)
input color              arr_up_color_1       = clrLime;  //Short arrows down  color
input int               code_up_1    = 233;               // Code for short down arrows
input int                arrwidth2    = 6;               // Long arrows   width
input color              arr_down_color2       = clrRed;  //  Long arrows   color
input int                code_down_2    = 222;               // Code for long arrows
input color              arr_up_color_2       = clrLime;  //  Long arrows   color
input int                code_up_2    = 221;               // Code for long arrows
extern bool              Show_ribbon   = true;       //  Ribbon fill visible?
extern color                 Color_Up        = C'0,120,0';   // Color up
extern color                 Color_Dn        = C'120,0,0';   // Color down
input int                    Ribbon_Width      =   1;                // Width of ribbon
extern bool alertsOn        = true;
extern bool alertsMessage   = true;
extern bool alertsSound     = false;
extern bool alertsEmail     = false;
extern string soundFile     ="alert2.wav";

double b1[],b2[],b3[],b4[],b5[],b6[],b7[],b8[];
double filluu[],fillud[],filldd[],filldu[],filluu2[],fillud2[],filldd2[],filldu2[],trend[];

int    TimeFrame;
int    shift1=PeriodWeakChannel/2;
int    shift2=PeriodMainChannel/2;

//---------------------------

int init()
{
   TimeFrame = MathMax(TimeFrame,_Period);
   IndicatorBuffers(17);
   
   SetIndexBuffer(0,filluu);  SetIndexStyle(0,Show_ribbon ? DRAW_HISTOGRAM :  DRAW_NONE,NULL,Ribbon_Width,Color_Up);
   SetIndexBuffer(1,fillud);  SetIndexStyle(1,Show_ribbon ? DRAW_HISTOGRAM :  DRAW_NONE,NULL,Ribbon_Width,Color_Up);
   SetIndexBuffer(2,filldd);  SetIndexStyle(2,Show_ribbon ? DRAW_HISTOGRAM :  DRAW_NONE,NULL,Ribbon_Width,Color_Dn);
   SetIndexBuffer(3,filldu);  SetIndexStyle(3,Show_ribbon ? DRAW_HISTOGRAM :  DRAW_NONE,NULL,Ribbon_Width,Color_Dn);
   SetIndexBuffer(4,filluu2); SetIndexStyle(4,Show_ribbon ? DRAW_HISTOGRAM :  DRAW_NONE,NULL,Ribbon_Width,Color_Up);
   SetIndexBuffer(5,fillud2); SetIndexStyle(5,Show_ribbon ? DRAW_HISTOGRAM :  DRAW_NONE,NULL,Ribbon_Width,Color_Up);
   SetIndexBuffer(6,filldd2); SetIndexStyle(6,Show_ribbon ? DRAW_HISTOGRAM :  DRAW_NONE,NULL,Ribbon_Width,Color_Dn);
   SetIndexBuffer(7,filldu2); SetIndexStyle(7,Show_ribbon ? DRAW_HISTOGRAM :  DRAW_NONE,NULL,Ribbon_Width,Color_Dn);
   
   SetIndexBuffer(8,b1);  SetIndexStyle(8, DRAW_ARROW,NULL,arrwidth1,arr_down_color_1); SetIndexArrow(8,code_down_1);
   SetIndexBuffer(9,b2);  SetIndexStyle(9, DRAW_ARROW,NULL,arrwidth1,arr_up_color_1);   SetIndexArrow(9,code_up_1);
   SetIndexBuffer(10,b3); SetIndexStyle(10,DRAW_ARROW,NULL,arrwidth2,arr_down_color2); SetIndexArrow(10,code_down_2);
   SetIndexBuffer(11,b4); SetIndexStyle(11,DRAW_ARROW,NULL,arrwidth2,arr_up_color_2);  SetIndexArrow(11,code_up_2);
   
   SetIndexBuffer(12,b5); SetIndexStyle(12,DRAW_LINE); SetIndexLabel(12,"Upper weak channel");
   SetIndexBuffer(13,b6); SetIndexStyle(13,DRAW_LINE); SetIndexLabel(13,"Lower weak channel");
   SetIndexBuffer(14,b7); SetIndexStyle(14,DRAW_LINE); SetIndexLabel(14,"Upper Main channel");
   SetIndexBuffer(15,b8); SetIndexStyle(15,DRAW_LINE); SetIndexLabel(15,"Lower Main channel");
   
   SetIndexBuffer(16,trend);
   
   IndicatorShortName(timeFrameToString(TimeFrame)+" Super-signals ("+PeriodWeakChannel+","+PeriodMainChannel+")");
   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,PeriodMainChannel);

   for (i=limit;i>=0;i--)
      {
         hhb1 = Highest(NULL,0,MODE_HIGH,PeriodWeakChannel,i-shift1);
         llb1 = Lowest(NULL,0,MODE_LOW,PeriodWeakChannel,i-shift1);
         hhb2 = Highest(NULL,0,MODE_HIGH,PeriodMainChannel,i-shift2);
         llb2 = Lowest(NULL,0,MODE_LOW,PeriodMainChannel,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] ;
         
         switch(colorOnma)
         {
            case cc_onfast:               trend[i]  = (i<Bars-1) ? (b5[i]>b5[i+1] || b6[i]>b6[i+1]) ? 1 : (b5[i]<b5[i+1] || b6[i]<b6[i+1]) ? -1 : trend[i+1]  : 0 ; break;
            default :  if (i<Bars-1)      trend[i]  = (i<Bars-1) ? (b7[i]>b7[i+1] || b8[i]>b8[i+1]) ? 1 : (b7[i]<b7[i+1] || b8[i]<b8[i+1]) ? -1 : trend[i+1]  : 0 ; 
         }
         
        
        
         filluu[i]  = fillud[i]  = EMPTY_VALUE;
         filldd[i]  = filldu[i]  = EMPTY_VALUE; 
         filluu2[i] = fillud2[i] = EMPTY_VALUE;
         filldd2[i] = filldu2[i] = EMPTY_VALUE;  
          
        if ( trend[i]== 1 )  {filluu[i] = b7[i]; fillud[i] = b5[i];  filluu2[i] = b8[i]; fillud2[i] = b6[i]; }
        if ( trend[i]==-1)   {filldd[i] = b8[i]; filldu[i] = b6[i];  filldd2[i] = b7[i]; filldu2[i] = b5[i];}    
        
      }

//-------------------------------------------------------------
 
   if (alertsOn)
      {
         if (b1[1] != EMPTY_VALUE && b3[1] != EMPTY_VALUE) doAlert(" @ MAIN channel");
         if (b2[1] != EMPTY_VALUE && b4[1] != EMPTY_VALUE) doAlert(" @ MAIN channel");
         if (b1[1] != EMPTY_VALUE && b3[1] == EMPTY_VALUE) doAlert(" @ WEAK channel");
         if (b2[1] != EMPTY_VALUE && b4[1] == EMPTY_VALUE) doAlert(" @ WEAK 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("");
}

//-------------------------------------------------------------------------------------