Improve your time execution by removing Bars function calls

 

Hey guys.

Today I am going to share a simple function used to get a daily unique value in function of a symbol and a timeFrame.

This is useful when you want to know if you got a new bar on your chart, this is useful to avoid useless computation.

You can basically use the Bars function like that :

static int prev_bars = -1;
int bars;

bars = Bars(Symbol(), Period());
if (bars == prev_bars) // no need to update
  return ;

prev_bars = bars;

// your code

 The Bars function isn't really efficient, that's why I developed my own function :

int  decomposeTimeframe(string symbol,ENUM_TIMEFRAMES t,int precedentValue)
  {
   int               seconds=PeriodSeconds(t);
   datetime          barTime[1];
   datetime          realTime=TimeCurrent();
   MqlDateTime       timeStruct;
   int               secondsOfTheDay;

   TimeToStruct(realTime,timeStruct);
   secondsOfTheDay=(timeStruct.sec)+(timeStruct.min*60)+(timeStruct.hour*60*60);
   if(secondsOfTheDay/seconds!=precedentValue && precedentValue != -1)
     {
      CopyTime(symbol,t,0,1,barTime);
      TimeToStruct(barTime[0],timeStruct);
      secondsOfTheDay=(timeStruct.sec)+(timeStruct.min*60)+(timeStruct.hour*60*60);
     }

   return secondsOfTheDay/seconds;
}

The way to use this function is the same, there is just an additional parameter to avoid useless call to CopyTime function (TimeCurrent call is just here to get an approximation of the result, you can't be sure to get a new bar with it, but it's really more efficient than CopyTime).

static int prev_bars = -1;

int bars;

bars = decomposeTimeFrame(Symbol(), Period(), prev_bars);
if (bars == prev_bars) // no need to update
  return ;

prev_bars = bars;

// your code

In some cases you may not have a precedent value (first call for example), so just pass -1 as the 3rd parameter.

After a basic profiling I get the following results :

62000 calls to Bars = 630 000 time units.

62000 calls to decomposeTimeFrame = 46 000 time units.

This function gives you unique results per days for every time frame between M1 to H12. 

 
wizmoisa:

Hey guys.

Today I am going to share a simple function used to get a daily unique value in function of a symbol and a timeFrame.

This is useful when you want to know if you got a new bar on your chart, this is useful to avoid useless computation.

You can basically use the Bars function like that :

 The Bars function isn't really efficient, that's why I developed my own function :

The way to use this function is the same, there is just an additional parameter to avoid useless call to CopyTime function (TimeCurrent call is just here to get an approximation of the result, you can't be sure to get a new bar with it, but it's really more efficient than CopyTime).

In some cases you may not have a precedent value (first call for example), so just pass -1 as the 3rd parameter.

After a basic profiling I get the following results :

62000 calls to Bars = 630 000 time units.

62000 calls to decomposeTimeFrame = 46 000 time units.

This function gives you unique results per days for every time frame between M1 to H12. 

Very good job and good idea to check second before using CopyTime. Thank you for sharing.

Please note though that using Bars() isn't reliable to check new bar. As the bar count on a chart is limited by setting, once this limit is reached you don't get a new bar. Only reliable way it to work with time in all case.

 
angevoyageur:

Very good job and good idea to check second before using CopyTime. Thank you for sharing.

Please note though that using Bars() isn't reliable to check new bar. As the bar count on a chart is limited by setting, once this limit is reached you don't get a new bar. Only reliable way it to work with time in all case.

In fact, it's possible to get a unique value with Bars function, but to do it, you have to set unlimited MaxBars (in Graphic/MetaTrader options). This is obviously why the Bars call isn't efficient in term of Time/Space complexity.

Thank's for your feedback Ange ;) 

Reason: