Download MetaTrader 5
To add comments, please log in or register
Have you developed a trading robot? Publish it in the Market and make profit!
JC
1399
JC 2016.10.07 15:42  
jjc:
It should work with W1.

Er, no. Thinking about it, it will round to a Thu-Wed week, not Sun-Sat. The following should work on all periods:

datetime RoundTime(datetime tm) {
   switch (Timeframe) {
      case PERIOD_MN1:
         return tm - (tm % 86400) - ((TimeDay(tm) - 1) * 86400);
      case PERIOD_W1:
         return tm - (tm % 86400) - (TimeDayOfWeek(tm) * 86400);
      default:
         return tm - (tm % (Timeframe * 60));
   }
}
JC
1399
JC 2016.10.07 16:28  
jamescater:

This works well for the low /medium timeframes, however I'm not sure it will map correctly to the Weekly and Monthly boundaries

v3 attached, changing the RoundTime() function as above, and adding another couple of improvements (described in notes in the code's header).

 

jamescater:

I also profiled it and the overhead is smaller than I expected.

It's faster than I was expecting. For example, the performance of its internal MTF_iBarShift seems extremely close to that of MT4's own iBarShift - around half a microsecond per call, on my computer.

I had wondered whether storing the bar table in a hashtable might be a better approach, but I don't believe that anything written in and reliant on MQL4 is going to match the speed which it gets from being able to use the platform's own native-code ArrayBsearch. 

/ /123
To add comments, please log in or register