//---------------------------------------------------- |
//                                          5WeekHL.mq4|
//                                            BlueRain |
//                                           15/11/2020|
//-----------------------------------------------------|
#property copyright "BlueRain"
#property link   "https://www.forexfactory.com/bluerain"
#property description "Draw W1 ~ W5"

#property indicator_chart_window

extern color  High1_LineColor = clrLime;
extern color  Low1_LineColor = clrRed;
extern color  High2_LineColor = clrDodgerBlue;
extern color  Low2_LineColor = clrFireBrick;
extern color  High3_LineColor = clrCyan;
extern color  Low3_LineColor = clrOrange;
extern color  High4_LineColor = clrGreen;
extern color  Low4_LineColor = clrMaroon;
extern color  High5_LineColor = clrGreenYellow;
extern color  Low5_LineColor = clrCoral;
   
extern int   LineWidth = 2; 
extern bool ShowW1 = true;
extern bool ShowW2 = true;
extern bool ShowW3 = true;
extern bool ShowW4 = true;
extern bool ShowW5 = true;
extern bool ShowPriceTag = false;

string LabelID = "5WHL";
double Poin;
double timeX = 0;
double range ; 
double targetline;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
    if (Point==0.00001) Poin=0.0001;
   else {
      if (Point==0.001) Poin=0.01;
      else Poin=Point;
   }   
  //Print("I is ", i);  
  if(Period()==1   )timeX=60;if(Period()==5    )timeX=300;if(Period()==15   )timeX=900;
  if(Period()==30  )timeX=1800;if(Period()==60   )timeX=3600;if(Period()==240  )timeX=14400;
  if(Period()==1440)timeX=86400;if(Period()==10080)timeX=604800;if(Period()==43200)timeX=2592000;
  
	return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   deleteObjects();
    
	return(0);
}

void deleteObjects()
{
   string lookFor       = LabelID;
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
    if(id==CHARTEVENT_CHART_CHANGE)
    ScreenDrawing();
//---
   
  }

//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   RefreshRates();
   ScreenDrawing();
   return (0);
}

