Loading…

Trade Session Filter

A session filter looks trivial and has one trap that catches almost every hand-rolled version of it: a session defined as "22:00 to 02:00", a perfectly normal way to describe the Asia session, crosses midnight. A naive comparison of startMinutes <= nowMinutes <= endMinutes is false for every single minute of that window, because startMinutes is numerically larger than endMinutes. The filter silently never fires, and because the code reads as obviously correct - it is a plain range check - the bug survives review. SsfIsWithinHours detects the wrap case (start > end) and inverts the comparison automatically: membership becomes "at or after start, OR before end" instead of "at or after start AND before end". The caller passes hours and minutes exactly as a trader would describe the session and never has to think about which side of midnight it falls on. SsfIsWeekend is a deliberately coarse guard - Saturday or Sunday, server time - because forex brokers differ on the exact minute the week's first and last candles land, and a precise open/close time is broker-specific. SsfIsWithinSession combines the two for the everyday case, with an allowWeekend flag for instruments (some crypto CFDs) that do trade through the weekend. Notes and limitations - All comparisons use server time (TimeCurrent()), not the local terminal time or GMT. Confirm the broker's server time zone before setting session hours, since it varies by broker and by daylight saving convention. - A window where start equals end is zero-width by design, not "all day" - under the half-open [start, end) rule, no minute can be both at-or-after and before the same value. Use 00:00-23:59 (or 00:00-24:00) to mean the full day; the demo shows this explicitly. - SsfIsWeekend is a coarse Saturday/Sunday check, not an exact market open/close boundary. Combine it with SsfIsWithinHours if the Friday-evening or Sunday-evening edge needs to match a specific broker's calendar exactly.

Script · MT5 · v1.0 · Free

About Trade Session Filter

Trade Session Filter is a script for MetaTrader 5. It has 4 settings, such as London start hour, London end hour and Asia start hour. MQL source code included.

Files in the download: SessionFilter.mq5.mqh, SessionFilter.txt, SessionFilterDemo.ex5, SessionFilterDemo.mq5.

Settings

SettingDefaultWhat it does
London start hour8London session start, server time
London end hour17London session end, server time
Asia start hour22Asia session start, server time (crosses midnight)
Asia end hour6Asia session end, server time

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Trading Sessions · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria