What you have to do is to shift the start and end time in some cases and if your start if after your end (from 20:00 to 02:00).
Example if the current time is before the start, start must be shifted back by 24 hours.
Example if the current time is after the start, end must be shifted forward by 24 hours.
Example how you can code it:
if(start_time > end_time && TimeCurrent() < start_time) start_time -= 24*60*60;
else if(start_time > end_time && TimeCurrent() > start_time) end_time += 24*60*60;
You must define your trading session. Example :
start_time = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE) + " " + Time_To_Start);
end_time = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE) + " " +Time_To_Stop);
if(start_time > end_time && TimeCurrent() < start_time) start_time -= 24*60*60;
else if(start_time > end_time && TimeCurrent() > start_time) end_time += 24*60*60;
To be tested. That's just a quick piece of code for the example ...
input double SessionStart =22.00; input double SessionEnd =05.00; //----------------------------------------- if(Session()) { } //----------------------------------------- bool Session() { double Servertime=Hour()+(Minute()*0.01); if((SessionEnd-SessionStart)<0 && (Servertime<SessionEnd || Servertime>=SessionStart)) return (true); if((SessionEnd-SessionStart)>0 && Servertime<SessionEnd && Servertime>=SessionStart) return (true); if(SessionStart+SessionEnd==0) return (true); return (false); }
I think I got it now and it´s working fine it seems. Anyone seeing a problem with this that I might have missed?
bool IsTradingSession(int FuncStartHour, int FuncEndHour) { if (FuncStartHour < FuncEndHour) { if (Hour() >= FuncStartHour && Hour() <= FuncEndHour) return(TRUE); return(FALSE); } if (FuncStartHour > FuncEndHour) { if (Hour() >= FuncStartHour && Hour() <= 23) return(TRUE); if (Hour() <= FuncEndHour) return(TRUE); return(FALSE); } if (Hour() == FuncStartHour) return(TRUE); return(FALSE); }
Thanks a lot, much appreciated. However, what is "Endl", that variable is not declared.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi :)
I have a check for trading hours in my EA that I want to extend. Currently it´s working like this:
This check works great, however, only if EndHour is not extending beyond "23". But I want to be able to defne a range of for example: 20 to 2, in which case the above approach will not work correctly as you see. It will work again if using 0 to 2, but not for 20 to 2.
Can someone help please and adjust the code? Thank you:)