I just need some help with the logic to this problem I seem to be having, and perhaps it is because I am thinking of this in the incorrect manner?
So, for finding the time of the beginning of the week from any given day within that week, I have simply used:
Is this correct?
And then when it comes to finding the beginning of the month I am struggling. I am thinking of doing this but not sure how it would work:
But that would include weekends?
Not sure how I would be able to get the integer from the current Day() to the first day of the month so I can use Time without including weekends.
Any help is appreciated.
Thanks! I was wondering whether that returned the start of the candle or the current time of that candle which would just be TimeCurrent().
I will use this now, thank you.
Another approach, avoiding the use of timeseries functions, and rounding any date rather than just the current date. Adapted from https://www.mql5.com/en/forum/160249/page3#comment_3818093:
In other words: for periods below W1, it's just a question of rounding down to the multiple of the number of seconds, such as 3600 (for H1).
This doesn't work with W1 because dates start from 1/1/1970 which was a Thursday. Simple rounding therefore creates a Thu-Wed week. The "259200" in the above code shifts things so that it rounds to a Sun-Sat week. An entirely different approach is required for MN1; there is no simple rounding because months are of different lengths. Therefore, the code rounds to the start of the current day and then subtracts the number of seconds for each prior day in the month so far.
That, of course, also works.
But, for people concerned with squeezing every last second out of some pointless mass-optimization backtest, bouncing a date in and out of MqlDateTime via TimeToStruct() and then StructToTime() is orders of magnitude slower than what I'm proposing. No significant difference for any scenario outside of backtesting.
D1, // PERIOD_D1
W1, // PERIOD_W1
MN1, // PEIROD_MN1
Y1, // PERIOD_Y1
datetime GetCriteriaTime(datetime t, ENUM_CRITERIA_TYPE TYPE)
time = t - (t % 86400) - ((TimeDayOfYear(t)-1) * 86400);
time=t -(t%86400) -((TimeDay(t)-1)*86400);
return t - (t % 86400) - (TimeDayOfWeek(t) * 86400);
return t - (t % 86400);
return t - (t % (PeriodSeconds(Period())));
This function is able to find first "tradable" datetime in each timeframe.
Hopefully it is useful
datetime wkstart =iTime(NULL,PERIOD_W1,0);
Brokers use a variety of timezones. Their local time (with or without DST,) GMT/UTC, GMT+2, NY+7.
Only with NY+7 does the broker's 00:00 equals 5pm ET and the start of a daily bar (and H4) is the start of a new
GMT brokers, means there is a 1 or 2 hour D1/H4 bar on Sunday (depending on
NY DST,) and a short Friday bar.
GMT+2 is close but doesn't adjust for NY DST.
EET is closer except when their DST
doesn't match NY's. Last
Sunday of March and 1:00 on the last Sunday of October vs second Sunday in
March and return at 2:00 a.m. EDT to 1:00 a.m. EST on the first Sunday
Non-NY+7, means the chart daily
bar overlaps the start, and converting broker time to NY time requires broker to GMT to NY timezone conversions.