void ScreenDrawing()
{

   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied = CopyRates(Symbol(),PERIOD_W1,0,5,rates);

   if ( copied > 0)
   {
      DrawVLine(LabelID + "d1openvline", rates[4].time,clrYellow,1,STYLE_DOT);
   
      int x,y;
      if(ShowW1){
      DrawLine(LabelID + "high1", rates[0].time,Time[0] + timeX*5, rates[0].high, rates[0].high,  High1_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[0].high, x, y);
      x = x -50;
      y = y + 0 ;
      if(ShowPriceTag){
      DrawLabel(LabelID + "high1text",x,y,"High Week 1 @ "+DoubleToStr(rates[0].high,Digits),8,"Arial",High1_LineColor,DoubleToStr(rates[0].high,Digits));}
       
      DrawLine(LabelID + "low1", rates[0].time,Time[0] + timeX*5,rates[0].low,rates[0].low, Low1_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[0].low, x, y);
      x = x -50;
      y = y + 0 ;
      if(ShowPriceTag){
      DrawLabel(LabelID + "low1text",x,y,"Low Week 1 @ "+DoubleToStr(rates[0].low,Digits),8,"Arial",Low1_LineColor, DoubleToStr(rates[0].low,Digits));}
      }
      //======
      if(ShowW2){
      DrawLine(LabelID + "high2", rates[1].time,Time[0] + timeX*5, rates[1].high, rates[1].high,  High2_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[1].high, x, y);
      x = x -50;
      y = y + 0;
      if(ShowPriceTag){
      DrawLabel(LabelID + "high2text",x,y,"High Week 2 @ "+DoubleToStr(rates[1].high,Digits),8,"Arial",High2_LineColor,DoubleToStr(rates[1].high,Digits));}
       
      DrawLine(LabelID + "low2", rates[1].time,Time[0] + timeX*5,rates[1].low,rates[1].low, Low2_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[1].low, x, y);
      x = x -50;
      y = y + 0;
      if(ShowPriceTag){
      DrawLabel(LabelID + "low2text",x,y,"Low Week 2 @ "+DoubleToStr(rates[1].low,Digits),8,"Arial",Low2_LineColor, DoubleToStr(rates[1].low,Digits));}
       }
       //======
       if(ShowW3){
      DrawLine(LabelID + "high3", rates[2].time,Time[0] + timeX*5, rates[2].high, rates[2].high,  High3_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[2].high, x, y);
      x = x -150;
      y = y + 0;
      if(ShowPriceTag){
      DrawLabel(LabelID + "high3text",x,y,"High Week 3 @ "+DoubleToStr(rates[2].high,Digits),8,"Arial",High3_LineColor, DoubleToStr(rates[2].high,Digits));}
 
       
      DrawLine(LabelID + "low3", rates[2].time,Time[0] + timeX*5,rates[2].low,rates[2].low, Low3_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[2].low, x, y);
      x = x -150;
      y = y + 0;
      if(ShowPriceTag){
      DrawLabel(LabelID + "low3text",x,y,"Low Week 3 @ "+DoubleToStr(rates[2].low,Digits),8,"Arial",Low3_LineColor, DoubleToStr(rates[2].low,Digits));}
     }
        //======
        if(ShowW4){
      DrawLine(LabelID + "high4", rates[3].time,Time[0] + timeX*5, rates[3].high, rates[3].high,  High4_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[3].high, x, y);
      x = x -200;
      y = y + 0;
      if(ShowPriceTag){
      DrawLabel(LabelID + "high4text",x,y,"High Week 4 @ "+DoubleToStr(rates[3].high,Digits),8,"Arial",High4_LineColor, DoubleToStr(rates[3].high,Digits));}
 
       
      DrawLine(LabelID + "low4", rates[3].time,Time[0] + timeX*5,rates[3].low,rates[3].low, Low4_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[3].low, x, y);
      x = x -200;
      y = y + 0;
      if(ShowPriceTag){
      DrawLabel(LabelID + "low4text",x,y,"Low Week 4 @ "+DoubleToStr(rates[3].low,Digits),8,"Arial",Low4_LineColor, DoubleToStr(rates[3].low,Digits));}
     }
      //======
      if(ShowW5){
      DrawLine(LabelID + "high5", rates[4].time,Time[0] + timeX*5, rates[4].high, rates[4].high,  High5_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[4].high, x, y);
      x = x -250;
      y = y + 0;
      if(ShowPriceTag){
      DrawLabel(LabelID + "high5text",x,y,"High Week 5 @ "+DoubleToStr(rates[4].high,Digits),8,"Arial",High5_LineColor, DoubleToStr(rates[4].high,Digits));}
 
       
      DrawLine(LabelID + "low5", rates[4].time,Time[0] + timeX*5,rates[4].low,rates[4].low, Low5_LineColor,LineWidth,STYLE_SOLID);
      ChartTimePriceToXY(0, 0,Time[0] + timeX*5, rates[4].low, x, y);
      x = x -250;
      y = y + 0;
      if(ShowPriceTag){
      DrawLabel(LabelID + "low5text",x,y,"Low Week 5 @ "+DoubleToStr(rates[4].low,Digits),8,"Arial",Low5_LineColor, DoubleToStr(rates[4].low,Digits));}
    }
  }     
   WindowRedraw();
}

void DrawPriceValues(string name, double price,color textcolor, int fontsize  )
{
   
  if (ObjectFind(name) ) ObjectDelete(name);
  
  ObjectCreate(name, OBJ_TEXT, 0, Time[0],price);
  ObjectSet(name, OBJPROP_ANGLE, 0);
  
  ObjectSetText(name,DoubleToStr(price,2) ,fontsize, "Corbel", textcolor);
}
//+------------------------------------------------------------------+
//| Draws a pips distance for SL or TP.                              |
//+------------------------------------------------------------------+
void DrawPrice(string name, datetime time1, double price1, color col, int fontSize, string text)
{  
   int x, y;

	// Needed only for y, x is derived from the chart width.
   ChartTimePriceToXY(0, 0, time1, price1, x, y);
   DrawLabel(name,x,y-10,text,fontSize,"Arial",col,DoubleToStr(price1,Digits));
   
}


void DrawPipDiff(string name, datetime time1, double price1, color col, int fontSize, string text, int Yposition)
{
   int x, y;
  
	// Needed only for y, x is derived from the chart width.
   ChartTimePriceToXY(0, 0, time1, price1, x, y);
   DrawLabel(name,x,y-Yposition,text,fontSize,"Arial",col,DoubleToStr(price1,Digits));
     
}




