How to Use Time Filters in MQL4
rfxsignals October 14, 2025 No Comments
How to Use Time Filters in MQL4

How to Use Time Filters in MQL4 — Practical Guide & Code Examples

Time filters are essential when you want your Expert Advisor (EA) or indicator to operate only at specific market hours (e.g., London open, NY close) or to block trading during low-liquidity sessions. This guide shows practical patterns and robust MQL4 code you can drop into your EA to include time filters, timezone-safe checks, and trading-session windows — all while keeping the code clean and lightweight for live use.

Why use time filters?

Time filters let you control when an EA opens or manages trades. Common uses:

  • Run scalping logic only during high-liquidity hours.
  • Block trading during major news events or weekends.
  • Separate logic per session (e.g., London vs New York)

Core concepts & timezone notes

MQL4 uses server time returned by TimeCurrent() which depends on your broker. Don't assume it equals UTC. Two common approaches:

  1. Use broker server time (fast & simple) — best for brokers you trust and when you want consistent behavior with quotes and orders.
  2. Convert to UTC/local by applying an offset (safer across brokers but requires managing DST).

We'll show both approaches and also a DST-aware option to avoid trading at wrong times when clocks change.

Basic MQL4 time filter (simple window)

Drop this into your EA's main loop or a helper function. It uses server time and checks if current hour is inside allowed window.

// Returns true if current hour is between startHour and endHour (inclusive)
bool IsWithinHours(int startHour, int endHour)
{
  datetime now = TimeCurrent();
  int hour = TimeHour(now);
  if(startHour <= endHour)
    return (hour >= startHour && hour <= endHour);
  // window wraps midnight, e.g., 22 - 04
  return (hour >= startHour || hour <= endHour);
}

// usage example:
if(IsWithinHours(7, 16)) // only trade between 07:00 and 16:59 server time
{
  // trading code here
}

Note: This checks hour only — for minute precision use TimeMinute() and compare full datetimes.

Advanced session windows & DST-safe approach

For session-specific control (e.g., London open: 08:00–16:30 UTC) convert server time to UTC using a configured offset and update offset when DST changes.

int BrokerUTCOffsetHours = 2; // set this once for your broker (example: +2)
bool IsWithinUTCWindow(int startHourUTC, int endHourUTC)
{
  datetime nowServer = TimeCurrent();
  datetime nowUTC = nowServer - BrokerUTCOffsetHours*60*60;
  int h = TimeHour(nowUTC);
  if(startHourUTC <= endHourUTC)
    return (h >= startHourUTC && h <= endHourUTC);
  return (h >= startHourUTC || h <= endHourUTC);
}

To handle DST: either update BrokerUTCOffsetHours on DST change or implement a small routine that checks GMT offset from a trusted web API (requires external call which is not always available).

Practical examples

1) Minute-precise trading window

bool IsBetween(datetime startDT, datetime endDT)
{
  datetime now = TimeCurrent();
  return (now >= startDT && now <= endDT);
}

// usage:
datetime start = StrToTime("2025.10.14 07:30"); // server-time formatted string
datetime end   = StrToTime("2025.10.14 16:45");
if(IsBetween(start, end)) { /* allow trading */ }

2) Block trading during news (using scheduled windows)

Predefine news blocks and skip trading if current time falls inside any block — store blocks in an array for quick checking.

3) Session-based flags for multi-strategy EAs

Toggle strategyA during London session and strategyB during NY session using IsWithinUTCWindow. This reduces false signals and improves performance.

Best practices

  • Log time checks for first 24 hours to confirm broker offsets.
  • Test on demo with server time in logs.
  • Keep time logic centralized (one helper function) to avoid mistakes.

SEO & Link strategy (inbound & outbound)

For SEO, add internal links to cornerstone pages (for example, link to your signals page and related long-form posts). Example inbound links we recommend:

Outbound (authority) links improve relevance: link to official docs for MQL4 and authoritative resources:

Need help implementing this in your EA?

We can integrate time filters and test on your demo account — fast and safe. Contact us directly using the buttons below.

Or visit our Contact page for more options.

Quick FAQ

Q: Should I use server time or UTC?
A: Use server time for consistency with orders/prices. Convert to UTC if you maintain multi-broker deployments.
Q: How to handle DST?
A: Update your broker offset on DST changes or implement an external time check; log and test thoroughly.

Author: RFXSignals — Expert EAs & Signals