TimeGMT_TestEA

Download script
Author: Copyright © 2018, Amr Ali

This script is a specialized utility designed for traders who use the MetaTrader 5 platform to conduct backtesting. Its primary purpose is to solve a common technical headache: accurately identifying and calculating time differences between your computer's local time, the broker's server time, and Coordinated Universal Time (GMT).

Core Functionality

1. Timezone Synchronization The script works by comparing three different types of time data simultaneously:

  • Current Time: The actual time on your local device.
  • Trade Server Time: The time used by the broker to execute trades.
  • GMT Time: The universal standard time.

By calculating the difference between these values, the script determines the exact "Broker Offset"—essentially telling you how many hours ahead or behind your broker's server is compared to the rest of the world.

2. Dynamic Adjustments for Daylight Savings (DST) One of the most complex parts of trading is accounting for Daylight Savings Time, which shifts automatically in different regions and at different times. This script includes a specific logic feature to handle this:

  • It can use a "Gold Symbol" (XAUUSD) as a reference point.
  • Because gold and forex markets sometimes switch their clocks at different moments, this allows the user to choose which market's rules should dictate the timezone calculation, ensuring that your backtest results remain accurate even when the clocks jump forward or backward.

3. Automated Tracking for Backtesting The script is equipped with a "Timer" function. This is particularly useful for long-term strategy testing. It can be set to trigger at specific intervals (e.g., every 24 hours), allowing it to log and display the time progression automatically throughout a multi-week or multi-month test period.

User Experience & Reporting

The script provides clear, human-readable feedback in the terminal logs:

  • Time Formatting: It converts complex raw data into a clean format (e.g., "Wed, 2023.02.14 01:59") so you can easily read the results.
  • Real-time Logging: As it runs, it prints out the specific offsets for each period, making it easy to see exactly when a timezone shift occurred during your test.
  • Performance Metrics: Upon completion, it calculates the total elapsed time of the process in milliseconds, giving you a clear picture of how efficiently the data was processed.

Summary of Use Case

For a non-programmer, this tool acts like a "Time Translator." It ensures that when you are testing a trading strategy that relies on specific hours (like the London open or New York session), the results you see are based on real-world accuracy, accounting for all the hidden shifts in broker time and global standards.

5 Views
0 Downloads
0 Favorites
TimeGMT_TestEA
ÿþ//+------------------------------------------------------------------+

//|                                               TimeGMT_TestEA.mq5 |

//|                                        Copyright © 2018, Amr Ali |

//|                             https://www.mql5.com/en/users/amrali |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2018, Amr Ali"

#property link      "https://www.mql5.com/en/users/amrali"

#property version "1.63"



// Uncomment the following line to see debug messages

// #define PRINT_GMT_DETAILS

#include "TimeGMT.mqh"



input int  timer_hours      = 24;    // Timer hours for strategy tester

input bool InpUseGoldSymbol = true;  // Load XAUUSD symbol for estimation of the server's TZ/DST

//+------------------------------------------------------------------+

//| Global variables                                                 |

//+------------------------------------------------------------------+

long     sum = 0;

ulong    start = GetMicrosecondCount();

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

   Print("Server : ", AccountInfoString(ACCOUNT_SERVER));

   EventSetTimer(timer_hours * 3600);  // use timer to backtest weekends

//--- set the option of using Gold symbol for estimation of the server's TZ/DST

   CTimeGMT::SetUsingGoldSymbol(InpUseGoldSymbol);

