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

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

//---- indicator settings 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 LimeGreen
 

//---- indicator parameters  

extern int   MaxBars         = 24;

//---- indicator buffers 


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



//---- buffers


double zRange = 0;


double   AboveAvg, BelowAvg, AboveCur, BelowCur ;


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

      SetIndexBuffer(0,AvgRange); 
      SetIndexLabel(0,"AvgRange");
   SetIndexStyle(0,DRAW_HISTOGRAM);
         
   return(0); 
  } 

int deinit() 
{ 

   return(0); 
}  

//+------------------------------------------------------------------+ 
//| 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); 
   

   AboveAvg = 0 ; 
   BelowAvg = 0 ; 
   AboveCur = 0 ; 
   BelowCur  = 0 ;
 
int j, jlimit ;

jlimit = MaxBars + 1; 
 
   for(j=limit; j>=0; j--)
   { 
  
   limit = MaxBars + 1;  

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


		double buf_tmp = 0 ;
		int z = 0 ;


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

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

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

     xRange   = HighVal - LowVal ; // range

     tRange = xRange / Point; // range
     
     buf_tmp =  buf_tmp + xRange ;
 
     
   } // for i

   AvgRange[j] = buf_tmp / MaxBars /Point ; // average range 
   
} // for j

   return(0); 
   
}
  
  
//+------------------------------------------------------------------+

 