Attempting to verify that a new bar has formed

 

Hello all, 

I'm testing an EA that scans multiple currency pairs at the beginning of the bar and calculates the relative currency strength of each and notifies me of the strongest or most active pair at any given moment.  I've come to realize that sometimes a pair may be slow at the moment and when I want to calculate the overall strength, not all pairs have received new ticks on the new bar so the calculation is incorrectly using old bar data.   Waiting a couple of seconds works most of the time, but I'm looking to determine if a new bar has been received across all pairs to make sure the calculation is always current.

I read through some discussion on the forum, and then went to the documentation, and I think the following will work.  The main question I have is if in the case of the time series that has not had a new tick to form the new bar, will the old bar still be in index position[0] until that tick comes in, which would cause the below to not work as I'd hope?

bool VerifyCurrentBarHasFormedOnAllPairs(ENUM_TIMEFRAMES my_period)
{
   int paircnt = 28;
   string cpair[];
   FillPairArray(cpair);        
   
   int   bar_index   = 0;
   bool  exact       = false; 
   datetime now      = TimeCurrent();
   
   for(int i=0; i<paircnt; i++)
   {
      bar_index = iBarShift(cpair[i], my_period, now, exact);
      if(bar_index < 0) return(false);
   }
   return(true);
}


Much of the discussion on the board was around using Time (iTime), so here's my thought on how to implement it using iTime...

bool VerifyCurrentBarHasFormedOnAllPairs(ENUM_TIMEFRAMES my_period)
{
   int paircnt = 28;
   string cpair[];
   FillPairArray(cpair);        
   
   datetime now      = TimeCurrent() - TimeSeconds(TimeCurrent());
   
   for(int i=0; i<paircnt; i++)
   {
      if( iTime(cpair[i], my_period, 0)-now < 0) return(false);      
   }
   return(true);
}

I am aware that I have not accounted for the start time of a bar of different chart time frames--ie. if it is a M15 chart I'll need to trim the excess minutes off of "now" to get to the correct minute interval, but that being said I am curious if the thesis of either approach is correct or if I am missing something...which seems to happen now and then.

 

Just wait until all bar open times are equal then you will know that they have all renewed it can take a few seconds but you can not make a measurement during that time anyway. 

So when one out of all bars trips over into a new bar the condition will become false until all bars have been renewed then it will be true again.

In testing environment i always set the chart background color to red if the bars are unequal and to green if they are equal this way you will get a visual representation of when one bar trips over into a new candle and will become green again when all bars are equal again and have renewed.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
 
Marco vd Heijden:

Just wait until all bar open times are equal then you will know that they have all renewed it can take a few seconds but you can not make a measurement during that time anyway. 

So when one out of all bars trips over into a new bar the condition will become false until all bars have been renewed then it will be true again.

In testing environment i always set the chart background color to red if the bars are unequal and to green if they are equal this way you will get a visual representation of when one bar trips over into a new candle and will become green again when all bars are equal again and have renewed.


So to be clear, if I am monitoring 28 currency pairs all I need to do is check the iTime of the first pair and compare it to all the rest and fail if it is not the same across all pairs?  ....and regardless of how many seconds into the bar the tick was received, the bar will reflect the expected open time once that tick is received.

Thanks much, this is a simple way to look at it.

N

Reason: