Some of my backtests have been hampered by time conversion issues. I decided to draw up an include file that manages converting to/from times in various locales so I can easily check 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 US, depending on year.
I'm uploading the file because it's nice to share and I'm hoping for a bit of help from fellow programmers with the QA.
I would advise against calling ConvertDateTime(...) with every timestamp that needs converting since it could be processor intensive. I call it on the first bar of every Sunday to get the time offset and then reuse the offset for the rest of the week.
Here's an example:
and then you can make use of the offset with impunity...
As is, the constant settings work for NorthFinance. You'll probably have to add the offset for your broker in the constants and in the GmtOffset(...) function.
I can't upload an mqh, so I've attached it as txt.
I'm uploading the file because it's nice to share and I'm hoping for a bit of help from fellow programmers with the QA.
I would advise against calling ConvertDateTime(...) with every timestamp that needs converting since it could be processor intensive. I call it on the first bar of every Sunday to get the time offset and then reuse the offset for the rest of the week.
Here's an example:
Inserted Code
// if this is the first bar or a new week calculate eastern time offset if (i == Bars - 1 || (TimeDayOfWeek(Time[i]) < TimeDayOfWeek(Time[i+1]))) iEasternOffset = ConvertDateTime(Time[i],TIME_SERVER,TIME_NEWYORK) - Time[i];
Inserted Code
datetime dtEasternTime = Time[i] + iEasternOffset;
As is, the constant settings work for NorthFinance. You'll probably have to add the offset for your broker in the constants and in the GmtOffset(...) function.
I can't upload an mqh, so I've attached it as txt.
Attached File(s)
timefuncs.txt
3 KB
|
380 downloads