//+------------------------------------------------------------------
//|
//+------------------------------------------------------------------

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  DeepSkyBlue
#property indicator_color2  PaleVioletRed
#property indicator_width1  2
#property indicator_minimum 0

//
//
//
//
//

extern int    CmiPeriod = 60;
extern int    AvgPeriod = 10;
extern int    AvgMethod = MODE_SMA;
extern double Level1    = 40;
extern double Level2    = 50;
extern double Level3    = 60;

double CmiBuffer[];
double AvgBuffer[];

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,CmiBuffer);
   SetIndexBuffer(1,AvgBuffer);
      SetLevelValue(0,Level1);
      SetLevelValue(2,Level2);
      SetLevelValue(3,Level3);
      IndicatorShortName("Choppy market index ("+CmiPeriod+","+AvgPeriod+")");
   return(0);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int i,limit,countedBars = IndicatorCounted();

   if (countedBars<0) return(-1);
   if (countedBars>0) countedBars--;
         limit = MathMin(Bars-countedBars,Bars-1);
   
   //
   //
   //
   //
   //
   
   for(i = limit; i >=0; i--)
   {
      double hi = High[iHighest(NULL,0,MODE_HIGH,CmiPeriod,i)];
      double lo =  Low[iLowest (NULL,0,MODE_LOW ,CmiPeriod,i)];
         if (hi!=lo)
               CmiBuffer[i] = 100.0*MathAbs(Close[i]-Close[i+CmiPeriod])/(hi-lo);
         else  CmiBuffer[i] = 0;
	}
   for(i = limit; i >=0; i--) AvgBuffer[i] = iMAOnArray(CmiBuffer,0,AvgPeriod,0,AvgMethod,i);
	return(0);	
}

