//+------------------------------------------------------------------+
//|   AverageBarRangeRH                                              |
//|   FF rhtrader - March 2015                                       |
//+------------------------------------------------------------------+


#property indicator_chart_window
#property indicator_buffers 2

extern int     ABRPeriods =               480;
extern int     SubWindow =                0 ;
extern int     WindowCorner =             0 ;
extern int     ABRLabelXPos =             13 ;
extern int     ABRLabelYPos =             55;
extern bool    DispLastBarInfo =          true ;
extern string  LastBarInfoLabel =         "Last: " ;          
extern bool    DispCurrentBarInfo =       true ;
extern string  CurrBarInfoLabel =         "Curr: " ; 
extern int     BarInfoXPos =              13 ;
extern int     BarInfoYPos =              78 ;
extern double  BarSizeExceptionMult =     2.0 ;
extern color   BarSizeGoodRangeColor =    Lime;
extern color   BarSizeBadRangeColor =     Red;

extern int     Fontsize =                 12;
extern string  Fontname =                 "Arial Black";
extern color   Fontcolor =                 DimGray ;

double   buffer[];
double   Range_Buffer[];
string   Pair;
double   Pip;
int      NrOfDigits;

int init()
{
   Pair = Symbol() ;
   NrOfDigits = MarketInfo(Pair, MODE_DIGITS);          // Nr. of decimals used by Symbol
   Pip = MarketInfo(Pair, MODE_POINT);                  // Pips multiplier for value adjustment
   if(NrOfDigits == 5 || NrOfDigits == 3)
      Pip = MarketInfo(Pair, MODE_POINT) * 10;          // Multiply pips by 10   

   SetIndexBuffer(0,buffer);
   SetIndexLabel(0,"ABR (" + ABRPeriods + ")");
   SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(1,Range_Buffer);
   SetIndexLabel(1,"ABR Bar Pips");
   SetIndexStyle(1,DRAW_NONE);   
   IndicatorShortName("ABR (" + ABRPeriods + ")");
   
   ObjectCreate("label_ABR_Pips", OBJ_LABEL, SubWindow, 0, 0);
   ObjectSet("label_ABR_Pips", OBJPROP_CORNER, WindowCorner);    
   ObjectSet("label_ABR_Pips", OBJPROP_XDISTANCE, ABRLabelXPos);
   ObjectSet("label_ABR_Pips", OBJPROP_YDISTANCE, ABRLabelYPos);
   ObjectSetInteger(0,"label_ABR_Pips",OBJPROP_SELECTABLE,false);
   
   if(DispLastBarInfo || DispCurrentBarInfo)
   {
      ObjectCreate("label_BarSize_Pips",OBJ_LABEL, SubWindow, 0, 0);
      ObjectSet("label_BarSize_Pips", OBJPROP_CORNER, WindowCorner); 
      ObjectSet("label_BarSize_Pips", OBJPROP_XDISTANCE, BarInfoXPos);
      ObjectSet("label_BarSize_Pips", OBJPROP_YDISTANCE, BarInfoYPos);
      ObjectSetInteger(0,"label_BarSize_Pips",OBJPROP_SELECTABLE,false);   
   }
   return(0);
 }

int deinit()
{
   if(ObjectFind("label_ABR_Pips")!=-1) ObjectDelete("label_ABR_Pips");
   if(ObjectFind("label_BarSize_Pips")!=-1) ObjectDelete("label_BarSize_Pips");
   
   return(0);
}

int start()
  {
   int limit;  
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   for (int i = 0; i < limit; i++)
   {
      buffer[i] = MathCeil(iATR(NULL,0,ABRPeriods,i)/Pip) ;
      Range_Buffer[i-1]=(High[i]-Low[i])/Pip;      
   }
    
   string text = "ABR ("+ ABRPeriods+ "): "+ DoubleToStr(buffer[0],0);
   ObjectSetText("label_ABR_Pips",text, Fontsize,Fontname, Fontcolor);
   
   text = "" ;
   double LastRange = 0 ;
   double CurrRange = 0 ;
   
   if(DispLastBarInfo)
   {
      LastRange = Range_Buffer[0] ;
      text = LastBarInfoLabel + DoubleToStr(LastRange,0) ;
      text = text + " " ;
   }
   if(DispCurrentBarInfo)
   {
      CurrRange = (High[0]-Low[0])/Pip ;
      text = text + CurrBarInfoLabel + DoubleToStr(CurrRange,0) ;
   }
   
   if(DispLastBarInfo || DispCurrentBarInfo)
   {
      if((LastRange > buffer[0] * BarSizeExceptionMult && DispLastBarInfo) || (CurrRange  > buffer[0] * BarSizeExceptionMult && DispCurrentBarInfo))
         color RangeColor = BarSizeBadRangeColor ;
      else
         RangeColor = BarSizeGoodRangeColor ;  
      ObjectSetText("label_BarSize_Pips",  text,Fontsize, Fontname, RangeColor);   
   }   
   return(0);
  }
