GMT and offset calculations - page 2

 
cameofx:

If it's any consolation, The developer of MQL5 fortunately address this. :)

Up to a point. The documentation strongly implies that this is returning GMT based on the local computer clock. Gordon's already expressed some reservations about this.

 
jjc:

Up to a point. The documentation strongly implies that this is returning GMT based on the local computer clock. Gordon's already expressed some reservations about this.

GMT offset and DST problems are not that easy to solve I agree. I personally haven't test or study it more thoroughly or encounter the need for it's precise calculations for that matter.

I know I've read some elaborate codes regarding this... Fwiw, here are some resources I've found.

- New very cool candle time - Forex Factory

- Search result of 'clock' from FF

On a lighter note... maybe MT5 developers are waiting for an automatic catch-all solutions from us :))))

 
cameofx:

On a lighter note... maybe MT5 developers are waiting for an automatic catch-all solutions from us :))))

Well, Forex Megadroid publishes a time signal for use by its EA at http://www.forex-megadroid.com/timetest.php. For as long as that website continues to exist, you could read their time signal using one of the HTTP libraries on this forum. Note that it's only specified to the minute; the EA presumably uses this to establish the broker's offset by rounding the difference in seconds to the nearest 30 minutes (or 15 minutes, in case the broker is based in Kathmandu).

Or you could run your own time signal from your own web server.

 
cameofx: On a lighter note... maybe MT5 developers are waiting for an automatic catch-all solutions from us :))))

This is not too light a note maybe... From the banner and slogan they apparently do waiting for our suggestions...

 
there are also public time servers accessible through the ntp protocol.
 

now hopefully Rosh or Stringo will have a say for this thread...

Jjc, what about atomic clock sync... I've read it somewhere. It suppose to be dedicated and more reliable I think...

 
7bit:
there are also public time servers accessible through the ntp protocol.

Er, see above. Personally, I wouldn't want the job of writing MQL4 code to talk to an NTP server. And the accuracy of NTP shouldn't be necessary when there's already meant to be an accurate time signal coming in from the broker. All you should need is a GMT offset, not a complete replacement GMT timestamp.

 
jjc:

Er, see above. Personally, I wouldn't want the job of writing MQL4 code to talk to an NTP server. And the accuracy of NTP shouldn't be necessary when there's already meant to be an accurate time signal coming in from the broker. All you should need is a GMT offset, not a complete replacement GMT timestamp.

I guess 7bit and I was referring to the same thing. There's also the 'more popular' request to have the chart Time axis to be offset-able by user...
Or maybe It's been done in MT5?
 
cameofx:
There's also the 'more popular' request to have the chart Time axis to be offset-able by user...

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:

<%@ Page Language="C#" %>
<script runat="server">
    void Page_Load()
    {
        System.DateTime dt1970 = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        System.DateTime dtNow = System.DateTime.Now.ToUniversalTime();
        System.TimeSpan tsResult = dtNow.Subtract(dt1970); 
        Response.Write(Math.Floor(tsResult.TotalSeconds));
        Response.End();    
    }
</script>


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:

#import "http51.dll"
   string httpGET    (string URL, int  status []);
#import

#define URL_FOR_TIMESTAMP  "http://www.whatever.com/gmt.aspx"

[...]

// Returns the number of seconds which the broker is ahead(+)/behind(-) GMT.
// For example, currently returns Alpari UK as being 7200 seconds ahead of GMT.
// If the broker is in a timezone such as India which is not a multiple of 60 minutes
// ahead/behind GMT, then AccuracyThresholdInMinutes needs to be altered accordingly.
int GetBrokerGMTOffset(int AccuracyThresholdInMinutes = 60)
{
   int httpStatus[1];
   string strResponseText = httpGET(URL_FOR_TIMESTAMP, httpStatus);
   int lGMT = StrToInteger(strResponseText);
   // Watch for obviously invalid responses from the server
   if (lGMT <= D'2010/1/1') {
      return (-9999);
   } else {
      // Get the current broker time and round the difference to the nearest n minutes
      int lBrokerTimestamp = TimeCurrent();
      double lDiff = lBrokerTimestamp - lGMT;
      lDiff = MathRound(lDiff / (AccuracyThresholdInMinutes * 60));
      lDiff *= (AccuracyThresholdInMinutes * 60);
      return (lDiff);
   }
}
 
jjc:

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:

Now that is some code ... I may not have immediate use of it but thanks for sharing this Jjc ... :)
Reason: