MT5 Expert Advisor Development
You write the rules. We implement them in MQL5 as a working Expert Advisor that runs on your own MetaTrader 5 terminal, …
Read moreTime filters look like the easy part of a build. They are where a large share of real bugs live, because server time is not your time and midnight is a special case.
The first thing to settle in any time-based rule is which clock it refers to. MQL5 gives you three, and they are routinely different:
| Function | Returns |
|---|---|
TimeCurrent() | Last known server time — the one your rules should almost always use |
TimeLocal() | The clock on the machine running the terminal |
TimeGMT() | GMT, derived from the local machine's clock and timezone |
A VPS in Germany running an EA for a client in Varanasi on a broker with a GMT+3 server has three different "nows". Hard-code nothing; decide explicitly, write it in the specification, and log which clock the filter used.
input int StartHour = 9;
input int StartMin = 15;
input int EndHour = 15;
input int EndMin = 15;
bool InSession()
{
MqlDateTime t;
TimeToStruct(TimeCurrent(), t);
int now = t.hour * 60 + t.min;
int start = StartHour * 60 + StartMin;
int end = EndHour * 60 + EndMin;
if(start <= end) return (now >= start && now < end);
// Window crosses midnight, e.g. 22:00 -> 03:00
return (now >= start || now < end);
}
That last branch is the midnight-wrap bug, and it is in a great many EAs. Without it, a 22:00–03:00 window is simply never true, and the EA silently never trades. Nothing errors; it just does nothing, which is the hardest kind of bug to notice.
Usually simpler than they look, with two caveats: MqlDateTime.day_of_week is 0
for Sunday, and Sunday is a trading day on several brokers for part of the evening.
Blocking "the weekend" by blocking Saturday alone leaves Sunday open.
This is the question most briefs forget. When the session window closes and a position is still open, there are four defensible answers:
All four are reasonable. The software has to do exactly one. Decide it in writing before the build, or you will be deciding it during a live session.
MT5 exposes the economic calendar to code, which is better than the old approach of downloading a CSV that goes stale:
MqlCalendarValue values[];
datetime from = TimeCurrent() - 3600;
datetime to = TimeCurrent() + 3600;
if(CalendarValueHistory(values, from, to, NULL, NULL))
{
for(int i = 0; i < ArraySize(values); i++)
{
MqlCalendarEvent ev;
if(!CalendarEventById(values[i].event_id, ev)) continue;
if(ev.importance < CALENDAR_IMPORTANCE_HIGH) continue;
// within the blocked window around a high-importance event
}
}
Three practical notes. The calendar is only populated if the terminal has downloaded it, so check and fail loudly rather than silently treating "no events" as "all clear". Filter by currency so a US release does not block a trade on an unrelated symbol. And the Strategy Tester has limited calendar access, so a news filter that works live may behave differently in a backtest — a fact worth stating in the delivery notes rather than discovering later.
A filter that blocks silently is indistinguishable from a broken EA. Log the reason, every time:
if(!InSession())
{
if(g_lastBlockReason != "session")
{
Print("Entry blocked: outside session window");
g_lastBlockReason = "session";
}
return;
}
Guarding on the last reason keeps the log readable instead of printing the same line on every tick. When a client asks "why did it not trade yesterday", this log answers in seconds.
You write the rules. We implement them in MQL5 as a working Expert Advisor that runs on your own MetaTrader 5 terminal, …
Read moreRule-based automation for Nifty, BankNifty, MCX and equity segments, connected to your own broker terminal or broker API…
Read moreSend your rules, your script or your existing file on WhatsApp and we will reply with a written scope, price and timeline.