What is the time on the quotation charts ?

Time is moneyIn recent times, there has been a lot of discussion about AdvisorsThe Expert Advisor can also be used in the following cases: if you want to trade at a certain time of day or day of the week. For example: allow (disallow) to the Expert Advisor operations with orders linked to the end time Asian trading sessions. Linking the time of exchange sessions with the trading strategy of a robot-advisor can be a very promising "fresh jet" in the technique of programming trading robots, but... As a person who is directly involved in experiments with trading programs, I wondered beforehand: "And what kind of time do we see under the quotation curves, and what time do we tie the Expert Advisor to?". I found some of the answers to this question so curious that I decided to bring them to the attention of all my colleagues who program trading robots.

What time do traders live by?

Evening. On my computer the local time is ~22:00 (I eliminate small minutes from consideration for clarity of the picture), this Ukrainian time (GMT+2), i.e. corresponding to 20:00 GMT. I open in the terminal MT4 from the dealing center Alpari GBPUSD chart, H1For example (everything was done on demo accounts, the specific brokerage companies in the description were chosen arbitrarily, I did the same with four other companies), the last bar on the open chart (forming) was 21:00, the last completed bar (Time[1]) was 20:00.

Next, the same currency pair and time frame, but on the MetaTrader 4 terminal from InstaForex dealing center. The time of the last bar on the chart (forming) is 22:00, the last completed one is 21:00. I asked a trader I know in Skype from St. Petersburg (GMT+3) to check the time stamps: his local time is 23:19, the last bar formed in MT4 from Alpari is 21:00, i.e. it coincides with mine.

Now for the last bar on the GBPUSD H1 charts we look at the numerical values of the closing price: for Alpari it is 1.60963 (5-digit quote), for InstaTrader it is 1.6097 (4-digit quote).

fortrader.org

is exactly what the TimeCurrent() function of MQL4 gives you, and what the code line will show you: TimeToStr( TimeCurrent(), TIME_SECONDS). But this is definitely not the time by which I would like to synchronize the work of the robo-advisor.

Time is money

What can the difference in timelines given by different DCs result in practically?

This is one of the reasons why during the testing of your Expert Advisor in the MetaTrader 4 tester on the quotes from different brokerage companies you will never get exactly the same test resultsbecause the "twenty-four hours" in these DTs will start and end at different points in time.

2. For an EA that will explicitly use timestamps (for example, to allow or disallow trades by time interval), you will never be able to conduct reliable testing and optimizationbecause your test quoting histories will be tied to the fictitious time of the DC, and not to the time of the real exchange sessions.

How can the case be helped?

Once upon a time I encountered functions on the Internet (I, unfortunately, could not restore the source) that return to the MQL4 program the local time of your computer (GetWinLocalDateTime()) and its corresponding GMT time (GetWinUtcDateTime() - This time is also called UTC), exactly in the units of that dimension with which the MQL4 program operates (i.e., in the same way as the built-in TimeCurrent() function):

#import "kernel32.dll"
int SystemTimeToFileTime(int& TimeArray[], int& FileTimeArray[]);
int FileTimeToLocalFileTime(int& FileTimeArray[], int& LocalFileTimeArray[]);
void GetSystemTime(int& TimeArray[]);
#import
datetime GetWinLocalDateTime() {
double hundrednSecPerSec = 10.0 * 1000000.0;
double bit32to64 = 65536.0 * 65536.0;
double secondsBetween1601And1970 = 11644473600.0;
int TimeArray[4];
int FileTimeArray[2]; // 100nSec since 1601/01/01 UTC
int LocalFileTimeArray[2]; // 100nSec since 1601/01/01 Local
GetSystemTime( TimeArray );
SystemTimeToFileTime( TimeArray, FileTimeArray );
FileTimeToLocalFileTime( FileTimeArray, LocalFileTimeArray );
double lfLo32 = LocalFileTimeArray[0];
if( lfLo32 < 0 )
lfLo32 = bit32to64 + lfLo32;
double ticksSince1601 = LocalFileTimeArray[1] * bit32to64 + lfLo32;
double secondsSince1601 = ticksSince1601 / hundrednSecPerSec;
double secondsSince1970 = secondsSince1601 - secondsBetween1601And1970;
return (secondsSince1970);
}
datetime GetWinUtcDateTime() {
double hundrednSecPerSec = 10.0 * 1000000.0;
double bit32to64 = 65536.0 * 65536.0;
double secondsBetween1601And1970 = 11644473600.0;
int TimeArray[4];
int FileTimeArray[2]; // 100nSec since 1601/01/01 UTC
GetSystemTime( TimeArray );
SystemTimeToFileTime( TimeArray, FileTimeArray );
double lfLo32 = FileTimeArray[0];
if(lfLo32 < 0) lfLo32 = bit32to64 + lfLo32; double ticksSince1601 = FileTimeArray[1] * bit32to64 + lfLo32; double secondsSince1601 = ticksSince1601 / hundrednSecPerSec; double secondsSince1970 = secondsSince1601 - secondsBetween1601And1970; return (secondsSince1970); }

