Session Schedule Compiler
MQL4 has no built-in timezone or daylight-saving database. A trading session defined in its own local time (e.g. London 08:00-16:30) only maps correctly onto the broker's server time if today's DST offset for that timezone is known - and recomputing "is DST active" from the US or EU calendar rule (2nd Sunday of March, last Sunday of October, and so on) on every single OnTick is wasted work and a likely source of subtle bugs if the boundary-date arithmetic is inlined in several places. This library splits the work into a compile step and a lookup step. SessionScheduleCompile() takes a SessionSpec (local open/close hour and minute, the timezone's standard-time GMT offset, and a DST rule of NONE/US/EU) plus the broker's own fixed server-GMT offset, works out whether DST is currently active using the appropriate calendar rule, and produces a CompiledSession holding a plain server-time minute-of-day start/end window. SessionScheduleIsActive() then answers with two integer comparisons - no date arithmetic at all. SessionScheduleEnsureFresh() recompiles automatically only when the server calendar day has moved on, since a DST boundary can only ever fall on a day change. Notes and limitations - InpServerGmtOffsetHours (or the equivalent parameter your own EA passes) must be supplied by the user - MQL4 has no function that reports the broker server's own GMT offset. Getting it wrong shifts every compiled session by the same fixed amount. - The DST boundary calculation (SessionScheduleIsDstActive) evaluates the US/EU calendar rule using the "moment" value passed to it exactly as given - it does not itself convert server time into the session's home timezone first. SessionScheduleCompile() calls it with the server-time value, which is an approximation: the DST switch-over MOMENT is correct to the day in the overwhelming majority of cases, but a session whose home timezone is many hours away from server time could theoretically observe the DST flip a day early or late right at the boundary weekend itself. This is a known, documented edge case, not a silent one. - Only the US and EU DST rules are implemented. A third rule (Australia's rule, for example, which runs in the opposite half of the year) would need its own case added to SessionScheduleIsDstActive() - SESSION_DST_NONE is available today for any timezone that does not observe DST or whose rule is not yet built in. - A session is defined by ONE open/close pair per day. Split sessions (e.g. a market that closes for a midday break) need two SessionSpec/ CompiledSession pairs, checked independently.
Script · MT4 · v1.0 · Free
About Session Schedule Compiler
Session Schedule Compiler is a script for MetaTrader 4. It has 1 setting, such as Server gmt offset hours. MQL source code included.
Files in the download: SessionScheduleCompiler.mqh, SessionScheduleCompiler.txt, SessionScheduleCompilerDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Server gmt offset hours | 2 | This broker's server clock offset from GMT (check your broker's docs) |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trading Sessions · MT4 Script · MQL4 Source Code