//+-------------------------------------------------------------------+ 
//|                                        RANGE_X2.mq4                  | 
//|                                                                   | 
//|                                                                   | 
//+-------------------------------------------------------------------+ 
#property  copyright "Copyright © 2006, MetaQuotes Software Corp."
#property  link      "OTCFX" 


//---- indicator settings 
#property  indicator_separate_window 
#property  indicator_buffers 2 
#property  indicator_color1  Red 
#property  indicator_color2  Green 
#property  indicator_minimum 0
#property indicator_width1 2
#property indicator_width2 2
//---- indicator buffers 
double     RANGEBuffer[]; 
double     RANGEBuffer1[]; 

//---- indicator parameters  
extern color CurrentBarColor = Gold; 
extern color ThresholdColor  = Aqua; 
extern int   MaxBars         = 250;

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

double zRANGE = 0;
//extern int win = 3;
extern int price.x.offset= 0;
extern int price.y.offset= 0;
//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
  { 
IndicatorShortName("RANGE");
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID); 
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID); 
   SetIndexBuffer(0,RANGEBuffer); 
   SetIndexBuffer(1,RANGEBuffer1);  
   SetIndexLabel(0,"RANGE"); 
   SetIndexLabel(1,"RANGE"); 

//---- initialization done 
   return(0); 
  } 
int deinit() 
{ 
   ObjectDelete("CurrentBarValue"); 
   ObjectDelete("LabelCurrentBarValue"); 
   ObjectDelete("RNG530");
   ObjectDelete("RNG529");
   ObjectDelete("RNG529B");
   ObjectDelete("RNG531");
   ObjectDelete("RNG532");
   ObjectDelete("RNG533");
   ObjectDelete("RNG534");  
   ObjectDelete("RNG535");
}  
//+------------------------------------------------------------------+ 
//| Range                                                            | 
//+------------------------------------------------------------------+ 
int start() 
  { 
   double HighVal,LowVal,HighVal1,LowVal1; 
   int rng_1=0;
   int limit; 
   int counted_bars=IndicatorCounted(); 
//---- last counted bar will be recounted 
  if(counted_bars>0) counted_bars--; 
  limit=Bars-counted_bars; 
//---- RANGE counted in the 1-st buffer 
   for(int i=0; i<limit; i++) 
   {
   
    double R2;
    int h=0;
      
    R2       =  (iHigh(NULL,0,1)-iLow(NULL,0,1))/Point;
    HighVal  = iHigh(NULL,0,i);        
    LowVal   = iLow(NULL,0,i); 
    if ((HighVal-LowVal)/Point >= R2) { RANGEBuffer1[i] = (HighVal-LowVal)/Point; } 
    else { RANGEBuffer[i] = (HighVal-LowVal)/Point; } 

     
   MaxRange = 0 ; 
   MinRange = 999999 ; 

  
   if(counted_bars>0) counted_bars--; 
   if(counted_bars<0) return(-1); 
     
    limit = MaxBars ;  


   double buf_tmp = 0 ;
	int z = 0 ;
   for(int l=0; l<limit; l++) 
   { 

     HighVal  = iHigh(NULL,0,l);        
     LowVal   = iLow(NULL,0,l);
    
    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 ;  
   
   zRANGE = (iHigh(NULL,0,0)-iLow(NULL,0,0))/Point;

   if ( zRANGE > AvgRange ) { color RangeColor = ThresholdColor ; } else { RangeColor = CurrentBarColor   ; }        
   color  ColorCOF;     
   if( zRANGE  > R2   ) { ColorCOF = Green ; } else { ColorCOF = Crimson ; }
   
           
   ObjectDelete("CurrentBarValue"); 
   ObjectCreate("CurrentBarValue", OBJ_HLINE, WindowFind("RANGE"), Time[0], (iHigh(NULL,0,0)-iLow(NULL,0,0))/Point); 
   ObjectSet("CurrentBarValue", OBJPROP_COLOR, ColorCOF); 
   ObjectSet("CurrentBarValue", OBJPROP_STYLE, STYLE_SOLID); 
   ObjectSet("CurrentBarValue", OBJPROP_WIDTH, 1); 
   ObjectSet("CurrentBarValue", OBJPROP_BACK, true); 
      
   
   ObjectSet("LabelCurrentBarValue", OBJPROP_COLOR, CurrentBarColor); 
   ObjectSetText("LabelCurrentBarValue", "Current Bar value",8); 
   ObjectSet("LabelCurrentBarValue", OBJPROP_ANGLE, 0); 
   
              
   ObjectCreate("RNG531", OBJ_LABEL, WindowFind("RANGE"), 0, 0);
   ObjectSetText("RNG531","Curr-RX  "+  DoubleToStr(zRANGE,0), 12, "Courier Bold", ColorCOF );
   ObjectSet("RNG531", OBJPROP_CORNER, 0);
   ObjectSet("RNG531", OBJPROP_XDISTANCE, 300+price.x.offset); 
   ObjectSet("RNG531", OBJPROP_YDISTANCE, 0+price.y.offset);
      
   
   ObjectCreate("RNG532", OBJ_LABEL, WindowFind("RANGE"), 0, 0);   
   ObjectSetText("RNG532","Prv-RX  "+  DoubleToStr(R2,0), 12, "Courier Bold", Orange); 
   ObjectSet("RNG532", OBJPROP_CORNER, 0);
   ObjectSet("RNG532", OBJPROP_XDISTANCE, 150+price.x.offset);
   ObjectSet("RNG532", OBJPROP_YDISTANCE, 0+price.y.offset);    
          
   }    
    
//---- done 
   return(0); 
  } 