//+------------------------------------------------------------------+
//|                                         Daily Range Extended.mq4 |
//|                                             bfis108137 & dynel14 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "bfis108137 & dynel14"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Maroon
#property indicator_color2 Red
#property indicator_color3 BurlyWood
#property indicator_color4 Lime
#property indicator_color5 DarkGreen
#property indicator_color6 DimGray
#property indicator_color7 DimGray
#property indicator_width1 5
#property indicator_width2 3
#property indicator_width3 3
#property indicator_width4 3
#property indicator_width5 5
#property indicator_width6 5
#property indicator_width7 5
#property indicator_style2 STYLE_DASH
#property indicator_style4 STYLE_DASH

//--- input parameters
extern int    daysBack = 5;
extern int    adrPeriod = 5;
extern string FontName = "Arial Black";
extern int    FontSize = 8;
extern color  FontColor = Yellow;
extern color  TradeableColor = Lime;
extern color  UntradeableColor = Red;
extern color  BackgroundColor = CLR_NONE;
extern int    Window = 0;
extern int    Corner = 2;
extern int    HorizPosBack = 0;
extern int    VertPosBack =50;
extern int    HorizPosAdr = 5;
extern int    HorizPosToday = 135;
extern int    VertPos = 0;

//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];




//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
double pnt;
int    dig;
string objName = "Daily Range Extended";
string objNameToday = "Daily Range Extended Today";
string objNameBackground = "Background";


//+------------------------------------------------------------------+
int init()
{
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexStyle(5,DRAW_SECTION);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexStyle(6,DRAW_SECTION);
   SetIndexBuffer(6,ExtMapBuffer7);
   
   pnt = MarketInfo(Symbol(),MODE_POINT);
   dig = MarketInfo(Symbol(),MODE_DIGITS);
   if (dig == 3 || dig == 5) {pnt *= 10;}
   if (dig == 6)             {pnt *= 100;}
   if (dig == 7)             {pnt *= 1000;}
   
   RefreshRates();
   if (Digits == 3 && StringFind(DoubleToStr((Ask-Bid),Digits),DoubleToStr(MarketInfo(Symbol(),MODE_SPREAD),0),0)==2)
   {
     pnt *= 100; // for pairs with 2 decimal places but 3 digits, e.g. GOLD
   }
   
   ObjectCreate(objName,OBJ_LABEL,Window,0,0);
   ObjectCreate(objNameToday,OBJ_LABEL,Window,0,0);
   ObjectCreate(objNameBackground,OBJ_LABEL,Window,0,0);
//----
   return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
   ObjectDelete(objName);
   ObjectDelete(objNameToday);
   ObjectDelete(objNameBackground);
   
   return(0);
//----
}
  

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int limit;
   int counted_bars=IndicatorCounted();
  
   //---- check for possible errors
   if(counted_bars<0) return(-1);
  
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
  
   //---- main loop drawing lines      
   for(int i=0; i<limit; i++)
   {
     int index = iBarShift(Symbol(),PERIOD_D1,Time[i]);
     if(index>daysBack)
     break;
     double adr = GetAdr(adrPeriod,index);
     double open = iOpen(Symbol(),PERIOD_D1,index);
         
     ExtMapBuffer1[i]=open+adr;
     ExtMapBuffer2[i]=open+adr*0.5;      
     ExtMapBuffer3[i]=open;
     ExtMapBuffer4[i]=open-adr*0.5;
     ExtMapBuffer5[i]=open-adr;
     ExtMapBuffer6[i]=(open+adr)-(open+adr*0.5);
     ExtMapBuffer7[i]=(open-adr)-(open-adr*0.5);
   }
      
   //writing adr/atr data to chart
   color Color;
   double rangeToday = iHigh(Symbol(),PERIOD_D1,0)-iLow(Symbol(),PERIOD_D1,0);
   adr = GetAdr(adrPeriod,0);
   /////////////////////////////////////
   adr = adr / pnt;

   if (i>0 && pnt>0)  
   {
     string objtext = "ADR = " + DoubleToStr(adr,1) + "  (" + adrPeriod + " days)";
     string objtextToday = "Today = " + DoubleToStr((rangeToday)/pnt,1);
     if (adr<((rangeToday)/pnt)) 
     {
       Color = UntradeableColor; 
     }
     else Color = TradeableColor;
     
     ObjectSet(objNameBackground, OBJPROP_CORNER, Corner);
     ObjectSet(objNameBackground, OBJPROP_XDISTANCE, HorizPosBack);
     ObjectSet(objNameBackground, OBJPROP_YDISTANCE, VertPosBack);
     ObjectSet(objNameBackground, OBJPROP_COLOR, BackgroundColor);
     ObjectSet(objNameBackground, OBJPROP_BACK, false);
     ObjectSetText(objNameBackground, "ggggggggggggggggg", 17, "Webdings");
          
     ObjectSet(objName,OBJPROP_CORNER,Corner);
     ObjectSet(objName,OBJPROP_XDISTANCE,HorizPosAdr);
     ObjectSet(objName,OBJPROP_YDISTANCE,VertPos);
     ObjectSetText(objName,objtext,FontSize,FontName,FontColor);
      
     ObjectSet(objNameToday,OBJPROP_CORNER,Corner);
     ObjectSet(objNameToday,OBJPROP_XDISTANCE,HorizPosToday);
     ObjectSet(objNameToday,OBJPROP_YDISTANCE,VertPos);
     ObjectSetText(objNameToday,objtextToday,FontSize,FontName,Color);
   }
   return(0);
}


//+------------------------------------------------------------------+
double GetAdr(int days,int start)
{
  int c=0;
  double sum=0;
  for (int i=1; i<Bars-1; i++)  
  {
    double hi = iHigh(NULL,PERIOD_D1,start + i);
    double lo = iLow(NULL,PERIOD_D1,start + i);
    datetime dt = iTime(NULL,PERIOD_D1,start + i);
    if (TimeDayOfWeek(dt) > 0 && TimeDayOfWeek(dt) < 6)  
    {
      sum += hi - lo;
      c++;
      if (c>=days) break;
    } 
  }
      
  double range = sum / c;
  return(range);      
}
//+------------------------------------------------------------------+