//+------------------------------------------------------------------+ 
//|  _TRO_RANGE_EZ_C                                                 | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpldone@gmail.com                                         |  
//+------------------------------------------------------------------+ 
/*


TOP LEFT ( green )is the MAXIMUM RANGE

BOTTOM LEFT ( crimson ) is the MINIMUM RANGE

TOP RIGHT ( orange )is the AVERAGE RANGE

BOTTOM RIGHT ( grey ) is the NUMBER OF BARS CHECKED

BIG NUMBER ( white ) IS THE CURRENT RANGE.

*/
#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpldone.com/" 

//---- indicator settings 
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_minimum 0
#property indicator_maximum 1



//---- indicator parameters  
//extern int   Threshold       = 20; 

extern int win = 0;
extern int price.x.offset= 250;//250
extern int price.y.offset= 0;//-160



extern int   MaxBars         = 960;


extern color CurrentBarColor = LightGray ; 
extern color ThresholdColor  = LimeGreen; 


//---- indicator buffers 


double     MaxRange = 0 ; 
double     MinRange = 0 ; 
double     AvgRange = 0 ; 
double     xRange   = 0 ; 
double     tRange   = 0 ; 



//---- buffers

int   Chart_Period    = 0;
double zRANGE = 0;

bool  show.info       = true;
bool  ShowChart       = true; 

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
  { 
      Chart_Period    = Period();
   return(0); 
  } 

int deinit() 
{ 

   
   ObjectDelete("RNG330");
   ObjectDelete("RNG329");
   ObjectDelete("RNG329B");
   ObjectDelete("RNG331");
   ObjectDelete("RNG332");
   ObjectDelete("RNG334");   
}  

//+------------------------------------------------------------------+ 
//| Range                                                            | 
//+------------------------------------------------------------------+ 
int start() 
  { 

    MaxRange = 0 ; 
    MinRange = 999999 ; 

   double HighVal,LowVal; 
   int limit; 
   int counted_bars=IndicatorCounted(); 
    
//---- last counted bar will be recounted 
   if(counted_bars>0) counted_bars--; 
   if(counted_bars<0) return(-1); 
   

  
   limit = MaxBars ;  

//---- RANGE counted in the 1-st buffer 


		double buf_tmp = 0 ;
		int z = 0 ;



   for(int i=0; i<limit; i++) 
   { 

     HighVal  = iHigh(NULL,Chart_Period,i);        
     LowVal   = iLow(NULL,Chart_Period,i); 

     xRange   = HighVal - LowVal ; // range

     tRange = xRange / Point; // range
     
     buf_tmp =  buf_tmp + xRange ;
 
     
     if( tRange > MaxRange ) {MaxRange = tRange  ; }
     if( tRange < MinRange ) {MinRange = tRange  ; }  
    
    
   } 

   AvgRange = buf_tmp / MaxBars /Point ; // average range 


   
 
   
    zRANGE = (iHigh(NULL,Chart_Period,0)-iLow(NULL,Chart_Period,0))/Point ;
   

//} 
 
//+------------------------------------------------------------------+ 
   
//void DoShowInfo()

//{

string tPeriod = " "+TimeFrameToString( Chart_Period ) ;

if ( zRANGE > AvgRange ) { color RangeColor = ThresholdColor ; } else { RangeColor = CurrentBarColor   ; }

   ObjectCreate("RNG330", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG330","RANGE", 30, "Arial Bold", Gray);
   ObjectSet("RNG330", OBJPROP_CORNER, 0);
   ObjectSet("RNG330", OBJPROP_XDISTANCE, 760+price.x.offset);
   ObjectSet("RNG330", OBJPROP_YDISTANCE, 160+price.y.offset);
 
   ObjectCreate("RNG329", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG329",DoubleToStr(MaxRange,0), 15, "Arial Bold", Lime);
   ObjectSet("RNG329", OBJPROP_CORNER, 0);
   ObjectSet("RNG329", OBJPROP_XDISTANCE, 770+price.x.offset);
   ObjectSet("RNG329", OBJPROP_YDISTANCE, 197+price.y.offset);
   
   ObjectCreate("RNG329b", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG329b",DoubleToStr( MinRange ,0), 15, "Arial Bold", Crimson);
   ObjectSet("RNG329b", OBJPROP_CORNER, 0);
   ObjectSet("RNG329b", OBJPROP_XDISTANCE, 770+price.x.offset);
   ObjectSet("RNG329b", OBJPROP_YDISTANCE, 260+price.y.offset);                 
                     
   ObjectCreate("RNG331", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG331",DoubleToStr(zRANGE,0)+tPeriod, 40, "Arial Bold", RangeColor );
   ObjectSet("RNG331", OBJPROP_CORNER, 0);
   ObjectSet("RNG331", OBJPROP_XDISTANCE, 770+price.x.offset); // 765
   ObjectSet("RNG331", OBJPROP_YDISTANCE, 210+price.y.offset); 
           
 
   ObjectCreate("RNG332", OBJ_LABEL, win, 0, 0);   // 
   ObjectSetText("RNG332",DoubleToStr( AvgRange ,2), 15, "Arial Bold", Orange); // RANGEBuffer2[0] buf0[0]
   ObjectSet("RNG332", OBJPROP_CORNER, 0);
   ObjectSet("RNG332", OBJPROP_XDISTANCE, 850+price.x.offset);
   ObjectSet("RNG332", OBJPROP_YDISTANCE, 197+price.y.offset);
                 
   ObjectCreate("RNG334", OBJ_LABEL, win, 0, 0);
   ObjectSetText("RNG334",DoubleToStr( MaxBars ,0), 15, "Arial Bold", Silver);
   ObjectSet("RNG334", OBJPROP_CORNER, 0);
   ObjectSet("RNG334", OBJPROP_XDISTANCE, 860+price.x.offset);
   ObjectSet("RNG334", OBJPROP_YDISTANCE, 260+price.y.offset);    
    

   WindowRedraw();    
 
//} 

   return(0); 
} 

//+------------------------------------------------------------------+

 
string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
   }
   return(tfs);
}
 
//+------------------------------------------------------------------+