Synchronise Windows local time with the MT5 server

 

Good afternoon!

Is it possible to adjust the current local time of computer from MQL5?

Will GetLocalTime() and SetLocalTime() work?

 
prostotrader:

Good afternoon!

Is it possible to adjust the current local time of computer from MQL5?

Will GetLocalTime() and SetLocalTime() work?

Checked it, it works.

//+------------------------------------------------------------------+
//|                                                     TimeTest.mq5 |
//|                                      Copyright 2016 prostotrader |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016 prostotrader"
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
struct _SYSTEMTIME {
  ushort wYear;
  ushort wMonth;
  ushort wDayOfWeek;
  ushort wDay;
  ushort wHour;
  ushort wMinute;
  ushort wSecond;
  ushort wMilliseconds;
};

_SYSTEMTIME loc_time;

#import "kernel32.dll"
void GetLocalTime(_SYSTEMTIME &sys_time);
bool SetLocalTime(_SYSTEMTIME &sys_time);
#import
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
    GetLocalTime(loc_time);
    if(loc_time.wYear > 0)
    {
      loc_time.wSecond = 55;
      if(SetLocalTime(loc_time))
      {
        if(loc_time.wYear > 0)
        {
        }
      }
    }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  }
//+------------------------------------------------------------------+
 

Now we just need to figure out how to correctly synchronize the local time

computer with MT5 server time, taking into account that TimeCurrent() is not updated at clearing and non-trading time (FORTS).

Any ideas?

 
prostotrader:

Now we just need to figure out how to correctly synchronise the local time

time of the computer with the time of MT5 server, taking into account that TimeCurrent() is not updated at clearing and non-trading time on FORTS.

Any ideas?

Generate ticks as in MT4?
Files:
zSendTick.mq4  2 kb
 
prostotrader:

Now we just need to figure out how to correctly synchronise the local time

time of the computer with the time of MT5 server, taking into account that TimeCurrent() is not updated at clearing and non-trading time on FORTS.

Any ideas?

So the update is only needed when the tick comes in. Therefore, it should be called only in OnTick. You have made too much trouble.
 
fxsaber:
So the update is needed only when the tick comes. Therefore, the call only in OnTick. They are too complicated.

Nothing is overdone...

There's a pre-market where quotes come into the stack, so from 9-50 to 10-00 you can sync the time with the server time.

Added

If you add a stanan of any symbol, then without filtering, events from all instruments will go there,

that are selected in Market Watch. Then take CopyTicks() and see the time to synchronize....

Like this.

Added

Correct me if I missed something.

//+------------------------------------------------------------------+
//|                                              Time_sync_forts.mq5 |
//|                                      Copyright 2017 prostotrader |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017 prostotrader"
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
struct _SYSTEMTIME
  {
   ushort            wYear;
   ushort            wMonth;
   ushort            wDayOfWeek;
   ushort            wDay;
   ushort            wHour;
   ushort            wMinute;
   ushort            wSecond;
   ushort            wMilliseconds;
  };

_SYSTEMTIME loc_time;

#import "kernel32.dll"
void GetLocalTime(_SYSTEMTIME &sys_time);
bool SetLocalTime(_SYSTEMTIME &sys_time);
#import
//---
bool is_sync;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   is_sync=false;
   MarketBookAdd(Symbol());
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   MarketBookRelease(Symbol());
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnBookEvent(const string &symbol)
  {
   loc_time.wYear=0;
   GetLocalTime(loc_time);
   if(loc_time.wYear>0)
     {
      if((loc_time.wHour==9) && (loc_time.wMinute>=50) && (loc_time.wMinute<=59))
        {
         MqlTick curr_tick[1];
         if(CopyTicks(symbol,curr_tick,COPY_TICKS_ALL,0,1)==1)
           {
            MqlDateTime sv_time;
            TimeToStruct(curr_tick[0].time,sv_time);
            if(!is_sync)
              {
               loc_time.wHour=ushort(sv_time.hour);
               loc_time.wMinute = ushort(sv_time.min);
               loc_time.wSecond = ushort(sv_time.sec);
               loc_time.wMilliseconds=ushort(ulong(curr_tick[0].time_msc)-ulong(curr_tick[0].time)*1000);
               if(SetLocalTime(loc_time))
                {
                 is_sync=true;
                 Print("Local time sync is done.");
                }
              }
           }
        }
      else is_sync=false;
     }
  }
//+------------------------------------------------------------------+
 

Just in case, check the quote for "freshness"

              if(!is_sync)
              {
                if((loc_time.wDayOfWeek == ushort(sv_time.day_of_week)) &&
                   (loc_time.wHour == ushort(sv_time.hour)))
                {
                   loc_time.wMinute = ushort(sv_time.min);
                 loc_time.wSecond = ushort(sv_time.sec);
                 loc_time.wMilliseconds=ushort(ulong(curr_tick[0].time_msc)-ulong(curr_tick[0].time)*1000);
                 if(SetLocalTime(loc_time))
                 {
                  is_sync=true;
                  Print("Local time sync is done.");
                 }
                }
              }
 
prostotrader:

Now we just need to figure out how to correctly synchronise the local time

computer with MT5 server time, taking into account that TimeCurrent() is not updated at clearing and non-trading time (FORTS).

Any ideas?

Maybe it's easier to synchronise with the source? Any event occurring on the exchange gets its time stamp. The time on the exchange itself is synchronised with the astronomical time with a margin of error of milliseconds. Whereas the time of arrival of notifications to the MT5 server (which is at the broker's) is already different, the time of sending a packet to the terminal is third, the time of arrival to the terminal is fourth. If you synchronize the system time with the astronomical time on the local computer, it will always work and the local time will coincide with the time on the exchanges. There are several hundreds of public (free) NTP servers for exact time. These are alive and working now, for example:

195.234.155.123;ntp.dvconsulting.dk
85.17.207.62;ntp.edge.tillo.ch
195.34.89.227;ntp.favey.ch
195.234.155.124;ntp.gal.dk
193.226.65.36;ntp.idsi.md

 
To CopyTicks[0] add a ping.
 
Vladimir:

Wouldn't it be easier to synchronise with the original source? Any event occurring on an exchange gets its time stamp. The time on the exchange itself is synchronised with the astronomical time with a margin of error of milliseconds. Whereas the time of arrival of notifications to the MT5 server (which is at the broker's) is already different, the time of sending a packet to the terminal is third, the time of arrival to the terminal is fourth. If you synchronize the system time with the astronomical time on the local computer, it will always work and the local time will coincide with the time on the exchanges. There are several hundreds of public (free) NTP servers for exact time. These are alive and working now, for example:

195.234.155.123;ntp.dvconsulting.dk
85.17.207.62;ntp.edge.tillo.ch
195.34.89.227;ntp.favey.ch
195.234.155.124;ntp.gal.dk
193.226.65.36;ntp.idsi.md

Time, which is taken from CopyTicks() this is exchange time, it is clear that there are delays in delivery

of the packet to my computer, but it's quite small 4-6ms.

The exact time will not give anything, because Exchange time may not coincide with it, and we torrent exactly at Exchange time

and from MT5.

 
fxsaber:
To CopyTicks[0] add a ping.
It is insignificant (milliseconds) and floats, I don't think it will make "weather".
Reason: