I have modified the following alert (which is functionally working fine) to add a start and end time, however the alert still continues regardless and only being a cut and paste basic coder I am struggling to debug the issue. i have commented the lines I have added with // I ADDED THIS LINE.
Basicly I want to set start and end times for alerts to be sent.
If any one can help that would be great as I have tried a few combinations and the code compiles OK
#property indicator_chart_window
extern double TouchRangePips = 2;
extern double NextBarMovePips = 3;
extern int HoursOffset = -2;
extern int HourFrom=10; // hour // I ADDED THIS LINE
extern int HourTo=23; // hour - 24H // I ADDED THIS LINE
extern bool AudioAlert = true;
extern bool PopUpAlert = true;
extern bool EmailAlert = true;
extern bool PushAlert = true;
extern bool FromStartCheck = true;
double MyPoint;
string Lines[20]={"Pivot_Line", "R1_Line", "R2_Line","R3_Line","S1_Line","S2_Line","S3_Line", //7
"[PIVOT] MR1 Line","[PIVOT] MR2 Line","[PIVOT] MR3 Line","[PIVOT] MS1 Line","[PIVOT] MS2 Line","[PIVOT] MS3 Line", //6
"[PIVOT] Pivot Line","[PIVOT] R1 Line","[PIVOT] R2 Line","[PIVOT] R3 Line","[PIVOT] S1 Line","[PIVOT] S2 Line","[PIVOT] S3 Line" }; //7
int bNewBar = -1;
string sLine = "";
bool IsTimeToTrade() // I ADDED THIS LINE
{
bool OK = true; // I ADDED THIS LINE
if(Hour() < HourFrom) OK = false; // I ADDED THIS LINE
if((Hour() > HourTo)) OK = false; // I ADDED THIS LINE
return(OK); // I ADDED THIS LINE
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
if (Point == 0.00001) MyPoint = 0.0001; //5 digits
else if (Point == 0.001) MyPoint = 0.01; //3 digits (for Yen based pairs)
else MyPoint = Point; //Normal
if (FromStartCheck) {bNewBar = check_prev_bar();}
else bNewBar = -1;
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if (IsTimeToTrade() == true )//Start code if it is time to trade. // I ADDED THIS LINE
// check if previous bar touched within TouchRangePips range any of the lines
if (NewBar()) bNewBar = check_prev_bar();
if (bNewBar!=0)
{
if (bNewBar*(Close[0]-Open[0])/MyPoint>=NextBarMovePips)
{
string sDirection = "NO";
if (bNewBar == -1) sDirection = "YES";
if (AudioAlert) PlaySound("alert.wav");
if (PopUpAlert) Alert (Symbol()+" - "+sDirection+" from "+sLine+" on "+ Period()+" min Chart "); //+TimeToStr(Time[0],TIME_MINUTES)
if (EmailAlert) SendMail(Symbol()+" - "+sDirection+" from "+sLine,Symbol()+" Hourly To Finish Higher Than Start Level - "+sDirection+" at " +TimeToStr(TimeCurrent()+HoursOffset*60*60,TIME_MINUTES));
if (PushAlert) SendNotification (Symbol()+" Hourly To Finish Higher Than Start Level - "+sDirection); // Send Alert to Mobile Application
bNewBar = 0;
}
}
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
// Check if prev bar has close price near any level
//+------------------------------------------------------------------+
int check_prev_bar()
{
double dLinePrice;
sLine = "";
for (int i=0; i<20; i++)
{
dLinePrice = ObjectGet(Lines[i], OBJPROP_PRICE1);
if (Close[1]<=dLinePrice+TouchRangePips*MyPoint && Close[1]>=dLinePrice-TouchRangePips*MyPoint)
{
sLine = Lines[i];
if (Close[1]>Open[1]) return(1);
else return(-1);
}
}
return (0);
}
//+------------------------------------------------------------------+
// Check if new bar appeared
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime NewTime = 0 ;
if (NewTime == 0) NewTime = Time[0];
if(NewTime != Time[0])
{
NewTime=Time[0];
return (true);
}
return (false);
}
Basicly I want to set start and end times for alerts to be sent.
If any one can help that would be great as I have tried a few combinations and the code compiles OK
#property indicator_chart_window
extern double TouchRangePips = 2;
extern double NextBarMovePips = 3;
extern int HoursOffset = -2;
extern int HourFrom=10; // hour // I ADDED THIS LINE
extern int HourTo=23; // hour - 24H // I ADDED THIS LINE
extern bool AudioAlert = true;
extern bool PopUpAlert = true;
extern bool EmailAlert = true;
extern bool PushAlert = true;
extern bool FromStartCheck = true;
double MyPoint;
string Lines[20]={"Pivot_Line", "R1_Line", "R2_Line","R3_Line","S1_Line","S2_Line","S3_Line", //7
"[PIVOT] MR1 Line","[PIVOT] MR2 Line","[PIVOT] MR3 Line","[PIVOT] MS1 Line","[PIVOT] MS2 Line","[PIVOT] MS3 Line", //6
"[PIVOT] Pivot Line","[PIVOT] R1 Line","[PIVOT] R2 Line","[PIVOT] R3 Line","[PIVOT] S1 Line","[PIVOT] S2 Line","[PIVOT] S3 Line" }; //7
int bNewBar = -1;
string sLine = "";
bool IsTimeToTrade() // I ADDED THIS LINE
{
bool OK = true; // I ADDED THIS LINE
if(Hour() < HourFrom) OK = false; // I ADDED THIS LINE
if((Hour() > HourTo)) OK = false; // I ADDED THIS LINE
return(OK); // I ADDED THIS LINE
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
if (Point == 0.00001) MyPoint = 0.0001; //5 digits
else if (Point == 0.001) MyPoint = 0.01; //3 digits (for Yen based pairs)
else MyPoint = Point; //Normal
if (FromStartCheck) {bNewBar = check_prev_bar();}
else bNewBar = -1;
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if (IsTimeToTrade() == true )//Start code if it is time to trade. // I ADDED THIS LINE
// check if previous bar touched within TouchRangePips range any of the lines
if (NewBar()) bNewBar = check_prev_bar();
if (bNewBar!=0)
{
if (bNewBar*(Close[0]-Open[0])/MyPoint>=NextBarMovePips)
{
string sDirection = "NO";
if (bNewBar == -1) sDirection = "YES";
if (AudioAlert) PlaySound("alert.wav");
if (PopUpAlert) Alert (Symbol()+" - "+sDirection+" from "+sLine+" on "+ Period()+" min Chart "); //+TimeToStr(Time[0],TIME_MINUTES)
if (EmailAlert) SendMail(Symbol()+" - "+sDirection+" from "+sLine,Symbol()+" Hourly To Finish Higher Than Start Level - "+sDirection+" at " +TimeToStr(TimeCurrent()+HoursOffset*60*60,TIME_MINUTES));
if (PushAlert) SendNotification (Symbol()+" Hourly To Finish Higher Than Start Level - "+sDirection); // Send Alert to Mobile Application
bNewBar = 0;
}
}
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
// Check if prev bar has close price near any level
//+------------------------------------------------------------------+
int check_prev_bar()
{
double dLinePrice;
sLine = "";
for (int i=0; i<20; i++)
{
dLinePrice = ObjectGet(Lines[i], OBJPROP_PRICE1);
if (Close[1]<=dLinePrice+TouchRangePips*MyPoint && Close[1]>=dLinePrice-TouchRangePips*MyPoint)
{
sLine = Lines[i];
if (Close[1]>Open[1]) return(1);
else return(-1);
}
}
return (0);
}
//+------------------------------------------------------------------+
// Check if new bar appeared
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime NewTime = 0 ;
if (NewTime == 0) NewTime = Time[0];
if(NewTime != Time[0])
{
NewTime=Time[0];
return (true);
}
return (false);
}