Time Conversion Functions - Includes Daylight Savings

thread: Time Conversion Functions - Includes Daylight Savings

  1. #1
    Junior Member rodrimolok's Avatar
    6

    Time Conversion Functions - Includes Daylight Savings

    Some of my backtests are hampered by time conversion problems. I chose to draw an include file which manages converting to/from times in a variety of locales so I can easily assess the London time, New York time, etc.. I believe it accounts for daylight savings time accurately, even taking into account the old or new rules for DST in the united states, depending on year.

    I am uploading the file since it's wonderful to talk about and I am hoping for a bit of help from fellow developers with the QA.

    I'd advise against calling ConvertDateTime(...) with every timestamp that requires converting since it could be processor intensive. I call it around the very first bar of every Sunday to find the time offset and then reuse the offset for the remainder of the week.

    Here is an illustration:
    Inserted Code // when that is the first bar or a new week compute mid time offset if (I == Bars - 1 || (TimeDayOfWeek(Time#91;I#93;-RRB- lt; TimeDayOfWeek(Time#91;I 1#93;-RRB-)) iEasternOffset = ConvertDateTime(Period #91;I#93;,TIME_SERVER,TIME_NEWYORK) - Time#91;I#93;; and then it is possible to use the offset with impunity...
    Inserted Code datetime dtEasternTime = Time#91;I#93; iEasternOffset;
    As is, the constant settings operate for NorthFinance. You will probably have to bring the offset to your broker in the constants and in the GmtOffset(...) function.

    I can not upload an mqh, so I've attached it as txt.
    https://www.cliqforex.com/attachment...1970386558.txt

  2. #2
    Member ina99's Avatar
    98
    Thank you for this, I can make decent use of it...

  3. #3
    Well done, could come in handy.
    Here are a few suggestions: First, how about including a timezone for southern europe.
    Also it'd be nice to automatically detect the server timezone. Imho, this should really be a standard purpose of mql, but I have seen code calling windows dlls to accomplish this. I'll look into it, and see if it's sensible.

  4. #4
    Junior Member rodrimolok's Avatar
    6
    Additionally it'd be nice to automatically discover the server timezone. Imho, this should really be a typical purpose of mql, but I have seen code calling windows dlls to accomplish this. I will look into it, and see whether it is sensible.
    Yup, this can be done but it frees the ability to backtest accurately which I believe is among the biggest advantage of the code.

  5. #5
    Therefore it was mostly for backtesting, then.

    I have added some code supplying very flawless internet integration:
    When you place TIME_SERVER and TIME_LOCAL to unique IDs, then the GmtOffset() for these areas will get calculated automatically (just works reside ).
    If you place them to one of the predefined constants, GmtOffset() will yield the offline values.

    Inserted Code #define TIME_GMT 0 #define TIME_LONDON 1 #define TIME_NEWYORK 2 #define TIME_TOKYO 3 #define TIME_CYPRUS 4 #define TIME_CHICAGO 5 #define TIME_FRANKFURT 6 // Offline Settings (Backtesting) #define TIME_SERVER TIME_FRANKFURT #define TIME_LOCAL TIME_FRANKFURT /* // Online Settings #define TIME_SERVER 7 #define TIME_LOCAL 8 */ You can also use another functions, such as TimeGMT() when reside.
    https://www.cliqforex.com/attachment...2102258898.txt

  •