MQL4 date and time

MQL4 time functions and server-time boundaries

In a live terminal, TimeCurrent returns the last known server time, TimeLocal uses the computer clock, and TimeGMT derives GMT from that local environment. In the Strategy Tester all three use simulated server time. Use iTime for bar opening times and make external timestamp timezones explicit.

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

TimeFunctionsExample.mq4MQL4
#property strict

void OnStart()
  {
   datetime server_time = TimeCurrent();
   datetime bar_time = iTime(NULL, 0, 0);
   if(bar_time == 0)
     {
      Print("Bar history is unavailable. Error ", GetLastError());
      return;
     }
   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 in live use
  • 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.

In live use, 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. The table below describes live-terminal clocks.

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, and both TimeLocal and TimeGMT equal that simulated server time. A backtest therefore cannot discover the historical broker-to-UTC offset by subtracting those clocks. Supply an independently verified offset schedule if the rule depends on UTC.

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.mq4MQL4
#property strict

void OnStart()
  {
   datetime server_time = TimeCurrent();
   datetime bar_time = iTime(NULL, 0, 0);
   if(bar_time == 0)
     {
      Print("Bar history is unavailable. Error ", GetLastError());
      return;
     }
   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?

In a live terminal, TimeLocal uses the computer clock. In the Strategy Tester, it equals the simulated server time returned by TimeCurrent.

How do I get the current bar time?

Use iTime for shift 0 on the required symbol and timeframe. Check for zero, which can mean that local history is not loaded.

What does PeriodSeconds return?

It returns the number of seconds for the selected ENUM_TIMEFRAMES value; the default is the current chart period. Use actual bar timestamps for calendar boundaries and gaps.

Does TimeCurrent work in the Strategy Tester?

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

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