Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Broker Clock - library for MetaTrader 5

Hayk Babayan
Hayk Babayan
Algorithmic trader. I build and test Expert Advisors for MetaTrader 5 with one rule: a strategy has to prove itself on data it has never seen before I trust it.
Views:
2
Published:
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

An EA that trades at "08:00 London" or "09:30 New York" has to turn that into broker server time, and the answer changes during the year. Most brokers run their server at UTC+2 in winter and UTC+3 in summer, switching on the US dates. Europe switches two to three weeks later in spring and one week earlier in autumn, so for about a month a year a fixed server hour points at the wrong market hour. In the Strategy Tester it is worse: TimeGMT() simply follows the server clock, so the offset cannot be measured at all.

BrokerClock.mqh is one include file with the rules built in. It works on any date, so backtests over many years use the right offset for every single day.

BrokerClockPanel example indicator

What it does

  • Daylight saving rules for the US, the EU (London, Frankfurt) and Sydney; Tokyo has none.
  • Three common broker server clocks: UTC+2/+3 on US dates ("New York close", the most common), UTC+2/+3 on EU dates, and UTC+0/+1 (London). A fixed offset is also possible.
  • Live, the rule can be detected from the current server offset (SERVER_AUTO). In the tester, set it explicitly.
  • Conversions between server time, UTC and market local time in every direction.
  • ZoneTimeOnServer(): "08:00 London today" as server time, in one call.
  • Tested every hour from 2015 to 2030 against the IANA time zone database: no differences.

Example

#include "BrokerClock.mqh"
CBrokerClock clock;

int OnInit()
  {
   clock.Init(SERVER_NY_CLOSE);          // or SERVER_AUTO on a live account
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
   datetime now =TimeCurrent();
   datetime open=clock.ZoneTimeOnServer(now,CLOCK_LONDON,8,0);   // today's 08:00 London, in server time
   if(now>=open && now<open+60)
      Print("London is opening");
  }

Example indicator

BrokerClockPanel shows the server time, UTC and the five markets with their current zone (GMT/BST, EST/EDT, ...), and the server time of any market hour you choose. Put BrokerClock.mqh in the same folder as the indicator.

Notes

  • The US rule is the one used since 2007, and Sydney's since 2008. Dates before that are not covered.
  • In autumn one clock hour happens twice. For that hour the conversions return the earlier moment.
  • In SERVER_AUTO mode the offset is measured from your PC clock (TimeGMT), so keep the PC clock synchronised.
Market Structure Visualizer - HH HL LH LL BOS CHoCH Market Structure Visualizer - HH HL LH LL BOS CHoCH

Free non-repainting MT5 market structure indicator that identifies confirmed HH, HL, LH and LL swings, plots BOS and CHoCH structure breaks, and includes close/wick confirmation, dashboard bias and optional alerts.

Risk Lot Calculator Risk Lot Calculator

Drop it on the chart at your stop-loss level and get the exact lot for your risk. Correct for gold, indices, crypto and cross pairs. It never opens trades.

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

MACD Signals MACD Signals

Indicator edition for new platform.