CopyTime returning -1 for data not ready - How can you wait for CopyX() Functions to fetch data first?

 

I am having some issues with the CopyX functions. CopyTime, CopyClose etc

It seems like when you call them, and the data is not there - the functions will return -1, and seem to asynchronously fetch the data you request.

So if you call CopyX a second time, the data will likely be there.

How can you wait for the functions to attempt to fetch data first before they return a failed result? Or give them time to...

My idea is pasted below, but it seems like Sleep() blocks the thread, and the CopyTime function still fails.

Any suggestions guys?

// My library method for fetching a candle's time
datetime ChartAPI::T(int CShift = USE_DEFAULT, const t_timeframe onTimeFrame = 0) {    
   if(CShift == USE_DEFAULT) { CShift = _candleShift; }
   t_timeframe tf = onTimeFrame == 0 ? _fileTimeFrame : onTimeFrame;
   
   #ifdef __MQL5__ 
      datetime timeseries[1];
      
      int try = 0;
      
      do {
         int result = CopyTime(_symbol, tf, CShift, 1, timeseries);
         
         if( result > 0 ) { break; }
         else {
            try++;
            printf("[%s] Error in accesing candle time %s %s %i", __FUNCTION__, _symbol, EnumToString(tf), CShift);
            printf("sleeping and trying again try = %i", try);
            Sleep(50); // Seems to block everything, and the CopyTime function does not download in the background while Sleep is active >:(
         } 
            
      } while(try <= 10);
      
      return timeseries[0];
      
      
      //return ( CopyTime(_symbol, tf, CShift, 1, timeseries) == 1 ) ? timeseries[0] : 0;
   #else
      return iTime(_symbol, tf, CShift); 
   #endif 
}

Seems like this problem needs to be solved with multi threading somehow.

 
dazamate:

I am having some issues with the CopyX functions. CopyTime, CopyClose etc

It seems like when you call them, and the data is not there - the functions will return -1, and seem to asynchronously fetch the data you request.

So if you call CopyX a second time, the data will likely be there.

How can you wait for the functions to attempt to fetch data first before they return a failed result? Or give them time to...

My idea is pasted below, but it seems like Sleep() blocks the thread, and the CopyTime function still fails.

Any suggestions guys?

Seems like this problem needs to be solved with multi threading somehow.

Bad idea. Why do you want to wait ?

If the data are not there returns and try on next tick. If you can't wait next tick, use a timer.

 
Alain Verleyen:

Bad idea. Why do you want to wait ?

If the data are not there returns and try on next tick. If you can't wait next tick, use a timer.

Hi Alain

I needed the current candle timestamp for a login authentication in the OnInit() part of the code.

 
dazamate:

Hi Alain

I needed the current candle timestamp for a login authentication in the OnInit() part of the code.

Move your login authentication code outside OnInit(), and run it when you know you have data.

As @Alain Verleyen points out, use a timer.

 
dazamate: a login authentication in the OnInit() part of the code.
Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
Alright thank you guys, sounds sensible.