Results 1 to 7 of 7
 0 Attachment(s)    

Thread: Automatic GMT offset

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    6

    Default Automatic GMT offset

    Hello and thanks in advance!

    Can anyone paste code for determining automatically the brokers / servers GMT offset?

    thanks

  2. #2

    Default

    Quote Originally Posted by lforet3252 View Post
    Hello and thanks in advance!

    Can anyone paste code for determining automatically the brokers / servers GMT offset?

    thanks
    We are doing this through a DLL which collects the UTC from our web server, compares it to the broker server time and writes the offset to a file for later retrieval (eg strategy testing when the market is closed and broker server time is old). It also needs to check each day in case any DST changes have been applied since the last check.

    It uses about 1,000 lines of C++ code.

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    6

    Default Thanks for the reply!

    What about using kernel32.dll... it has the offset from local PC to GMT.. then compare with server's time... to get offset from server to GMT..??

    What do you see as the downside to this?

    thanks again..

  4. #4

    Default

    The problem is not in obtaining the UTC. That is the easy part and there are several ways of doing this which will all work.

    The problems are instead related to handling all of the different scenarios which can occur and how they are checked and corrected for errors.

    For instance, you need to think about the time comparison when the broker's server is not updating at the weekend and how to store and recall a previously saved offset.

    You also need to think about what will happen if the user switches broker and account within the same instance of MT4 and how the different offset for the different broker would be saved and called for re-use in that situation. You also need to think about what will happen if the client-side PC time is incorrect and how this problem would be addressed.

    Finally, if you are fetching a UTC from a distant time server, you need to think about how to handle any error messages returned by the distant server to stop them crashing your EA.

  5. #5

  6. #6
    Junior Member
    Join Date
    Aug 2009
    Posts
    14

    Default

    PHP Code:
    //========================================================================
    //=========================   Timezone Functions   =======================
    //========================================================================

    #property copyright "Derk Wehler"
    #property link      "derkwehler@gmail.com"

    #import "kernel32.dll"
    int  GetTimeZoneInformation(intTZInfoArray[]);
    #import

    #define TIME_ZONE_ID_UNKNOWN   0
    #define TIME_ZONE_ID_STANDARD  1
    #define TIME_ZONE_ID_DAYLIGHT  2

    bool IsDST()    // DST is the one over the winter
    {
        
    int TZInfoArray[43];

        switch(
    GetTimeZoneInformation(TZInfoArray))
        {
        case 
    TIME_ZONE_ID_UNKNOWN
            Print(
    "WARNING: TIMEZONE ID UNKNOWN, Cloq may give wrong time!  Returning FALSE");
            return(
    false);

        case 
    TIME_ZONE_ID_STANDARD:
            Print(
    "TZ ---> STANDARD");
            return(
    false);
        
        case 
    TIME_ZONE_ID_DAYLIGHT:
            Print(
    "TZ ---> DAYLIGHT SAVINGS");
            return(
    true);
            
        default:
            Print(
    "Unkown return value from GetTimeZoneInformation in kernel32.dll. Returning FALSE");
            return(
    false);
        }
    }


    // Local timezone in hours, adjusting for daylight saving
    double TimeZoneLocal()
    {
        
    int TZInfoArray[43];

        switch(
    GetTimeZoneInformation(TZInfoArray))
        {
        case 
    TIME_ZONE_ID_UNKNOWN
            Print(
    "Error obtaining PC timezone from GetTimeZoneInformation in kernel32.dll. Returning 0");
            return(
    0);

        case 
    TIME_ZONE_ID_STANDARD:
    //        Print("PC timezone is STANDARD");
            
    return(TZInfoArray[0]/(-60.0));
        
        case 
    TIME_ZONE_ID_DAYLIGHT:
    //        Print("PC timezone is DAYLIGHT");
            
    return((TZInfoArray[0]+TZInfoArray[42])/(-60.0));
            
        default:
            Print(
    "Unkown return value from GetTimeZoneInformation in kernel32.dll. Returning 0");
            return(
    0);
        }
    }

    // Server timezone in hours
    double TimeZoneServer()
    {
        
    int ServerToLocalDiffMinutes = (TimeCurrent()-TimeLocal())/60;
        
        
    // round to nearest 30 minutes to allow for inaccurate PC clock
        
    int nHalfHourDiff MathRound(ServerToLocalDiffMinutes/30.0);
        
    ServerToLocalDiffMinutes nHalfHourDiff*30;
        return(
    TimeZoneLocal() + ServerToLocalDiffMinutes/60.0);
    }

    // Uses local PC time, local PC timezone, and server time to calculate GMT time at arrival of last tick
    datetime TimeGMT()
    {
        
    // two ways of calculating
        // 1. From PC time, which may not be accurate
        // 2. From server time. Most accurate except when server is down on weekend
        
    datetime dtGmtFromLocal TimeLocal() - TimeZoneLocal()*3600;
        
    datetime dtGmtFromServer TimeCurrent() - TimeZoneServer()*3600;

        Print(
    "TimeZoneLocal returned: "TimeZoneLocal());
        
    // return local-derived value if server value is out by more than 5 minutes, eg during weekend
        
    if (dtGmtFromLocal dtGmtFromServer 300)
        {
            return(
    dtGmtFromLocal);
        }
        else
        {
            return(
    dtGmtFromServer);
        }    
    }

    //=================================================================
    //==================   END IMPORTED FUNCTIONS  ====================
    //================================================================= 
    From this indicator code

  7. #7
    Junior Member
    Join Date
    Aug 2009
    Posts
    6

    Default

    To all: Thank you so much for all the help! i am so glad i found this forum.. I will post my solution in a bit... hve to go to a meeting now...

    Thanks!

Similar Threads

  1. GMT Offset in EA settings - please advise
    By speedbird1229 in forum Trading discussion
    Replies: 2
    Last Post: 05-30-2011, 05:05
  2. Semi Automatic PA EA
    By blackdog in forum Ideas for expert advisors
    Replies: 7
    Last Post: 05-24-2010, 14:13
  3. How do i add an automatic lot size changer to my EA?
    By paulme33 in forum Ideas for expert advisors
    Replies: 3
    Last Post: 01-20-2010, 21:15
  4. Automatic Forex Crusher
    By johnboy411 in forum Commercial expert advisors
    Replies: 5
    Last Post: 09-10-2009, 15:57
  5. EA (but not automatic)
    By inkuumba in forum Ideas for expert advisors
    Replies: 0
    Last Post: 03-07-2009, 17:06

Tags for this Thread

automatic, broker, brokers, comparison, how to, indicator, mq4, mt4, server, stop, strategy, thank you, time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •