GMT and offset calculations - page 3

 
jjc:

Er, see above. Personally, I wouldn't want the job of writing MQL4 code to talk to an NTP server.

Shouldn't be very complicated if the simpler SNTP protocol is used: http://code.activestate.com/recipes/117211-simple-very-sntp-client/ is an example in python. Just send an UDP packet and then receive one. Maybe I'll try it if i have some time, just for fun. Shouldn't be more complicated than my mql4 IRC client.

 

Thanks for the response, I am not sure I got the solution I need or want, and I am not sure anything in this conversation was very helpful except for JJCs code snippet, but I can say the following:

1. As a professional programmer in a pro shop that makes commercial products around the world, the rule of thumb around here is never rely on data that comes from a local machine, where time values are concerned. So using the clients local time to find GMT is really not a realiable source. It is one that I will use if forced to be at that level, but not desired.

2. Being dependent upon someone elses website to collect date and time is also a risk. Not knowing if the developer of that value performed the calculations correctly or take the risk that the website URL might change. Too much risk.

3. The best solution will be based off of a direct data feed or source of the GMT itself. Using that as the baseline for the remaining calculations would be the ideal low risk solution.

In truth, I think it is very American arogant to put most of the servers on Eastern time zone. I am shocked to find this out as I would have expected the developers at that level would have been smart emough to base their time systems on the GMT as well. But setting my personal opinion aside, it seems that the solution to doing this will remain elussive to resolve properly.

Ah, if only I had the time (tongue-in-cheeck).

 
LEHayes:

Ah, if only I had the time (tongue-in-cheeck).

Ok, if you really want to know the absolute time - get it off the data feed from one of these

http://www.easydevices.co.uk/pp/GPS_Receivers/USB_GPS/GLOBALSAT_BU353_SIRFSTAR_III_USB_GPS_RECEIVER.html

then use a VB or .NET app to write it to a file that the EA can see
Make sure you get one that has a known format output, some Garmins do not produce NMEA standard text

I have some VB code if you're interested ;)

FWIW

-BB-

 
LEHayes:

It really frustrates me that the MQL4, does not provide for time calculations, or even to say the least, the abilityt o pinpoint a specific time based off of GMT.
Just one point of curiosity, how could so many automations exist out there that are based on time and price values and yet no one in the industry has addressed this issue. Either this is pure insanity or a really good explanation of why so many EAs fail.

Why make it easy when it can readily be made as cumbersome, difficult and awkward as possible?

Is this 'by design' to make us metal heads to go through torturous contortions to be able sharpen our mental / logic skills |OR| to really confuse those of us that don't have enough knowledge and experience to stand any hope of utilizing it effectively? Would this be to 'force us to learn it' or just to completely dumbfound us?

Devious design, complete oversight or just ineptitude?

By design to make it as difficult and challenging as possible to force serious study in order to be able to understand it thoroughly (if possible) or just outright conspiracy ?

Perhaps you should be passing these along for input for suggestions to v5 in hopes that....

Nothing ventured.......

 

simple...

   int secOffset =  (TimeCurrent() - TimeGMT()) + 10; // always offset about 2-3s , so + 10 for correct minute

   int minOffset = (secOffset/60); //get total min offset

   Print("minOffset:",minOffset,"secOffset:",secOffset);

   int off = minOffset/60; //convert to hours

   Print("offset hours: ",off);


 

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Особенности языка mql4, тонкости и приёмы работы

fxsaber, 2018.03.29 14:32

#property strict

#define HOUR 3600
#define DAY (24 * HOUR)
#define WEEK 7

datetime GetBarTime( const datetime time, const bool NextBar = false, string Symb = NULL, const ENUM_TIMEFRAMES TimeFrame = PERIOD_M1 )
{
  if (Symb == NULL)
    Symb = _Symbol;
    
  return(iTime(Symb, TimeFrame, iBarShift(Symb, TimeFrame, time) - (NextBar ? 1 : 0)));
}

datetime GetTimeDayOfWeek( const int Shift = 0, const ENUM_DAY_OF_WEEK Day = SUNDAY )
{
  const datetime Res = TimeCurrent() / DAY * DAY;
  
  return(Res - (((WEEK + (TimeDayOfWeek(Res) - Day)) % WEEK) + Shift * WEEK) * DAY);
}

// Аналог по серверному времени - https://www.mql5.com/ru/docs/dateandtime/timegmtoffset
// Работает для FOREX-символов, когда M1-история доступна за ближайшую неделю
int TimeServerGMTOffset( void )
{
  const datetime Sunday = GetTimeDayOfWeek();
  
  return(((int)MathRound((double)MathMin(Sunday - DAY - GetBarTime(Sunday), Sunday + DAY - GetBarTime(Sunday, true)) / HOUR) - 3) * HOUR);
}

// Аналог по серверному времени - https://www.mql5.com/ru/docs/dateandtime/timegmt
// Работает для FOREX-символов, когда M1-история доступна за ближайшую неделю
datetime TimeServerGMT( void )
{
  return(TimeCurrent() + TimeServerGMTOffset());
}


Применение

#define PRINT(A) Print(#A + " = " + (string)(A))

void OnStart()
{  
  PRINT(TimeGMT());
  PRINT(TimeServerGMT());  
}


Подход хорош тем, что работает не только на выходных, в Тестере и реал-тайме, но и на сторонних данных. Т.е. определяется GMT котировок, взятых не из MT.

 
JC #:

Pass on that.

Going back to the other issue, if you want to run a time service for EA users then all you need is a server with a reasonably accurate clock and web server software such as Microsoft IIS. Example ASPX code for returning a GMT timestamp would then be as follows:


You could then use this to get the offset between broker time and GMT using one of the HTTP libraries on this forum. For example, using http51.dll, you could do the following:

Thank you for your good code
Reason: