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

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  Khaki
#property indicator_color2  Red
#property indicator_width1  0
#property indicator_width2  3
#property indicator_minimum 0
#property indicator_levelcolor C'70,70,70'

//
//
//
//
//

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;

int    Window    = 1;
int    Corner    = 1;
int    xDistance = 80;
int    yDistance = 10;
int    dig       = 4;

double CmiBuffer[];
double AvgBuffer[];
double Diff[];

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

int init()
{

   SetIndexBuffer(0,CmiBuffer); SetIndexLabel(0,"CMI");
   SetIndexBuffer(1,AvgBuffer); SetIndexLabel(1,"SMA");
   SetIndexBuffer(2,Diff);      SetIndexLabel(2,"DIFF");
      SetLevelValue(0,Level1);
      SetLevelValue(1,Level2);
      SetLevelValue(2,Level3);
      IndicatorShortName("Choppy market index ("+CmiPeriod+","+AvgPeriod+")");
      
       if(ObjectFind ("label_d20bars") == -1)
   
       ObjectCreate("label_d20bars", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_d20bars", OBJPROP_CORNER, Corner);
       ObjectSet("label_d20bars", OBJPROP_XDISTANCE, xDistance);
       ObjectSet("label_d20bars", OBJPROP_YDISTANCE, yDistance);

       if(ObjectFind ("label_diff") == -1)
   
       ObjectCreate("label_diff", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_diff", OBJPROP_CORNER, Corner);
       ObjectSet("label_diff", OBJPROP_XDISTANCE, xDistance-65);
       ObjectSet("label_diff", OBJPROP_YDISTANCE, yDistance);
              
       if (StringFind(Symbol(),"JPY",0) != -1) {dig = 2;}
          
   return(0);
}

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

int deinit()
  {
   
   if(ObjectFind("label_diff")!=-1)    ObjectDelete("label_diff");
   if(ObjectFind("label_d20bars")!=-1) ObjectDelete("label_d20bars");
   
   return(0);
  }
  
  
//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int i,limit,countedBars = IndicatorCounted();
   color Color;
   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);
      Diff[i] = Close[i] - Close[i+20];
      
      if(Diff[i] >= 0) Color = LimeGreen;
      if(Diff[i] == 0) Color = DimGray;
      if(Diff[i] <= 0) Color = Red;
   }
   
   string d20bars = "20 Bars Differential : ";
   string diff =  DoubleToStr(Diff[0],dig);
   ObjectSetText("label_d20bars", d20bars, 12, "Arial Black", DimGray);
   ObjectSetText("label_diff", diff, 12, "Arial Black", Color);
           
	return(0);	
}

