//+------------------------------------------------------------------+
//|                                                Range_DAYS v1.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                          Day Range Highlight.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
//---- input parameters
extern int       DaysToPlot     = 18;   
extern color     RangeColor1    = clrRed;
extern color     RangeColor2    = clrGreen;
extern color     RangeColor3    = clrGold;
extern color     RangeColor4    = clrMagenta;
extern color     RangeColor5    = clrAqua;
extern color     RangeColor6    = clrSilver;
extern bool      ShowRangeText  = true;
extern color     RangeTextColor = clrKhaki;
extern color     HiOpTextColor  = clrLime;
extern color     OpLoTextColor  = clrDarkOrange;
extern int       Fontsize       = 8;
extern int       ShiftText      = 5;  
extern bool      ShowAveDailyRange = true; 

double DayOpen;
double DayHigh;
double DayLow;
string DayRange;
double Day_Price[][6];
double DailyRange;
double hiop,loop;

double Poin;

 
double myPoint;

double SetPoint() 
{ 
   double mPoint; 
   
   if (Digits < 4) 
   mPoint = 0.01; 
   else 
   mPoint = 0.0001; 
   
  return(mPoint); 
}  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
 myPoint = SetPoint();
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   Comment("");
   for(int i=0; i<DaysToPlot; i++)
   {
      ObjectDelete("RangeDay" + i);
      ObjectDelete("RangeDaytext" + i);
      ObjectDelete("RangeDaytexthi" + i);
      ObjectDelete("RangeDaytextlo" + i);
   }   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
 
   deinit();
   DailyRange = 0;
   ArrayInitialize(Day_Price,0);
   ArrayCopyRates(Day_Price,(Symbol()), 1440);
   for(int i=0; i<DaysToPlot; i++)
   {
   
 
      if(i==0)
      { 
         ObjectCreate("RangeDay" + i,OBJ_RECTANGLE,0,Day_Price[i][0],Day_Price[i][2]);
         ObjectSet("RangeDay" + i,2,Time[i]);
         ObjectSet("RangeDay" + i,3,Day_Price[i][3]);
         ObjectSet("RangeDay" + i,OBJPROP_BACK,false);
         DayHigh = Day_Price[i][3];
         DayLow = Day_Price[i][2];
         DayOpen = Day_Price[i][1];
         DayRange = DoubleToStr((DayHigh - DayLow)/myPoint,0);
         hiop = DoubleToStr((DayHigh - DayOpen)/myPoint,0);
         loop = DoubleToStr((DayOpen - DayLow)/myPoint,0);
         if(ShowRangeText)
         {
            Text("RangeDaytext" + i,0, "                  "+DoubleToStr(DayRange,1), Fontsize,"Arial" , RangeTextColor,Day_Price[i][0],Day_Price[i][2]-ShiftText*myPoint,true);
            Text("RangeDaytexthi" + i,0, "                  "+DoubleToStr(hiop,1), Fontsize,"Arial" , HiOpTextColor,Day_Price[i][0],Day_Price[i][3]+(ShiftText-1)*myPoint,true);
            Text("RangeDaytextlo" + i,0, "                  "+DoubleToStr(loop,1), Fontsize,"Arial" , OpLoTextColor,Day_Price[i][0],Day_Price[i][2]-(ShiftText-4)*myPoint,true);
   
           // ObjectSetText("RangeDay" + i,DayRange + "" );
         }   
      }
      else
      {
         ObjectCreate("RangeDay" + i,OBJ_RECTANGLE,0,Day_Price[i][0],Day_Price[i][2]);
         ObjectSet("RangeDay" + i,2,Day_Price[i-1][0]);
         ObjectSet("RangeDay" + i,3,Day_Price[i][3]);
         ObjectSet("RangeDay" + i,OBJPROP_BACK,false);
         DayHigh = Day_Price[i][3];
         DayLow = Day_Price[i][2];
         DayOpen = Day_Price[i][1];
         DailyRange = DailyRange + ((DayHigh - DayLow)/myPoint);
         DayRange = DoubleToStr((DayHigh - DayLow)/myPoint,0);
         hiop = DoubleToStr((DayHigh - DayOpen)/myPoint,0);
         loop = DoubleToStr((DayOpen - DayLow)/myPoint,0);
         if(ShowRangeText)
         {
           Text("RangeDaytext" + i,0, "                  "+DoubleToStr(DayRange,1), Fontsize,"Arial" , RangeTextColor,Day_Price[i][0],Day_Price[i][2]-ShiftText*myPoint,true);
           Text("RangeDaytexthi" + i,0, "                  "+DoubleToStr(hiop,1), Fontsize,"Arial" , HiOpTextColor,Day_Price[i][0],Day_Price[i][3]+(ShiftText-1)*myPoint,true);
           Text("RangeDaytextlo" + i,0, "                  "+DoubleToStr(loop,1), Fontsize,"Arial" , OpLoTextColor,Day_Price[i][0],Day_Price[i][2]-(ShiftText-4)*myPoint,true);
   
           // ObjectSetText("RangeDay" + i,DayRange + "");
         }   
      }
      
     if(MathMod(i,6) == 0)
      {
         ObjectSet("RangeDay" + i,6,RangeColor1);
      }
      else if(MathMod(i,6) == 5)
      {
         ObjectSet("RangeDay" + i,6,RangeColor6);     
      }       
      else if(MathMod(i,6) == 4)
      {
         ObjectSet("RangeDay" + i,6,RangeColor5);     
      } 
       else if(MathMod(i,6) == 3)
      {
         ObjectSet("RangeDay" + i,6,RangeColor4);     
      }  
       else if(MathMod(i,6) == 2)
      {
         ObjectSet("RangeDay" + i,6,RangeColor3);     
      }  
       else if(MathMod(i,6) == 1)
      {
         ObjectSet("RangeDay" + i,6,RangeColor2);     
      }      
   }
   if (ShowAveDailyRange)
   {
      Comment("Ave Daily Range: " + DoubleToStr(DailyRange/(DaysToPlot-1),0));   
   }
   WindowRedraw(); 
   return(0);
  }
  
  
void Text(string TextName, int Window, string LabelText, int FontSize, string FontName, color Color, datetime Time1, double Price1, bool Delete)
  {
   if (Delete) ObjectDelete(TextName);
   if(ObjectFind(TextName)!=0)
     {
    ObjectCreate(TextName, OBJ_TEXT, Window, Time1,Price1);
    ObjectSetText(TextName, LabelText, FontSize, FontName, Color);
    ObjectSet(TextName,OBJPROP_BACK,false);
     }
   else
   ObjectMove(TextName, 0, Time1, Price1);
  } 
//+------------------------------------------------------------------+

