#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 Red

extern int     CCPeriod = 21;
extern int     ATRPeriod = 5;
extern double  ATR_Multiple = 0.7;
double bufGreen[];
double bufRed[];


int init()
{
  {SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1);
   SetIndexBuffer(0, bufGreen);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1);
   SetIndexBuffer(1, bufRed);}
   return (0);
  }

int deinit() 
{
   return (0);
}

int start()
{
   double dCCiNormal;
   double dCCiShift1;
   int counted = IndicatorCounted();
   if (counted < 0) return (-1);
   if (counted > 0) counted--;
   int li_0 = Bars - counted;

   for (int i = li_0; i >= 0; i--)
   {
      dCCiNormal = iCCI(NULL, 0, CCPeriod, PRICE_TYPICAL, i);
      dCCiShift1 = iCCI(NULL, 0, CCPeriod, PRICE_TYPICAL, i + 1);

      if (dCCiNormal >= 0 && dCCiShift1 < 0)    bufGreen[i + 1] = bufRed[i + 1];         
      if (dCCiNormal <= 0 && dCCiShift1 > 0)    bufRed[i + 1]   = bufGreen[i + 1];
 
      if (dCCiNormal >= 0)
      {
         bufGreen[i] = (Low[i]+Close[i])/2 - iATR(NULL, 0, ATRPeriod,i)*ATR_Multiple;
         if (bufGreen[i] < bufGreen[i + 1] && Close[i] > bufGreen[i])  bufGreen[i] = bufGreen[i + 1];
      }
      else
      {
         if (dCCiNormal <= 0)
         {
          bufRed[i] = (High[i]+Close[i])/2 + iATR(NULL, 0, ATRPeriod,i)*ATR_Multiple;
          if (bufRed[i] > bufRed[i + 1] && Close[i] < bufRed[i])  bufRed[i] = bufRed[i + 1]; 
         }
      }
   }
   return (0);
}