As you have been asked before . . . .
Please use this to post code . . . it makes it easier to read.
How do you know what is going on . . you have no Print statements in that code . . .
- Use SRC
datetime now = TimeCurrent(); int DOW = TimeDayOfWeek(now); // forum.mql4.com/33851 reports DayOfWeek() // always returns 5 in the tester.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello to the forum.
I have an unusual problem regarding a time filter. In backtesting the filter works well ( no problems ). When running the EA on a live account, the filter still works well, but there is a problem with the difference in server time and local time.
EA is set to trade 17:00 GMT to 05:00 GMT.
Example: My local time (on computer) is 23:59 tuesday. Server time is GMT 12:59 tuesday. When the local time clicks over to 00:00 Wednesday, the EA allows trading. This means that the EA allows trading at 13:00 GMT instead of 17:00 GMT. When I set my local time on PC to match GMT there is no problem at all, EA is good.
I think there may be a problem with DayOfWeek() recognition, perhaps a bug in the code. I think this is either one of those simple jobs, or I will have to ditch the whole filter and start again. See Code snippet below.
Any help with this is very much appreciated.
Regards, Chris.
//+------------------------------------------------------------------+
int start()// Start Function
//+------------------------------------------------------------------+
{
if(!start) return; // if error then exit
if(!IsTradeAllowed() && !IsTesting())
{
Comment("\nWarning: Experts are not allowed live trading. Please check advisor properties");
return(0);
}
comment="";
if(Time[0]!=time_saved2){ time_saved2=Time[0]; addpos = true;} //enable open 2/3 pos
//
if(USE_TIME_LIMITS)
{
time_start = BuildTime(START_HOUR, START_MINUTE, 0);
time_stop = BuildTime(STOP_HOUR, STOP_MINUTE, 0);
time_break = BuildTime(BREAK_FRIDAY_HOUR, BREAK_FRIDAY_MINUTE, 0);
comment = comment + "\nDay of Week: " + DayOfWeekToStr() + ", Time: " + TimeToStr(TimeCurrent(),TIME_MINUTES|TIME_SECONDS);
switch (DayOfWeek())
{
// 1-5
case 1:
case 2:
case 3:
case 4:
case 5:
comment = comment + "\n" + WorkingTimeToStr(time_start,time_stop);
close_all=false;
if(time_start < time_stop)
{
if(TimeCurrent() >= time_start && TimeCurrent() < time_stop)
{
if(Time[0]!=time_saved){ time_saved=Time[0]; signals = true; }
}
else signals = false;
}
else
{
if(TimeCurrent() >= time_start || TimeCurrent() < time_stop)
{
if(Time[0]!=time_saved){ time_saved = Time[0]; signals = true; }
}
else signals = false;
}
//--- close
if(DayOfWeek()==5 && TimeCurrent() >= time_break) {close_all = true; signals=false; addpos=false;}
break;
case 6:
case 0:
signals = false;
addpos = false;
break;
}// end switch
}// end USE_TIME_LIMITS
//--- no time limit
else
{
comment = comment + "Day of Week: " + DayOfWeekToStr();
comment = comment + "\nTime: around the clock";
if(Time[0]!=time_saved){ time_saved = Time[0]; signals = true; }
}
if(close_all)
{
CloseAllPositions(MAGIC_NUMBER);
return(0);
}
// Functions used in EA...
//+------------------------------------------------------------------+
string WorkingTimeToStr(datetime time_start,datetime time_stop/*, datetime time_break*/)
//+------------------------------------------------------------------+
{
string res = "Time: " + TimeToStr(time_start,TIME_MINUTES) +
" - " + TimeToStr(time_stop,TIME_MINUTES);
return(res);
}
//+------------------------------------------------------------------+
datetime BuildTime(int hours, int minutes, int offset)
//+------------------------------------------------------------------+
{
int new_hour;
if(hours + offset >= 24)new_hour = hours + offset - 24;
else if(hours + offset < 0) new_hour = 24 + hours + offset;
else new_hour = hours + offset;
string str_time = StringConcatenate(new_hour,":",minutes);
datetime time = StrToTime(str_time);
return(time);
}
//+------------------------------------------------------------------+
string DayOfWeekToStr()
//+------------------------------------------------------------------+
{
switch(DayOfWeek())
{
case 0: return("Sunday");
case 1: return("Monday");
case 2: return("Tuesday");
case 3: return("Wednesday");
case 4: return("Thursday");
case 5: return("Friday");
case 6: return("Saturday");
}
}