Unisciti alla nostra fan page
TimeGMT library for the strategy tester - libreria per MetaTrader 5
- Visualizzazioni:
- 2661
- Valutazioni:
- Pubblicato:
- 2024.02.22 01:54
- Aggiornato:
- 2024.07.07 11:59
- Hai bisogno di un robot o indicatore basato su questo codice? Ordinalo su Freelance Vai a Freelance
TimeGMT() Function
Returns the GMT, which is calculated taking into account the DST switch by the local time on the computer where the client terminal is running.
datetime TimeGMT();
Conversion of Session Hours to the Broker’s Time
Traders can use TimeGMT() to convert session times to the corresponding server time to determine when specific markets will open or close. For instance, if a trader wants to trade in the Asian (Tokyo) session, they must adjust their trading times, accordingly
1] Determine the offset of the Broker
int BrokerOffset = TimeTradeServer() - TimeGMT();
2] Convert local times of specific markets to the corresponding broker time
datetime Broker_time = Tokyo_time – Tokyo_offset + BrokerOffset
The second term in the equation (xxx_offset) should be increased by +1, if the destination time zone observes a daylight-saving time.
However, during testing in the strategy tester, TimeGMT() is always equal to TimeTradeServer() simulated server time, therefore the conversion of session times becomes wrong.
TimeGMT Library
This library will fix the TimeGMT() function in order to provide the true GMT during testing in the strategy tester. The library will install global API hooks (via macro substitution) for the two versions of TimeGMT.
class CTimeGMT { public: static datetime TimeGMT(void); static datetime TimeGMT(MqlDateTime &dt_struct);
Usage:
Before testing an expert advisor that uses News filter or any time restriction in strategy tester, just include this line at the beginning of code, then recompile the source in MetaEditor.
#include "TimeGMT.mqh"
- to see debug messages add this line before the #include directive:
#define _DEBUG
No other modifications or any function calls are required for the expert advisor or indicator.
- TimeGMT is fixed only if the strategy tester is detected.
- The fix works on the symbol selected in the strategy tester.
- No calculation errors during the weekends or Christmas holidays.
- Low overhead and fast calculation time in the strategy tester
The recalculation of broker offset takes place only at the start of the expert advisor and every Sunday and Monday midnight. The calculated broker offset will be cached in memory and it will be used for subsequent calls to TimeGMT.
TimeGMT_TestEA
To explore the library in the strategy tester, a demo expert advisor is tested in the strategy tester
these are the results of running the expert advisor in the strategy tester:
AUDUSD,M1 (ICMarketsSC-Demo): 1 minutes OHLC ticks generating AUDUSD,M1: testing of Experts\TimeGMT_TestEA.ex5 from 2023.03.05 00:00 to 2023.05.01 00:00 started with inputs: timer_hours=24 2023.03.05 00:00:00 TimeCurrent() = Sun, 2023.03.05 00:00:00 | TimeTradeServer() = Sun, 2023.03.05 00:00:00 | TimeGMT() = Sat, 2023.03.04 22:00:00 | BrokerOffset = 7200 (GMT+2) 2023.03.06 00:00:00 TimeCurrent() = Mon, 2023.03.06 00:00:00 | TimeTradeServer() = Mon, 2023.03.06 00:00:00 | TimeGMT() = Sun, 2023.03.05 22:00:00 | BrokerOffset = 7200 (GMT+2) 2023.03.07 00:00:00 TimeCurrent() = Tue, 2023.03.07 00:00:00 | TimeTradeServer() = Tue, 2023.03.07 00:00:00 | TimeGMT() = Mon, 2023.03.06 22:00:00 | BrokerOffset = 7200 (GMT+2) 2023.03.08 00:00:00 TimeCurrent() = Wed, 2023.03.08 00:00:00 | TimeTradeServer() = Wed, 2023.03.08 00:00:00 | TimeGMT() = Tue, 2023.03.07 22:00:00 | BrokerOffset = 7200 (GMT+2) 2023.03.09 00:00:00 TimeCurrent() = Thu, 2023.03.09 00:00:00 | TimeTradeServer() = Thu, 2023.03.09 00:00:00 | TimeGMT() = Wed, 2023.03.08 22:00:00 | BrokerOffset = 7200 (GMT+2) 2023.03.10 00:00:00 TimeCurrent() = Fri, 2023.03.10 00:00:00 | TimeTradeServer() = Fri, 2023.03.10 00:00:00 | TimeGMT() = Thu, 2023.03.09 22:00:00 | BrokerOffset = 7200 (GMT+2) 2023.03.11 00:00:00 TimeCurrent() = Fri, 2023.03.10 23:56:59 | TimeTradeServer() = Sat, 2023.03.11 00:00:00 | TimeGMT() = Fri, 2023.03.10 22:00:00 | BrokerOffset = 7200 (GMT+2) 2023.03.12 00:00:00 TimeCurrent() = Fri, 2023.03.10 23:56:59 | TimeTradeServer() = Sun, 2023.03.12 00:00:00 | TimeGMT() = Sat, 2023.03.11 22:00:00 | BrokerOffset = 7200 (GMT+2) 2023.03.13 00:00:00 TimeCurrent() = Mon, 2023.03.13 00:00:00 | TimeTradeServer() = Mon, 2023.03.13 00:00:00 | TimeGMT() = Sun, 2023.03.12 21:00:00 | BrokerOffset = 10800 (GMT+3) 2023.03.14 00:00:00 TimeCurrent() = Tue, 2023.03.14 00:00:00 | TimeTradeServer() = Tue, 2023.03.14 00:00:00 | TimeGMT() = Mon, 2023.03.13 21:00:00 | BrokerOffset = 10800 (GMT+3) 2023.03.15 00:00:00 TimeCurrent() = Wed, 2023.03.15 00:00:00 | TimeTradeServer() = Wed, 2023.03.15 00:00:00 | TimeGMT() = Tue, 2023.03.14 21:00:00 | BrokerOffset = 10800 (GMT+3) 2023.03.16 00:00:00 TimeCurrent() = Thu, 2023.03.16 00:00:00 | TimeTradeServer() = Thu, 2023.03.16 00:00:00 | TimeGMT() = Wed, 2023.03.15 21:00:00 | BrokerOffset = 10800 (GMT+3) 2023.03.17 00:00:00 TimeCurrent() = Fri, 2023.03.17 00:00:00 | TimeTradeServer() = Fri, 2023.03.17 00:00:00 | TimeGMT() = Thu, 2023.03.16 21:00:00 | BrokerOffset = 10800 (GMT+3) 2023.03.18 00:00:00 TimeCurrent() = Fri, 2023.03.17 23:56:59 | TimeTradeServer() = Sat, 2023.03.18 00:00:00 | TimeGMT() = Fri, 2023.03.17 21:00:00 | BrokerOffset = 10800 (GMT+3) 2023.03.19 00:00:00 TimeCurrent() = Fri, 2023.03.17 23:56:59 | TimeTradeServer() = Sun, 2023.03.19 00:00:00 | TimeGMT() = Sat, 2023.03.18 21:00:00 | BrokerOffset = 10800 (GMT+3)
Broker’s Time Zone and DST Schedule
Greenwich Mean Time (GMT), also called Coordinated Universal Time (UTC) is used as the official world reference for time. You will often see time zones represented like UTC - 3h or GMT - 3h. In this example the (-3h) refers to that time zone being three hours behind UTC or GMT. UTC+3h or GMT +3h would refer to that time zone being three hours ahead of UTC of GMT. The difference in time between our local time and GMT (UTC) is called GMT offset.
The Forex market opens Sunday at 17:00 New York time (GMT-5:00 in winter and GMT-4:00 in summer) and closes Friday at the same time. The Forex market starting time at NY Sun, 17:00 corresponds to Sun, 10:00 PM UTC in winter (and Sun, 09:00 PM UTC in summer). Forex closes on Fri, 10:00 PM UTC in winter (and 09:00 PM UTC in summer). Gold and silver spot market usually starts an hour later. link
Each forex broker has its time zone and server time. Therefore, the start of the trading week (H1 candles) is variable between brokers, and it can vary from Sun, 02:00 PM server time for brokers in San Francisco (GMT-8), up to Mon, 09:00 AM server time for brokers in Sydney (GMT+11). The end of trading week also varies from Fri, 02:00 PM server time, up to Sat, 09:00 AM server time.
Each broker is free to choose their Daylight-saving time (DST). And DST is not necessarily the same for that time zone. Sometimes they mix it using an EU time zone and a US DST instead of the EU DST. For brokers that do not follow the US schedule, the server time for the start (and end) of week varies by +/- one hour for the same broker throughout the year. To handle these variations, the recalculation of broker offset has to be done on a weekly basis, so that DST changes on the broker can be detected on time.
Using a time zone of +2 (and +3 in summer on the US schedule) therefore means that candles each week start at midnight Monday, and there are five D1 candles per week and the start of a daily candle (and H4) is the start of a new FX day. Each week ends just before midnight Saturday. There are no candles on Saturday and Sunday. Simply, time on these servers is always 7 hours ahead of New York and represented as NY+7. By far this is the most common setting, but there's a lot of less common variations.
Calculation of Broker Offset
When a call to TimeGMT() is intercepted by the library, it first checks if it is not running in the strategy tester, then it returns the original function. If the strategy tester is detected, the code starts to calculate the broker offset (see later), then the return value is simply the result of subtracting this offset from the current time of the trading server in the client terminal.
TimeGMT() = TimeTradeServer() - BrokerOffset
The 'BrokerOffset' itself can calculated as the difference between two known times:
1) the time at which the first bar of the trading week appears on the GOLD chart (FstBarWk),
2) the corresponding UTC time for GOLD trading starting time in New York at Sun, 18:00.
BrokerOffset = FstBarWk - UTC (NY Sun, 18:00)
The calculation of BrokerOffset utilizes the same algorithm that was published by Carl Schreiber here in Dealing with time (2) functions
According to https://www.timeanddate.com/time/change/usa/new-york
- In New York, DST begins (summertime) at 02:00 am local time on the second Sunday of March. The clock will move forward by one hour to GMT-4, thus the corresponding UTC time for NY Sun, 18:00 is (Sun, 22:00 UTC).
- As, the DST ends (wintertime) in New York at 02:00 local time on the first Sunday of November, the clock will move backward by one hour to GMT-5, thus the corresponding UTC time for NY Sun, 18:00 is (Sun, 23:00 UTC).
For example, if the FstBarWk on the GOLD chart starts at Mon, 01:00, and it is winter in New York at that bar, then BrokerOffset can be calculated as:
BrokerOffset = (FstBarWK, Mon, 01:00) – (Sun, 23:00 UTC) = 2 hours (GMT+2)
and, if it is summer in New York, then BrokerOffset:
BrokerOffset = (FstBarWK, Mon, 01:00) – (Sun, 22:00 UTC) = 3 hours (GMT+3)
This is a script to export rates and ticks of current chart's symbol into CSV-files compatible with MT5's export/import format.
KA-Gold Bot MT5KA-Gold Bot is an advanced trading advisor specifically designed for gold, utilizing the powerful combination of the Keltner channel strategy and two exponential moving averages (EMAs) - the 10-period EMA and the 200-period EMA. Principle of Operation: The 10-period EMA represents the average price cutting above/below the Keltner band, confirming an uptrend/downtrend. The price being above the 200-period EMA supports the uptrend/downtrend. This indicates that the uptrend/downtrend has been stronger than the previous 10 periods, considering the volatility over the last 50 periods. - Time frame: M15
The source codes written during the development of the library for creating multi-currency Expert Advisors that combine many instances of various trading strategies.
Class For Working With Databases In A Simplified Mannereasydatabase