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


TOP LEFT is the MAXIMUM RANGE

BOTTOM LEFT is the MINIMUM RANGE

TOP RIGHT is the AVERAGE RANGE

BOTTOM RIGHT is the NUMBER OF BARS CHECKED

BIG NUMBER 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 int   MA_Period       = 200;

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 ; 

 int   Chart_Period    = PERIOD_H1;


//---- buffers


double zRANGE = 0;

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

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
  { 

   return(0); 
  } 

int deinit() 
{ 

   
   ObjectDelete("RNG530");
   ObjectDelete("RNG529");
   ObjectDelete("RNG529B");
   ObjectDelete("RNG531");
   ObjectDelete("RNG532");
   ObjectDelete("RNG534");   
}  

//+------------------------------------------------------------------+ 
//| 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("RNG530", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG530","RANGE", 30, "Arial Bold", Gray);
   ObjectSet("RNG530", OBJPROP_CORNER, 0);
   ObjectSet("RNG530", OBJPROP_XDISTANCE, 760+price.x.offset);
   ObjectSet("RNG530", OBJPROP_YDISTANCE, 160+price.y.offset);
 
   ObjectCreate("RNG529", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG529",DoubleToStr(MaxRange,0), 15, "Arial Bold", Lime);
   ObjectSet("RNG529", OBJPROP_CORNER, 0);
   ObjectSet("RNG529", OBJPROP_XDISTANCE, 770+price.x.offset);
   ObjectSet("RNG529", OBJPROP_YDISTANCE, 197+price.y.offset);
   
   ObjectCreate("RNG529b", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG529b",DoubleToStr( MinRange ,0), 15, "Arial Bold", Crimson);
   ObjectSet("RNG529b", OBJPROP_CORNER, 0);
   ObjectSet("RNG529b", OBJPROP_XDISTANCE, 770+price.x.offset);
   ObjectSet("RNG529b", OBJPROP_YDISTANCE, 260+price.y.offset);                 
                     
   ObjectCreate("RNG531", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG531",DoubleToStr(zRANGE,0)+tPeriod, 40, "Arial Bold", RangeColor );
   ObjectSet("RNG531", OBJPROP_CORNER, 0);
   ObjectSet("RNG531", OBJPROP_XDISTANCE, 770+price.x.offset); // 765
   ObjectSet("RNG531", OBJPROP_YDISTANCE, 210+price.y.offset); 
           
 
   ObjectCreate("RNG532", OBJ_LABEL, win, 0, 0);   // 
   ObjectSetText("RNG532",DoubleToStr( AvgRange ,2), 15, "Arial Bold", Orange); // RANGEBuffer2[0] buf0[0]
   ObjectSet("RNG532", OBJPROP_CORNER, 0);
   ObjectSet("RNG532", OBJPROP_XDISTANCE, 850+price.x.offset);
   ObjectSet("RNG532", OBJPROP_YDISTANCE, 197+price.y.offset);
                 
   ObjectCreate("RNG534", OBJ_LABEL, win, 0, 0);
   ObjectSetText("RNG534",DoubleToStr( MaxBars ,0), 15, "Arial Bold", Silver);
   ObjectSet("RNG534", OBJPROP_CORNER, 0);
   ObjectSet("RNG534", OBJPROP_XDISTANCE, 860+price.x.offset);
   ObjectSet("RNG534", 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);
}
 
//+------------------------------------------------------------------+