MQL4 date and time

MQL4 time functions and server-time boundaries

TimeCurrent returns the last known server time, while TimeLocal uses the computer clock and TimeGMT derives GMT from the local environment. Use iTime for a bar opening time, TimeToStruct for calendar fields, and make the timezone of any external timestamp explicit.

Open the Coding Agent, then choose MQL4 from the language menu.

TimeFunctionsExample.mq4.mq4
#property strict

void OnStart()
  {
   datetime server_time = TimeCurrent();
   datetime bar_time = iTime(NULL, 0, 0);
   MqlDateTime parts;

   if(!TimeToStruct(server_time, parts))
     {
      Print("TimeToStruct failed. Error ", GetLastError());
      return;
     }

   int period_seconds = PeriodSeconds(PERIOD_CURRENT);

   PrintFormat("server=%s hour=%d bar=%s periodSeconds=%d",
               TimeToString(server_time, TIME_DATE|TIME_SECONDS),
               parts.hour,
               TimeToString(bar_time, TIME_DATE|TIME_MINUTES),
               period_seconds);
  }
  • TimeCurrent is server time
  • TimeLocal is machine time
  • iTime returns a bar open time
  • Backtests simulate TimeCurrent

Choose the clock that matches the rule

TimeCurrent reports the last known quote-server time. In OnTick it is the time of the handled tick; elsewhere it is the last server time available in Market Watch. It does not depend on the workstation clock.

TimeLocal and TimeGMT depend on the computer environment. They are useful for local operations, but mixing them with broker session rules can shift behavior when server offsets or daylight-saving policies differ.

FunctionClockTypical use
TimeCurrentTrade serverBroker session and current market time
TimeLocalComputerLocal workstation tasks
TimeGMTCalculated GMTExternal UTC comparison
iTimeSeries bar timeBar alignment

datetime is a timestamp, not a timezone label

MQL4 datetime stores elapsed seconds. TimeToStruct exposes year, month, day, hour, minute, second, and weekday fields; StructToTime converts those fields back to a timestamp.

A timestamp received from an API needs a declared timezone before comparison with TimeCurrent. The numeric value alone does not document whether a human-entered session time meant broker time, local time, or UTC.

  • Name timezone assumptions in inputs and comments
  • Use iTime for bar boundaries instead of reconstructing them from tick time
  • Use PeriodSeconds only for fixed chart-period calculations
  • Test daylight-saving transitions when local or regional sessions matter

Tester time is modeled

In the Strategy Tester, TimeCurrent is simulated from historical data. A live EA and a backtest can therefore see different arrival patterns even when timestamp comparisons are identical.

Compile checks cannot validate session calendars, holiday rules, daylight-saving changes, or missing bars. Those are data and runtime tests.

Review the complete example

The script separates server time from current-bar time, converts the server timestamp into calendar fields, and reads the chart period in seconds.

The example does not trade. Session logic still needs broker-server timezone, daylight-saving, historical-data, and restart tests.

TimeFunctionsExample.mq4.mq4
#property strict

void OnStart()
  {
   datetime server_time = TimeCurrent();
   datetime bar_time = iTime(NULL, 0, 0);
   MqlDateTime parts;

   if(!TimeToStruct(server_time, parts))
     {
      Print("TimeToStruct failed. Error ", GetLastError());
      return;
     }

   int period_seconds = PeriodSeconds(PERIOD_CURRENT);

   PrintFormat("server=%s hour=%d bar=%s periodSeconds=%d",
               TimeToString(server_time, TIME_DATE|TIME_SECONDS),
               parts.hour,
               TimeToString(bar_time, TIME_DATE|TIME_MINUTES),
               period_seconds);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Name the timezone

    Decide whether each rule uses broker server time, UTC, local time, or bar time.

  2. 2

    Read the matching clock

    Use TimeCurrent, TimeGMT, TimeLocal, or iTime according to that definition.

  3. 3

    Convert only for fields

    Use TimeToStruct when calendar parts are required and keep comparisons as datetimes.

  4. 4

    Test calendar edges

    Verify server offset, daylight-saving changes, weekends, and tester behavior.

Frequently asked questions

What timezone is TimeCurrent in MQL4?

It reports time formed on the trade server, so its timezone follows the broker server configuration.

Is TimeLocal the broker time?

No. TimeLocal uses the computer where the client terminal runs.

How do I get the current bar time?

Use iTime for shift 0 on the required symbol and timeframe.

What does PeriodSeconds return?

It returns the number of seconds in the specified standard timeframe, or zero for an invalid timeframe.

Does TimeCurrent work in the Strategy Tester?

Yes, but it is simulated from historical data during testing.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent