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.