Session Detection
Session boundaries are conventionally quoted in GMT, but TimeCurrent() in MQL4 returns the broker's server time, which can sit anywhere from GMT-5 to GMT+3 depending on the broker. Code that compares TimeHour(TimeCurrent()) directly against textbook session hours is silently wrong on every broker whose server offset is not exactly zero - it reports session changes hours early or late, and because the mistake compiles clean and mostly looks plausible, it tends to ship. Every function in this header takes its time reading from the built-in TimeGMT(), so the session windows are correct without the caller having to know or supply the broker's offset separately. The other detail worth naming is midnight wraparound. Sydney's session runs 21:00 to 06:00 GMT, which crosses midnight - a naive "hour >= start && hour < end" check returns false for the entire window on the wrong side of midnight. IsInGmtWindow, the primitive every session function is built on, detects a wrapping window (start hour greater than end hour) and handles it correctly; the demo proves this at the exact hour-21 and hour-6 boundaries rather than just somewhere in the middle of the window where the bug would not show up anyway. Session hours used are the commonly cited approximations: Sydney 21:00-06:00, Tokyo 23:00-08:00, London 07:00-16:00, New York 12:00-21:00 GMT. These are a reasonable default for general use, not an exchange-published standard - trading desks vary the exact boundaries, and this header does not attempt to model holiday calendars or early/late closes. Notes and limitations - Session hours are approximations, not exchange rules. Adjust the constants in IsSydneySession/IsTokyoSession/IsLondonSession/IsNewYorkSession directly if your use case needs different boundaries. - No holiday or early-close awareness. A public holiday in London does not change what IsLondonSession reports. - With the boundaries used here the four windows tile the full 24 hours with no gap, so ActiveSessionsToString should never return "None" in normal use - it is a defensive fallback for callers who narrow the windows via IsInGmtWindow. - GMT is read from the built-in TimeGMT(), which itself depends on the local computer's time zone settings being correct - see the ServerTime library notes if that matters for your setup.
Script · MT4 · v1.0 · Free
About Session Detection
Session Detection is a script for MetaTrader 4. MQL source code included.
Files in the download: SessionDetect.mqh, SessionDetect.txt, SessionDetectDemo.mq4.
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