Bars synchronization

 

So if one uses iOpen(Symbol(),Period(),Shift), for multiple pairs, how can you know that you are getting results for the very same bar?

Say that you have an EURUSD 1H chart open and at the opening of the new bar you want to load the bar open quote for EURUSD and GBPUSD.

double OpenEURUSD;

double OpenGBPUSD;

OpenEURUSD = iOpen("EURUSD",0,0);

OpenGBPUSD = iOpen("GBPUSD",0,0);

Then if the GBPUSD chart doesn't have the new bar yet, you get the open price of the previous bar compared to the EURUSD chart.

Anybody have some thoughts on this?

It would help if a similar function to return the open time.

 
HammerJack:


It would help if a similar function to return the open time.

Like iTime() ?
 
HammerJack: So if one uses iOpen(Symbol(),Period(),Shift), for multiple pairs, how can you know that you are getting results for the very same bar?
You don't. You've got the problem backwards. Don't try to check for the same bar, find it.
// double OpenEURUSD = iOpen("EURUSD",0,0);                  // The zero assumes the current chart is EURUSD
                                                             // So just use the predefined variables.
int      iGBPUSD  = iBarShift("GBPUSD", Period(), Time[0]);  // Can't use zero because GBPUSD is NOT the current chart.
                                                             // Find the correct shift and use it.
double OpenEURUSD = Open[0];
double OpenGBPUSD = iOpen("GBPUSD", Period(), iGBPUSD);
In the tester the current bar for the current chart is zero. But OTHER pairs are NOT tested/adjusted, their index can be in the thousands.
 

Thanks guys.

Would this be correct use of the mentioned code, to check that data from all needed bars is available?

   string Pairs[] = {"EURUSD","GBPUSD","USDJPY"};
   int BarShift[3];
   int NewTime = Time[1];
   bool AllBarsFound = True;
   for(int N =0; N<ArraySize(Pairs); N++){ 
      BarShift[N] =  iBarShift(Pairs[N],Period(),NewTime,True);
      if(BarShift[N]<0){AllBarsFound = False;}
   }
 
HammerJack:

Thanks guys.

Would this be correct use of the mentioned code, to check that data from all needed bars is available?

. . . and check for error 4066 . . . wait, and try again.
 
HammerJack:

So if one uses iOpen(Symbol(),Period(),Shift), for multiple pairs, how can you know that you are getting results for the very same bar?

Anybody have some thoughts on this?

It would help if a similar function to return the open time.

What about something like this:

string Pairs[] = {"EURUSD","GBPUSD","USDJPY"};
double OpenPrices[1];           
int iBar = 1;                    // looking back 1 bar on current chart                       
datetime BarTime = Time[iBar];   // looking at iBar's time     
   
if (Time[iBar] == BarTime) {
   OpenPrices[0] = Open[iBar];
   Print ("Open price for ", Symbol(), " is ", DoubleToStr(OpenPrices[0], 5));
}
for (int i = 0; i < ArraySize(Pairs); i++) {
   if (Pairs[i] == Symbol()) 
      continue;
   if (BarTime == iTime(Pairs[i], Period(), iBar)) {
      ArrayResize(OpenPrices, ArraySize(OpenPrices)+1);
      OpenPrices[ArraySize(OpenPrices) - 1] = iOpen(Pairs[i], Period(), iBar);
      Print ("Open price for ", Pairs[i], " is ", DoubleToStr(OpenPrices[ArraySize(OpenPrices) - 1], 5));
   }  
}

Test #1

As RaptorUK mentioned, you will need to test for an error 4066. If you get an error 4066, just wait for about 10 seconds and then retry again.

 
Thanks for bringing up the error 4066. If ignored when it occurs, would iBarShift() return -1 ?