//+------------------------------------------------------------------+
//|      _TRO_Channel                                RSI channel.mq4 |
//|                                                           mladen |
//|                                                                  |
//|                                                                  |
//| original idea for this indicator found on                        |
//|     http://www.fxexpert.ru (RSI-2 posted by Dm_35)               |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  DodgerBlue
#property indicator_color2  Green
#property indicator_color3  Red
#property indicator_color4  Orange
#property indicator_width4  2
#property indicator_style1  STYLE_DOT

 


extern int    HighLowPeriod = 5;

extern bool   ShowChannel   = false;
extern bool   ShowZigZag    = true;

 

double xBuffer[];
double HighBuffer[];
double LowBuffer[];
double ZigZagBuffer[];
double ZigZagLow[];
double ZigZagHigh[];




//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
 

int init()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0,ZigZagBuffer);
 
   SetIndexBuffer(1,HighBuffer);
   SetIndexBuffer(2,LowBuffer);
   SetIndexBuffer(3,xBuffer);
   SetIndexBuffer(4,ZigZagLow);
   SetIndexBuffer(5,ZigZagHigh);
 
         SetIndexStyle(0,DRAW_SECTION);
         SetIndexEmptyValue(0,0);
         SetIndexLabel(0,NULL);
                  
          
         SetIndexEmptyValue(3,0);
         SetIndexEmptyValue(4,0);
         SetIndexEmptyValue(5,0);


         if (ShowChannel)
            {
               SetIndexStyle(1,DRAW_LINE);
               SetIndexStyle(2,DRAW_LINE);
            }
         else      
            {
               SetIndexStyle(1,DRAW_NONE);
               SetIndexStyle(2,DRAW_NONE);
               SetIndexStyle(3,DRAW_NONE);               
            }


   IndicatorShortName("_TRO_Channel");
   return(0);
}
int deinit()
{
   return(0);
}  

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars = IndicatorCounted();
   int lastZag;
   int i,limit;


   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;


      if (ShowZigZag)
      for (lastZag=limit+1; lastZag<Bars; lastZag++) if (ZigZagLow[lastZag] != 0 || ZigZagHigh[lastZag] != 0) break;


   for(i=limit; i>=0; i--) 
   {
      xBuffer[i] = iClose(NULL,0,i); 
      
      LowBuffer[i]   = xBuffer[ArrayMinimum(xBuffer,HighLowPeriod,i)];
      HighBuffer[i]  = xBuffer[ArrayMaximum(xBuffer,HighLowPeriod,i)];
         if (!ShowZigZag) continue;


         
         ZigZagLow[i]  = 0;
         ZigZagHigh[i] = 0;
         if (LowBuffer[i] < LowBuffer[i+1])
            {
               if (ZigZagBuffer[lastZag] == LowBuffer[lastZag])
                   ZigZagBuffer[lastZag] = 0;
                   ZigZagBuffer[i]       =  LowBuffer[i];
                   ZigZagLow[i]          =  LowBuffer[i];
                                 lastZag = i;
                                 continue;
            }
         if (HighBuffer[i] > HighBuffer[i+1])
            {
               if (ZigZagBuffer[lastZag] == HighBuffer[lastZag])
                   ZigZagBuffer[lastZag] = 0;
                   ZigZagBuffer[i]       =  HighBuffer[i];
                   ZigZagHigh[i]         =  HighBuffer[i];
                                 lastZag = i;
                                 continue;
            }
         if (ZigZagBuffer[i] != 0)
            {
               ZigZagBuffer[i] = 0;
               for (lastZag=i+1; lastZag<Bars; lastZag++)
               {
                  if (ZigZagLow[lastZag]  != 0) { ZigZagBuffer[lastZag] = ZigZagLow[lastZag];  break; }
                  if (ZigZagHigh[lastZag] != 0) { ZigZagBuffer[lastZag] = ZigZagHigh[lastZag]; break; }
               }
            }
   }


      
   return(0);
}


