//+------------------------------------------------------------------+
//|                                           Daily Range PeterE.mq5 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_plots 0

input int      NumOfDays             = 25;
input string   FontName              = "Tahoma";
input int      FontSize              = 14;
input color    FontColor             = clrWhite;
input int      Window                = 0;
input ENUM_BASE_CORNER      Corner   = 0;
input int      HorizPos              = 85;
input int      VertPos               = 18;

double pnt;
long   dig;
string objname = "*DRPE";

//+------------------------------------------------------------------+
int OnInit()  {
//+------------------------------------------------------------------+
  pnt = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
  dig = SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
  if (dig == 3 || dig == 5) {
    pnt *= 10;
  }  
  ObjectCreate(0,objname,OBJ_LABEL,Window,0,0);
  return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
void OnDeinit(const int reason)  {
  ObjectDelete(0,objname);
  ChartRedraw(0);
 }
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &Time[],
                const double &Open[],
                const double &High[],
                const double &Low[],
                const double &Close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
  int c=0,i;
  double sum=0,hi=0,lo=0;
  datetime dt=0;
  for (i=1; i<Bars(_Symbol,PERIOD_CURRENT)-1; i++)  {
     hi = iHigh(NULL,PERIOD_D1,i);
     lo = iLow(NULL,PERIOD_D1,i);
     dt = iTime(NULL,PERIOD_D1,i);
    if (TimeDayOfWeek(dt) > 0 && TimeDayOfWeek(dt) < 6)  {
      sum += hi - lo;
      c++;
      if (c>=NumOfDays) break;
  } }
  hi = iHigh(NULL,PERIOD_D1,0);
  lo = iLow(NULL,PERIOD_D1,0);
  if (i>0 && pnt>0)  {
    string objtext = "D "+ DoubleToString((hi-lo)/pnt,1) + " | " + "Av "+DoubleToString(sum/c/pnt,1) + " | " +DoubleToString((((hi-lo)/pnt)/(sum/c/pnt))*100,0)+" %";     //DoubleToStr(sum/c/pnt,1) + " | " + DoubleToStr((hi-lo)/pnt,1) + " | " +DoubleToStr((((hi-lo)/pnt)/(sum/c/pnt))*100,0)+" %";   
//     add: + c + :here ^ to display user select adr number of days           
    ObjectSetInteger(0,objname,OBJPROP_CORNER,Corner);
    ObjectSetInteger(0,objname,OBJPROP_XDISTANCE,HorizPos);
    ObjectSetInteger(0,objname,OBJPROP_YDISTANCE,VertPos);
    ObjectSetInteger(0,objname, OBJPROP_SELECTABLE,false);
    ObjectSetText(objname,objtext,FontSize,FontName,FontColor);
  }  
  ChartRedraw(0);
  return(rates_total);
}
//+------------------------------------------------------------------+
int TimeDayOfWeek(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.day_of_week);
  }
//+------------------------------------------------------------------+
bool ObjectSetText(string name,
                       string text,
                       int font_size,
                       string font="",
                       color text_color=CLR_NONE)
  {
   int tmpObjType=(int)ObjectGetInteger(0,name,OBJPROP_TYPE);
   if(tmpObjType!=OBJ_LABEL && tmpObjType!=OBJ_TEXT) return(false);
   if(StringLen(text)>0 && font_size>0)
     {
      if(ObjectSetString(0,name,OBJPROP_TEXT,text)==true
         && ObjectSetInteger(0,name,OBJPROP_FONTSIZE,font_size)==true)
        {
         if((StringLen(font)>0)
            && ObjectSetString(0,name,OBJPROP_FONT,font)==false)
            return(false);
         if(ObjectSetInteger(0,name,OBJPROP_COLOR,text_color)==false)
            return(false);
         return(true);
        }
      return(false);
     }
   return(false);
  }
//+------------------------------------------------------------------+