I have a script that calculates the quotes time stamps for local time. Here is a script for example that calculates time stamps of quotes in local time and GMT, regardless of time stamps of brokerage companies. It's simple and tasteful:

start() {
int serv2lock = TimeLocal() - TimeCurrent(),
serv2utc = GetWinUTCDateTime() - TimeCurrent(),
local2utc = GetWinUTCDateTime() - GetWinLocalDateTime();
Print( "bar open time (Time[0]):",
" server=", TimeToStr( Time[ 0 ], TIME_DATE | TIME_SECONDS ),
" local=", TimeToStr( Time[ 0 ] + serv2lock, TIME_DATE | TIME_SECONDS ),
"UTC=", TimeToStr( Time[ 0 ] + serv2utc, TIME_DATE | TIME_SECONDS )
);
}

And its result on MetaTrader 4 different dealing centers:

- DC Alpari:

2011.02.11 01:07:36 LocalTime2 EURUSD, H1: bar open time (Time[0]): server=2011.02.11 00:00:00 local=2011.02.11 01:00:01 UTC=2011.02.10 23:00:01

Now everything is correct, even in the date of the day jumped (local date and UTC are different).

- InstaForex DC:

2011.02.11 01:07:52 LocalTime2 GBPUSD,H1: bar open time (Time[0]): server=2011.02.11 01:00:00 local=2011.02.11 01:00:13 UTC=2011.02.10 23:00:13

Regardless of the time stamps it deigns to give us dealing centersThe last quotes now coincide (both in local time and in UTC) from different DCs.

This is much closer to the goal of binding the EA to GMT, but only if the system time on your computer is not off and your time zone is set correctly. And this does not always correspond to reality.

Using NTP or SNTP time servers

It would be even more interesting to explicitly use (from the MQL4 program) to link time an extensive worldwide network of precision time servers of NTP or SNTP protocols.

The idea would be to request the time from the exact time server, and then to this value to bind all the time stamps of the program. But how to request the time from a program using the NTP protocol, for example, is perfectly clear to me in UNIX operating systems, but how to do it in Windows I somehow can't figure out. Instead, I will just show you how to synchronize the set time of your own computer system with NTP/SNTP exact time servers in its inclusionat least how to do it in the Windows XP:

1. after loading the operating system, stop its time service with a command in the console ("execute command"): > net stop w32time

2. set the address of the NTP time server (or a list of addresses, see below):
> net time /setsntp:ntp.vc.ukrtel.net

3. you can check which NTP server you have installed on your system by default for synchronization (but this does not necessarily mean it is the best choice):
> net time /querysntp

4. After setting the server, restart the Windows XP time service:
> net start w32time

In about 25 seconds after such manipulation, your system time will "jump" and be set to the exact world time with a discrepancy of only a few thousandths of a second! You can make sure that all this works by purposely setting the wrong time in your system, and see what happens to it after such actions.

After step 2, the NTP server address (or a list of several addresses) will be remembered by the time service, and henceforth you can not set it (until you want to change it).

Where do you get the addresses of the time servers from?one of which, Ukrtelecom, as an example, I wrote down above in point 2 of the example? Here by this In the Internet you will find a good hundred official addresses of time servers, of which there are 40 Russian and 2 Ukrainian, take any of them to your liking.

How to write (for insurance) a list of several time servers in the command? In this syntax - quotation marks are required, server addresses (as many as you want) are separated by spaces:
> net time /setsntp: "ntp.vc.ukrtel.net ntp21.vniiftri.ru"

What about Windows Vista and Windows 7? In discussions on the Internet, it is claimed that the time service in Windows 7 is managed in exactly the same way. Regarding Windows Vista, I can't say anything at all.

P.S. I came across the address of WWW-page for specifying GMT time, can help a lot when working out all sorts of experiments with time: http://wwp.greenwichmeantime.com/

Leave a Reply

Back to top button