//+------------------------------------------------------------------+
//|                                         CCITrend.mq4 |
//|                                                              JMF |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "JMF"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2  Green  
#property indicator_color3  Red 
#property indicator_color4 Yellow
#property indicator_color5 Yellow 
#property indicator_width1 0
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
#property indicator_minimum 0
#property indicator_maximum 1
//----
extern int CCI_Period = 50;
extern int Threshold1 = 0;
extern int Threshold2 = 200;

double cci[];
double cciHup[];
double cciHdn[];
double cciHup2[];
double cciHdn2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators setting
    SetIndexBuffer(0, cci);
    SetIndexBuffer(1, cciHup);
    SetIndexBuffer(2, cciHdn);
    SetIndexBuffer(3, cciHup2);
    SetIndexBuffer(4, cciHdn2);
//----
    SetIndexStyle(0, DRAW_NONE);
    SetIndexStyle(1, DRAW_HISTOGRAM);
    SetIndexStyle(2, DRAW_HISTOGRAM);
    SetIndexStyle(3, DRAW_HISTOGRAM);
    SetIndexStyle(4, DRAW_HISTOGRAM);
//----        
    IndicatorShortName("Stretch");
    SetIndexLabel(0, "Stretch");     
   SetIndexLabel(1, NULL);
   SetIndexLabel(2, NULL);  
   SetIndexLabel(3, NULL);
   SetIndexLabel(4, NULL); 

//----
    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) 
       return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) 
       counted_bars--;
   limit = Bars - counted_bars;  
//---- indicator calculation
   for(int i = Bars - 1; i >= 0; i--)
     {   
       cci[i] = iCCI(NULL, 0, CCI_Period, PRICE_TYPICAL, i);
       //----
       if(cci[i] >= Threshold1)
           cciHup[i] = 1;//cci[i];
       else
           cciHup[i] = 0;
        if (cci[i] >= Threshold2)
           cciHup2[i] = 1;//cci[i];
        else
           cciHup2[i] = 0;//cci[i];  
       //----
       if(cci[i] < -Threshold1)
           cciHdn[i] = 1;//cci[i];
       else
           cciHdn[i] = 0; 
       if(cci[i] < -Threshold2)
           cciHdn2[i] = 1;//cci[i];
       else   
           cciHdn2[i] = 0;//cci[i];
       
     }   
//----
   return(0);
  }

