//+------------------------------------------------------------------+
//|                                            Vertical Line_NRW.mq4 |
//|                       Copyright © 2022, nwesterhuijs@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, nwesterhuijs@hotmail.com"
#property indicator_chart_window

enum enDOW
{
   Sunday,     
   Monday,     
   Tuesday,    
   Wednesday,  
   Thursday,   
   Friday,     
   Saturday,    
};

bool PlotOnlyCurrentWeek = true;

extern enDOW Day_Num1 = Tuesday; //Day
extern string Hour_Num1   = "04:00"; //Hour Number:
extern color Line_Color1=Orange;

extern enDOW Day_Num2 = Wednesday; //Day
extern string Hour_Num2   = "04:00"; //Hour Number:
extern color Line_Color2=Orange;

extern enDOW Day_Num3 = Thursday; //Day
extern string Hour_Num3   = "08:00"; //Hour Number:
extern color Line_Color3=Orange;

extern int Line_Style=1;
extern int Line_Width=1;
extern bool Draw_as_Background=true;

int BarsBack = 10080;

extern bool            alertsOn         = true;            // Turn alerts on?
extern bool            alertsMessage    = true;            // Alerts should show pop-up message
extern bool            alertsSound      = false;           // Alerts should play alert sound
extern bool            alertsPushNotif  = false;           // Alerts should send push notification
extern bool            alertsEmail      = false;           // Alerts should send email

int limit, i;
string almes;

int init()
{
	return(0);
}

int deinit()
{
	ObjectDelete("V-Line1"); 
	ObjectDelete("V-Line2");
	ObjectDelete("V-Line3");
	
	return(0);
}

int start()
{

	int Counted_bars=IndicatorCounted(); // Number of counted bars   
	limit=MathMin(BarsBack, Bars-Counted_bars-1);           // Index of the first uncounted  

    for(i=limit; i>=0; i--)
    {   
		if(!PlotOnlyCurrentWeek || (WeekNumber(Time[0],false) == WeekNumber(Time[i],false)))
		{
			if(TimeDayOfWeek(Time[i]) == Day_Num1 && StringToTime(Hour_Num1)==Time[i+1])
			{			
				ObjectDelete("V-Line1");
				ObjectCreate("V-Line1",OBJ_VLINE,0,Time[i+1],Bid);
				ObjectSet("V-Line1",OBJPROP_COLOR,Line_Color1);
				ObjectSet("V-Line1",OBJPROP_STYLE,Line_Style);
				ObjectSet("V-Line1",OBJPROP_WIDTH,Line_Width);
				ObjectSet("V-Line1",OBJPROP_BACK,Draw_as_Background);
				almes=_Symbol+timeFrameToString(_Period)+" "+DayOfWeekToString(Day_Num1)+" Reversal CANDLE "+Hour_Num1+" CLOSE";
				doAlert(almes);
			}
			if(TimeDayOfWeek(Time[i]) == Day_Num2 && StringToTime(Hour_Num2)==Time[i+1])
			{			
				ObjectDelete("V-Line2");
				ObjectCreate("V-Line2",OBJ_VLINE,0,Time[i+1],Bid);
				ObjectSet("V-Line2",OBJPROP_COLOR,Line_Color2);
				ObjectSet("V-Line2",OBJPROP_STYLE,Line_Style);
				ObjectSet("V-Line2",OBJPROP_WIDTH,Line_Width);
				ObjectSet("V-Line2",OBJPROP_BACK,Draw_as_Background);
				almes=_Symbol+timeFrameToString(_Period)+" "+DayOfWeekToString(Day_Num2)+" Reversal CANDLE "+Hour_Num2+" CLOSE";
				doAlert(almes);
			}	
			if(TimeDayOfWeek(Time[i]) == Day_Num3 && StringToTime(Hour_Num3)==Time[i+1])
			{			
				ObjectDelete("V-Line3");
				ObjectCreate("V-Line3",OBJ_VLINE,0,Time[i+1],Bid);
				ObjectSet("V-Line3",OBJPROP_COLOR,Line_Color3);
				ObjectSet("V-Line3",OBJPROP_STYLE,Line_Style);
				ObjectSet("V-Line3",OBJPROP_WIDTH,Line_Width);
				ObjectSet("V-Line3",OBJPROP_BACK,Draw_as_Background);
				almes=_Symbol+timeFrameToString(_Period)+" "+DayOfWeekToString(Day_Num3)+" Reversal CANDLE "+Hour_Num3+" CLOSE";
				doAlert(almes);
			}			
		}
	}
	return(0);
}
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
int WeekNumber(datetime d, bool monday=false)  {
//+------------------------------------------------------------------+
// Returns julian week number since Jan 1, 1970
// If monday=true, weeks start on Mondays; otherwise Sundays
  if (monday)
    return(MathInt((d+3*86400)/7/86400));  
  else  
    return(MathInt((d+4*86400)/7/86400));  
}

//+------------------------------------------------------------------+
double MathInt(double n, int d=0)  {
//+------------------------------------------------------------------+
// Corrects a rounding/accuracy bug in MQL4's MathFloor function
//   (use MathInt(n) instead of MathFloor(n)
// Rounds n DOWN to d decimal places, e.g.
// MathInt(2.57,1) returns 2.5
// MathInt(2.99)   returns 2
   return(MathFloor(n*MathPow(10,d)+0.000000000001)/MathPow(10,d));
}  
//+------------------------------------------------------------------+
string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int cnt=ArraySize(iTfTable)-1; cnt>=0; cnt--) 
         if (tf==iTfTable[cnt]) return(sTfTable[cnt]);
                              return("");
}
//+------------------------------------------------------------------+
void doAlert(string mes)
{
   static datetime previousTime;
   string message=mes;
   
   if (previousTime != Time[0]) 
    {
       previousTime   = Time[0];
          if (alertsMessage)   Alert(message);
          if (alertsEmail)     SendMail(Symbol()+" StochHistogram",message);
          if (alertsPushNotif) SendNotification(message);
          if (alertsSound)     PlaySound("alert2.wav");
   }
}
//+------------------------------------------------------------------+
string DayOfWeekToString(int day)
 {
 string res="";
         if(Day_Num1==0||Day_Num2==0||Day_Num3==0)
         res="Sunday";
         if(Day_Num1==1||Day_Num2==1||Day_Num3==1)
         res="Monday";
         if(Day_Num1==2||Day_Num2==2||Day_Num3==3)
         res="Tuesday";
         if(Day_Num1==3||Day_Num2==3||Day_Num3==3)
         res="Wednesday";
         if(Day_Num1==4||Day_Num2==4||Day_Num3==4)
         res="Thursday";
         if(Day_Num1==5||Day_Num2==5||Day_Num3==5)
         res="Friday";
         if(Day_Num1==6||Day_Num2==6||Day_Num3==6)
         res="Saturday";
 return(res);
 }
//+------------------------------------------------------------------+
