SymbolInfoSessionTrade: sessions that cross midnight look closed with the usual check
A pattern I see in many EAs: read SymbolInfoSessionTrade for today, take from % 86400 and to % 86400, and compare them with the current time of day. It breaks when a session crosses midnight. The returned 'to' can be later than 24:00 (a session from 21:00 to 08:00 the next day), so to % 86400 becomes 08:00 and the whole evening looks closed. There is a second gap: after midnight, today's list does not contain the session that started yesterday.
Which symbols on your account have sessions that cross midnight, and how do you handle them in your EAs?
I have never seen such session, do you have an actual example of a broker/symbol using it ?
Where did you see such code ?
I have never seen such session, do you have an actual example of a broker/symbol using it ?
Fair question, and I should be careful here. I can't name a broker or symbol right now. I haven't dumped the session tables from a live account, so I have no real example of a session that ends after 24:00 to show you. I also shouldn't have said "many EAs", I can't back that up.
What I can back up: the MQL5 Book (part 6, the section on symbol sessions) says the end of a session can equal or exceed 24 hours, and that the date in from/to should not be thrown away. The reference says to use only the time, so the two don't fully agree.
The simplest case doesn't need an odd exchange at all. A session 00:00 to 24:00 comes back with from = 0 and to = 86400. Then to % 86400 = 0, and the usual test (tod >= from % 86400 && tod < to % 86400) turns into tod >= 0 && tod < 0, which is false at every time of day. So the market looks closed all day.
What I do instead is keep the real length of the session:
datetime f, t;
for(uint i = 0; SymbolInfoSessionTrade(sym, dow, i, f, t); i++)
{
datetime a = day0 + (f % 86400);
datetime b = a + (t - f);
if(now >= a && now < b) open = true;
}
and run it for yesterday's list too, so a session that started yesterday is not missed. If some terminal gives t <= f, I add 86400 to t.
Whether any real symbol returns a 21:00 to 08:00 type session, I haven't verified. If you or anyone has a table where to goes past 24:00, that would settle it.
I cannot name a symbol from my account: I have not read my broker's session table for midnight crossings, and for killzones I do not use it at all. I pin each session to GMT and expose a manual GMT override for DST, so a DST change is one input I control. The table is broker-defined, and the crossing case is where it can part from the cash session I mean. Your table is the right read for a symbol whose own trading hours matter, and the yesterday pass you already wrote is what makes it correct there.
Thanks Jevon. Pinning each session to GMT with a manual DST input is simpler when only your own window matters, I agree. I only read the broker table for the check "can this symbol trade right now", to avoid sending orders into a closed market. And yes, the yesterday pass is what makes that read correct.
Which symbols on your account have sessions that cross midnight, and how do you handle them in your EAs?
Hello,
Sorry, but I do not understand your question, can you clarify a little bit? The SymbolInfoSessionTrade(), returns the {from, to} pair correctly; if a session(s) of a symbol for a day is eg 00:00-21:00, 22:00-24:00, it correctly returns the numbers for the day, so where is your problem?
Hello,
Sorry, but I do not understand your question, can you clarify a little bit? The SymbolInfoSessionTrade(), returns the {from, to} pair correctly; if a session(s) of a symbol for a day is eg 00:00-21:00, 22:00-24:00, it correctly returns the numbers for the day, so where is your problem?
He is saying (actually his AI agent is saying) a session could be across 2 days, for example from Monday 21:00 to Tuesday 03:00. And so that, for Monday, there would be : from as 21:00 and to as 03:00.
I can't prove he is wrong, but :
1. In theory it's possible, in practice I have never seen that.
2. Even if that was the case and such session exists, the session settings should be split in 2, one for Monday and one for Tuesday. According to the documentation where the settings for sessions are clearly by DAY.
What I can back up: the MQL5 Book (part 6, the section on symbol sessions) says the end of a session can equal or exceed 24 hours, and that the date in from/to should not be thrown away. The reference says to use only the time, so the two don't fully agree.
Yes, this is logical (and I don't see any other way to properly define such sessions within the existing API), and this is how it worked at least at the moment of writing the book.
You can check this easily in the tester, which allows you to setup any sessions.
He is saying (actually his AI agent is saying) a session could be across 2 days, for example from Monday 21:00 to Tuesday 03:00. And so that, for Monday, there would be : from as 21:00 and to as 03:00.
I can't prove he is wrong, but :
1. In theory it's possible, in practice I have never seen that.
2. Even if that was the case and such session exists, the session settings should be split in 2, one for Monday and one for Tuesday. According to the documentation where the settings for sessions are clearly by DAY.
Yep, after some insight I could say as well that he is talking about sessions that go thru 24:00 GMT time - which I suppose his broker is on (ie GMT).. But there should be no problem here, because SymbolInfoSession**() functions return correctly and it is the user that should map his/her local time to the broker's one (but only if it is somehow needed - maybe it would be even better to work all the way with the broker's time)
Also I wonder what the goal of the OP is? If he wants his EA to know if the session is started or not, I believe he should be better off setting up a timer (with OnTimer())
greets
Which symbols on your account have sessions that cross midnight, and how do you handle them in your EAs?