//---

   show();

   return (INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert Timer function                                            |

//+------------------------------------------------------------------+

void OnTimer()

  {

   show();

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   EventKillTimer();

//--- Display results.

   PrintFormat("Total time elapsed: %.1f msec", (GetMicrosecondCount()-start)/1000.);

   Print("Sum_BrokerOffset: ", sum);

  }

//+------------------------------------------------------------------+

//| Formats time with the weekday name => "Wed, 2023.02.14 01:59"    |

//+------------------------------------------------------------------+

string t2s(const datetime t, const int mode = TIME_DATE | TIME_MINUTES)

  {

   const string days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

   const int i = ((int)((uint)t / 86400 + 4) % 7);  // i = DayOfWeek(t)

   return days[i % 7] + ", " + TimeToString(t, mode);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void show()

  {

   datetime tc  = TimeCurrent();

   datetime tts = TimeTradeServer();

   datetime gmt = TimeGMT();  // note, calls to TimeGMT() in tester will be fixed by the library



   int BrokerOffset = (int)(tts - gmt);



   PrintFormat("TimeCurrent() = %s  | TimeTradeServer() = %s  | TimeGMT() = %s | BrokerOffset = %d (GMT%+g)",

               t2s(tc),

               t2s(tts),

               t2s(gmt),

               BrokerOffset, BrokerOffset/3600.0);



   sum += tts * BrokerOffset;

  }

//+------------------------------------------------------------------+



//  Strategy Tester Journal:



/*

 EURUSD,M1 (ICMarketsSC-Demo): 1 minutes OHLC ticks generating

 EURUSD,M1: testing of Experts\TimeGMT_TestEA.ex5 from 2024.02.29 00:00 to 2024.03.31 00:00 started with inputs:

   timer_hours=24

   InpUseGoldSymbol=false

 2024.02.29 00:00:00   Server : ICMarketsSC-Demo

 2024.02.29 00:00:00   CTimeGMT >> Using Gold symbol option: false

 2024.02.29 00:00:00   CTimeGMT >> Loaded symbol: EURUSD,H1

 2024.02.29 00:00:00   TimeCurrent() = Thu, 2024.02.29 00:00  | TimeTradeServer() = Thu, 2024.02.29 00:00  | TimeGMT() = Wed, 2024.02.28 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.01 00:00:00   TimeCurrent() = Fri, 2024.03.01 00:00  | TimeTradeServer() = Fri, 2024.03.01 00:00  | TimeGMT() = Thu, 2024.02.29 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.02 00:00:00   TimeCurrent() = Fri, 2024.03.01 23:56  | TimeTradeServer() = Sat, 2024.03.02 00:00  | TimeGMT() = Fri, 2024.03.01 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.03 00:00:00   TimeCurrent() = Fri, 2024.03.01 23:56  | TimeTradeServer() = Sun, 2024.03.03 00:00  | TimeGMT() = Sat, 2024.03.02 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.04 00:00:00   TimeCurrent() = Fri, 2024.03.01 23:56  | TimeTradeServer() = Mon, 2024.03.04 00:00  | TimeGMT() = Sun, 2024.03.03 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.05 00:00:00   TimeCurrent() = Mon, 2024.03.04 23:58  | TimeTradeServer() = Tue, 2024.03.05 00:00  | TimeGMT() = Mon, 2024.03.04 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.06 00:00:00   TimeCurrent() = Tue, 2024.03.05 23:58  | TimeTradeServer() = Wed, 2024.03.06 00:00  | TimeGMT() = Tue, 2024.03.05 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.07 00:00:00   TimeCurrent() = Wed, 2024.03.06 23:58  | TimeTradeServer() = Thu, 2024.03.07 00:00  | TimeGMT() = Wed, 2024.03.06 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.08 00:00:00   TimeCurrent() = Fri, 2024.03.08 00:00  | TimeTradeServer() = Fri, 2024.03.08 00:00  | TimeGMT() = Thu, 2024.03.07 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.09 00:00:00   TimeCurrent() = Fri, 2024.03.08 23:56  | TimeTradeServer() = Sat, 2024.03.09 00:00  | TimeGMT() = Fri, 2024.03.08 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.10 00:00:00   TimeCurrent() = Fri, 2024.03.08 23:56  | TimeTradeServer() = Sun, 2024.03.10 00:00  | TimeGMT() = Sat, 2024.03.09 22:00 | BrokerOffset = 7200 (GMT+2)

 2024.03.11 00:00:00   TimeCurrent() = Mon, 2024.03.11 00:00  | TimeTradeServer() = Mon, 2024.03.11 00:00  | TimeGMT() = Sun, 2024.03.10 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.12 00:00:00   TimeCurrent() = Mon, 2024.03.11 23:58  | TimeTradeServer() = Tue, 2024.03.12 00:00  | TimeGMT() = Mon, 2024.03.11 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.13 00:00:00   TimeCurrent() = Tue, 2024.03.12 23:58  | TimeTradeServer() = Wed, 2024.03.13 00:00  | TimeGMT() = Tue, 2024.03.12 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.14 00:00:00   TimeCurrent() = Wed, 2024.03.13 23:58  | TimeTradeServer() = Thu, 2024.03.14 00:00  | TimeGMT() = Wed, 2024.03.13 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.15 00:00:00   TimeCurrent() = Thu, 2024.03.14 23:58  | TimeTradeServer() = Fri, 2024.03.15 00:00  | TimeGMT() = Thu, 2024.03.14 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.16 00:00:00   TimeCurrent() = Fri, 2024.03.15 23:56  | TimeTradeServer() = Sat, 2024.03.16 00:00  | TimeGMT() = Fri, 2024.03.15 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.17 00:00:00   TimeCurrent() = Fri, 2024.03.15 23:56  | TimeTradeServer() = Sun, 2024.03.17 00:00  | TimeGMT() = Sat, 2024.03.16 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.18 00:00:00   TimeCurrent() = Mon, 2024.03.18 00:00  | TimeTradeServer() = Mon, 2024.03.18 00:00  | TimeGMT() = Sun, 2024.03.17 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.19 00:00:00   TimeCurrent() = Tue, 2024.03.19 00:00  | TimeTradeServer() = Tue, 2024.03.19 00:00  | TimeGMT() = Mon, 2024.03.18 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.20 00:00:00   TimeCurrent() = Tue, 2024.03.19 23:58  | TimeTradeServer() = Wed, 2024.03.20 00:00  | TimeGMT() = Tue, 2024.03.19 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.21 00:00:00   TimeCurrent() = Thu, 2024.03.21 00:00  | TimeTradeServer() = Thu, 2024.03.21 00:00  | TimeGMT() = Wed, 2024.03.20 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.22 00:00:00   TimeCurrent() = Thu, 2024.03.21 23:58  | TimeTradeServer() = Fri, 2024.03.22 00:00  | TimeGMT() = Thu, 2024.03.21 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.23 00:00:00   TimeCurrent() = Fri, 2024.03.22 23:56  | TimeTradeServer() = Sat, 2024.03.23 00:00  | TimeGMT() = Fri, 2024.03.22 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.24 00:00:00   TimeCurrent() = Fri, 2024.03.22 23:56  | TimeTradeServer() = Sun, 2024.03.24 00:00  | TimeGMT() = Sat, 2024.03.23 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.25 00:00:00   TimeCurrent() = Mon, 2024.03.25 00:00  | TimeTradeServer() = Mon, 2024.03.25 00:00  | TimeGMT() = Sun, 2024.03.24 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.26 00:00:00   TimeCurrent() = Mon, 2024.03.25 23:58  | TimeTradeServer() = Tue, 2024.03.26 00:00  | TimeGMT() = Mon, 2024.03.25 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.27 00:00:00   TimeCurrent() = Tue, 2024.03.26 23:58  | TimeTradeServer() = Wed, 2024.03.27 00:00  | TimeGMT() = Tue, 2024.03.26 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.28 00:00:00   TimeCurrent() = Wed, 2024.03.27 23:58  | TimeTradeServer() = Thu, 2024.03.28 00:00  | TimeGMT() = Wed, 2024.03.27 21:00 | BrokerOffset = 10800 (GMT+3)

 2024.03.29 00:00:00   TimeCurrent() = Fri, 2024.03.29 00:00  | TimeTradeServer() = Fri, 2024.03.29 00:00  | TimeGMT() = Thu, 2024.03.28 21:00 | BrokerOffset = 10800 (GMT+3)

 final balance 10000.00 USD

 2024.03.29 23:56:59   Total time elapsed: 95.9 msec

 2024.03.29 23:56:59   Sum_BrokerOffset: 486475269120000



*/





// NOTE:

// If testing on EURUSD symbol: DST switches occur in the tester at the time of the first bar a week (e.g., Monday 00:00, on GMT+2 brokers)

// If testing on XAUUSD symbol: DST switches occur in the tester one hour after Forex starts (e.g., Monday 01:00 on GMT+2 brokers)

Comments