Download MetaTrader 5

Bars() always returning zero.

To add comments, please log in or register
Tom Clayson
233
Tom Clayson  

No matter what I do I can't seem to get Bars to return the number of bars between two times. Please could someone tell me what is wrong with the code below. This is run as a script just to test.

void OnStart() {

   datetime OpenTime[10];
   
   if(CopyTime(_Symbol,_Period,0,10,OpenTime)==-1) {
      Print(__FUNCTION__," CopyTime() returned -1: ",ErrorDescription(GetLastError()));
   }
   // Start position in CopyTime becomes that last element in the OpenTime[] array.
   // Accordingly, the StartPosition+Count element becomes the first. 
   
   datetime Time1 = OpenTime[0];
   datetime Time2 = OpenTime[9];
   
   int _Bars = Bars(_Symbol,_Period,Time1,Time2);
   
   if(_Bars>0){
      Print("_Bars = ",_Bars);
   }
   else {  //no available _Bars 
      //--- data on the symbol might be not Synchronized with data on the server
      bool Synchronized=false;
      //--- loop counter
      int Attempts=0;
      // make 5 Attempts to wait for synchronization
      while(Attempts<5) {
         Synchronized = (bool)SeriesInfoInteger(_Symbol,_Period,SERIES_SYNCHRONIZED);
         if(Synchronized){
            break;
         }
         //--- increase the counter
         Attempts++;
         //--- wait 10 milliseconds till the nest iteration
         Sleep(100);
      }
      //--- exit the loop after synchronization
      if(Synchronized){
         Print("_Bars = ",_Bars);
      }
      //--- synchronization of data didn't happen
      else{
         Print("Failed");
      }
   }

}
Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
Timeseries and Indicators Access / Bars - Documentation on MQL5
Alain Verleyen
Moderator
34525
Alain Verleyen  

There is currently a bug with Bars() function when used as in your code. This is present from build 868 (858?) and I reported it to ServiceDesk but don't get a reply.

So I suggest you to report it also to ServiceDesk.

Tom Clayson
233
Tom Clayson  
angevoyageur:

There is currently a bug with Bars() function when used as in your code. This is present from build 868 (858?) and I reported it to ServiceDesk but don't get a reply.

So I suggest you to report it also to ServiceDesk.

Thank you for letting me know. Wish I had asked earlier instead of stubbornly trying every combination i could think of to make it work! I can't find the service desk though, I just get to the teamwox site and can't see where to submit.

Do you have an alternative for an equivalent to iBarShift by any chance if this method is not working?

Alain Verleyen
Moderator
34525
Alain Verleyen  
whitebloodcell:

Thank you for letting me know. Wish I had asked earlier instead of stubbornly trying every combination i could think of to make it work! I can't find the service desk though, I just get to the teamwox site and can't see where to submit.

Do you have an alternative for an equivalent to iBarShift by any chance if this method is not working?

Get in touch with developers using Service Desk! - MQL5 forum

About an alternative, it depends on what's your goal.

Tom Clayson
233
Tom Clayson  
angevoyageur:

Get in touch with developers using Service Desk! - MQL5 forum

About an alternative, it depends on what's your goal.

Have reported the error now, and found an alternative implementation of iBarShift which does the job for now. Thanks again for your help.

To add comments, please log in or register