void DrawLabel(string name,int x,int y,string label,int size=9,string font="Arial",color clr=DimGray,string tooltip="")
  { 
   if (ObjectFind(name)) ObjectDelete(name);
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,label,size,font,clr);
   ObjectSet(name,OBJPROP_CORNER,0);
   ObjectSet(name,OBJPROP_XDISTANCE,x);
   ObjectSet(name,OBJPROP_YDISTANCE,y);
   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);
//--- justify text
//ObjectSet(name, OBJPROP_ANCHOR, 0);
//ObjectSetString(0, name, OBJPROP_TOOLTIP, tooltip);
//ObjectSet(name, OBJPROP_SELECTABLE, 0);
//---
  }
  
//+------------------------------------------------------------------+
//|  Custom indicator initialization function                        |
//+------------------------------------------------------------------+
void DrawVLine(string name, datetime Time1, color lcolor, int lwidth,ENUM_LINE_STYLE linestyle)
{
   
  
   ObjectCreate   (name,            OBJ_VLINE, 0, Time1,0);//, dValue);
   ObjectSet      (name,            OBJPROP_STYLE, linestyle);
   ObjectSet      (name,            OBJPROP_COLOR, lcolor); 
}


void DrawLine(string name, datetime Time1, datetime Time2, double Price1, double Price2,color lcolor, int lwidth,ENUM_LINE_STYLE linestyle)
{ 
          ObjectDelete(name);
          ObjectCreate(name,OBJ_TREND,0,Time1,0,Time2,0);                      
          ObjectSet(name,OBJPROP_COLOR,lcolor);
          if (linestyle == STYLE_SOLID)
          ObjectSet(name,OBJPROP_WIDTH,lwidth);          
          ObjectSet(name,OBJPROP_STYLE,linestyle);         
          ObjectSet(name,OBJPROP_RAY,false);              
          ObjectSet(name,OBJPROP_PRICE1,Price1);
          ObjectSet(name,OBJPROP_PRICE2,Price2);                  
}  

//+------------------------------------------------------------------+
void DrawObjects(datetime dt, string no, string tb, string te) {
  datetime t1, t2;
  double   p1, p2;
  int      b1, b2;

  t1=StrToTime(TimeToStr(dt, TIME_DATE)+" "+tb);
  t2=StrToTime(TimeToStr(dt, TIME_DATE)+" "+te);
  b1=iBarShift(NULL, 0, t1);
  b2=iBarShift(NULL, 0, t2);
  p1=High[Highest(NULL, 0, MODE_HIGH, b1-b2, b2)];
  p2=Low [Lowest (NULL, 0, MODE_LOW , b1-b2, b2)];
  ObjectSet(no, OBJPROP_TIME1 , t1);
  ObjectSet(no, OBJPROP_PRICE1, p1);
  ObjectSet(no, OBJPROP_TIME2 , t2);
  ObjectSet(no, OBJPROP_PRICE2, p2);
}

//+------------------------------------------------------------------+
datetime decDateTradeDay (datetime dt) {
  int ty=TimeYear(dt);
  int tm=TimeMonth(dt);
  int td=TimeDay(dt);
  int th=TimeHour(dt);
  int ti=TimeMinute(dt);

  td--;
  if (td==0) {
    tm--;
    if (tm==0) {
      ty--;
      tm=12;
    }
    if (tm==1 || tm==3 || tm==5 || tm==7 || tm==8 || tm==10 || tm==12) td=31;
    if (tm==2) if (MathMod(ty, 4)==0) td=29; else td=28;
    if (tm==4 || tm==6 || tm==9 || tm==11) td=30;
  }
  return(StrToTime(ty+"."+tm+"."+td+" "+th+":"+ti));
}
//+------------------------------------------------------------------+
void CreateObjects(string no, color cl) {
  ObjectCreate(no, OBJ_RECTANGLE, 0, 0,0, 0,0);
  ObjectSet(no, OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(no, OBJPROP_COLOR, cl);
  ObjectSet(no, OBJPROP_BACK, True);
}
//+------------------------------------------------------------------+
