//+------------------------------------------------------------------
//|  original version coded by Mladen for forex-tsd.com
//|  histo version mod by Brax64
//|
//+------------------------------------------------------------------

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1  CLR_NONE
#property indicator_color2  Gainsboro
#property indicator_color3  C'200,0,0'
#property indicator_color4  DimGray
#property indicator_color5  C'35,95,235'
#property indicator_color6  CLR_NONE
#property indicator_width2  4
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  2
#property indicator_minimum 0
#property indicator_levelcolor C'60,60,60'

//
//
//
//
//

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;
extern int    Window    = 1;

//int    Corner    = 1;
int    xDistance = 90;
int    yDistance = 10;
int    dig       = 4;

double CmiBuffer[];
double AvgBuffer[];
double Diff[];
double DrawRange[];
double DrawNoZone[];
double DrawTrend[];

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

int init()
{
   SetIndexBuffer(0,CmiBuffer);  SetIndexLabel(0,"CMI");    SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(1,AvgBuffer);  SetIndexLabel(1,"SMA");    SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,DrawRange);  SetIndexLabel(2,"Range");  SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3,DrawNoZone); SetIndexLabel(3,"NoZone"); SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4,DrawTrend);  SetIndexLabel(4,"Trend");  SetIndexStyle(4,DRAW_HISTOGRAM);
   SetIndexBuffer(5,Diff);       SetIndexLabel(5,"DIFF");   SetIndexStyle(0,DRAW_NONE);
   SetLevelValue(0,Level1); 
   SetLevelValue(1,Level2);
   SetLevelValue(2,Level3);

   IndicatorShortName("Choppy market index histo ("+CmiPeriod+","+AvgPeriod+")");
      
       if(ObjectFind ("label_d20bars") == -1)
   
       ObjectCreate("label_d20bars", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_d20bars", OBJPROP_CORNER, 1);
       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, 1);
       ObjectSet("label_diff", OBJPROP_XDISTANCE, xDistance-80);
       ObjectSet("label_diff", OBJPROP_YDISTANCE, yDistance);
       
       if(ObjectFind ("label_sma") == -1)
   
       ObjectCreate("label_sma", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_sma", OBJPROP_CORNER, 1);
       ObjectSet("label_sma", OBJPROP_XDISTANCE, xDistance+5);
       ObjectSet("label_sma", OBJPROP_YDISTANCE, yDistance+20);
       
       if(ObjectFind ("label_sma_value") == -1)
   
       ObjectCreate("label_sma_value", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_sma_value", OBJPROP_CORNER, 1);
       ObjectSet("label_sma_value", OBJPROP_XDISTANCE, xDistance-80);
       ObjectSet("label_sma_value", OBJPROP_YDISTANCE, yDistance+20);
       
       if(ObjectFind ("label_trend1") == -1)
   
       ObjectCreate("label_trend1", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_trend1", OBJPROP_CORNER, 3);
       ObjectSet("label_trend1", OBJPROP_XDISTANCE, xDistance-80);
       ObjectSet("label_trend1", OBJPROP_YDISTANCE, yDistance-5);
       
       if(ObjectFind ("label_trend2") == -1)
   
       ObjectCreate("label_trend2", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_trend2", OBJPROP_CORNER, 3);
       ObjectSet("label_trend2", OBJPROP_XDISTANCE, xDistance-80);
       ObjectSet("label_trend2", OBJPROP_YDISTANCE, yDistance+10);
       
       if(ObjectFind ("label_range1") == -1)
   
       ObjectCreate("label_range1", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_range1", OBJPROP_CORNER, 3);
       ObjectSet("label_range1", OBJPROP_XDISTANCE, xDistance-80);
       ObjectSet("label_range1", OBJPROP_YDISTANCE, yDistance+25);
       
       if(ObjectFind ("label_range2") == -1)
   
       ObjectCreate("label_range2", OBJ_LABEL, Window, 0, 0);
       ObjectSet("label_range2", OBJPROP_CORNER, 3);
       ObjectSet("label_range2", OBJPROP_XDISTANCE, xDistance-80);
       ObjectSet("label_range2", OBJPROP_YDISTANCE, yDistance+40);
              
       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");
   if(ObjectFind("label_sma")!=-1)       ObjectDelete("label_sma");
   if(ObjectFind("label_sma_value")!=-1) ObjectDelete("label_sma_value");
   if(ObjectFind("label_trend1")!=-1)    ObjectDelete("label_trend1");
   if(ObjectFind("label_trend2")!=-1)    ObjectDelete("label_trend2");
   if(ObjectFind("label_range1")!=-1)    ObjectDelete("label_range1");
   if(ObjectFind("label_range2")!=-1)    ObjectDelete("label_range2");
   
   return(0);
  }
  
  
//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int i,limit,countedBars = IndicatorCounted();
   color ColorDIF;
   color ColorAVG;
   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;

      DrawRange[i]  = EMPTY_VALUE;
      DrawNoZone[i] = EMPTY_VALUE;
      DrawTrend[i]  = EMPTY_VALUE;

   if (CmiBuffer[i] <= 40)
      {
       DrawRange[i]  = CmiBuffer[i];
       DrawNoZone[i] = 0;
       DrawTrend[i]  = 0;
      }
       
    if (CmiBuffer[i] > 40 && CmiBuffer[i] < 60)
      {
       DrawRange[i]  = 0;
       DrawNoZone[i] = CmiBuffer[i];
       DrawTrend[i]  = 0;
      }       
       
    if (CmiBuffer[i] >= 60)
      {
       DrawRange[i]  = 0;
       DrawNoZone[i] = 0;
       DrawTrend[i]  = CmiBuffer[i];
      }
 	}
	
   for(i = limit; i >=0; i--)
   {
      AvgBuffer[i] = iMAOnArray(CmiBuffer,0,AvgPeriod,0,AvgMethod,i);
      
      Diff[i] = Close[i] - Close[i+20];
      
      if(Diff[1] >  0)                           ColorDIF = C'35,95,235';
      if(Diff[1] == 0)                           ColorDIF = DimGray;
      if(Diff[1] <  0)                           ColorDIF = C'200,0,0';
      if(AvgBuffer[1] <= 40)                     ColorAVG = C'200,0,0';
      if(AvgBuffer[1] > 40 && AvgBuffer[1] < 60) ColorAVG = Gold;
      if(AvgBuffer[1] >= 60)                     ColorAVG = C'35,95,235';
   }
   
   string d20bars   = "20 Bars Differential : ";
   string diff      = DoubleToStr(Diff[1],dig);
   string sma       = "SMA(" + AvgPeriod + ") :";
   string sma_value = DoubleToStr(AvgBuffer[1],dig);
   string trend1    = "TREND LONG  if SMA>60 & dif positive";
   string trend2    = "TREND SHORT if SMA>60 & dif negative";
   string range1    = "RANGE LONG  if SMA<40 & dif negative";
   string range2    = "RANGE SHORT if SMA<40 & dif positive";
   ObjectSetText("label_d20bars", d20bars, 12, "Arial Black", DimGray);
   ObjectSetText("label_diff", diff, 12, "Arial Black", ColorDIF);
   ObjectSetText("label_sma", sma, 12, "Arial Black", DimGray);
   ObjectSetText("label_sma_value", sma_value, 12, "Arial Black", ColorAVG);
   ObjectSetText("label_trend1", trend1, 9, "Courier New", C'35,95,235');
   ObjectSetText("label_trend2", trend2, 9, "Courier New", C'35,95,235');
   ObjectSetText("label_range1", range1, 9, "Courier New", C'200,0,0');
   ObjectSetText("label_range2", range2, 9, "Courier New", C'200,0,0');
           
	return(0);	
}

