CopyRates function returning -1 with error 4401 : Different behavior between two launched.

 

Good morning,

I had a strange behavior using the CopyRates function in MQL5. I tried to load some Rates using the CopyRates function for a specified Symbol (CustomSymbol). The requested data is on a PERIOD_M1 and represents two months of rates starting from the SERIES_TERMINAL_FIRSTDATE of the Symbol.

The first time I ran the script, the function always returns -1 with the error code 4401 (from GetLastError()). The second time I ran the script with the same set of parameters, it worked perfectly. It seems that MT5 loaded the data asynchronously between the two launches.

The source code:

bool bCheckSymbolRates(string sSymbol, datetime oStartDateTime, datetime oEndDateTime)
{
  bool  bRet = true;
  int   _iSourceMqlRatesCount = 0;
 
  // Checks minimum first date
  if (oStartDateTime < (datetime) SeriesInfoInteger(sSymbol, 0, SERIES_TERMINAL_FIRSTDATE))
    oStartDateTime = (datetime) SeriesInfoInteger(sSymbol, 0, SERIES_TERMINAL_FIRSTDATE);
 
  // dynamic table allocation
  _iSourceMqlRatesCount = 0;
 
  while (bRet == true && oStartDateTime < oEndDateTime)
  {
    // 2 months of data
    datetime  oTmpEndDateTime = oStartDateTime + 3600 * 24 * 61;
    MqlRates  tTmpMqlRates[];
   
    ArraySetAsSeries(tTmpMqlRates, false);
    int  iCount = CopyRates(sSymbol, PERIOD_M1, oStartDateTime, oTmpEndDateTime, tTmpMqlRates);
   
    if (iCount != -1)
    {
      datetime oFirstvalue = tTmpMqlRates[0].time;
      _iSourceMqlRatesCount += iCount;
    }
    else
    {
      int iError = GetLastError();
      printf("AggSymbolData - Error while loading data : %i", GetLastError());
      bRet = false;
    }
  
    // Go to the next range
    oStartDateTime = oTmpEndDateTime + 60;
  }
  return bRet;
};

1st launch: FAILED

SERIES_TERMINAL_FIRSTDATE = D'2003.03.24 00:00:00'

SERIES_FIRSTDATE = D'2003.03.24 09:15:00'

SERIES_LASTBAR_DATE = D'2018.02.08 21:45:00'

SERIES_BARS_COUNTS = 203018

SERIES_SYNCHRONIZED = TRUE

iCount = -1 and iError = 4401


2nd launch:  SUCCESS with the same set of parameters

SERIES_TERMINAL_FIRSTDATE = D'2003.03.24 00:00:00'

SERIES_FIRSTDATE = D'2003.03.24 09:15:00'

SERIES_LASTBAR_DATE = D'2018.02.08 21:45:00'

SERIES_BARS_COUNTS = 203018

SERIES_SYNCHRONIZED = TRUE

iCount = 27427 (on the first while iteration).


Is there a solution to avoid this problem like?

Thanks

 

Just look into the reference!

You'll find: ERR_HISTORY_NOT_FOUND which means that your terminal hasn't got all the quotes it has requested.-

Either: if (..) return() or if(..) sleep().

 
fdelaitre:

Good morning,

I had a strange behavior using the CopyRates function in MQL5. I tried to load some Rates using the CopyRates function for a specified Symbol (CustomSymbol). The requested data is on a PERIOD_M1 and represents two months of rates starting from the SERIES_TERMINAL_FIRSTDATE of the Symbol.

The first time I ran the script, the function always returns -1 with the error code 4401 (from GetLastError()). The second time I ran the script with the same set of parameters, it worked perfectly. It seems that MT5 loaded the data asynchronously between the two launches.

The source code:

1st launch: FAILED

SERIES_TERMINAL_FIRSTDATE = D'2003.03.24 00:00:00'

SERIES_FIRSTDATE = D'2003.03.24 09:15:00'

SERIES_LASTBAR_DATE = D'2018.02.08 21:45:00'

SERIES_BARS_COUNTS = 203018

SERIES_SYNCHRONIZED = TRUE

iCount = -1 and iError = 4401


2nd launch:  SUCCESS with the same set of parameters

SERIES_TERMINAL_FIRSTDATE = D'2003.03.24 00:00:00'

SERIES_FIRSTDATE = D'2003.03.24 09:15:00'

SERIES_LASTBAR_DATE = D'2018.02.08 21:45:00'

SERIES_BARS_COUNTS = 203018

SERIES_SYNCHRONIZED = TRUE

iCount = 27427 (on the first while iteration).


Is there a solution to avoid this problem like?

Thanks

There is no problem at all, it's how it works.
 
On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
          Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
 

I don't understand why, when calling CopyRates for a different period in an OnCalculate of a custom indicator, the first time I always get -1 as results even if SERIES_SYNCHRONIZED do not return 0 (false).

This is inside of my OnCalculate function, at the first tick after indicator is attached to the chart I get CopyRates<0 with error 4401 but I do not see log    "D1 prices not synched" 

if( !(bool)SeriesInfoInteger(NULL,PERIOD_D1,SERIES_SYNCHRONIZED) ) { Print("D1 prices not synched"); return(0); }

copied = CopyRates(NULL,PERIOD_D1,0,2,D1);
if( copied<0 ) { Print("error in getting D1 prices"); return(0); }

At the second tick rates are successfully copied. It's not exactly I problem but I can't understand why it happens...

 
Fabio Cavalloni:

I don't understand why, when calling CopyRates for a different period in an OnCalculate of a custom indicator, the first time I always get -1 as results even if SERIES_SYNCHRONIZED do not return 0 (false).

This is inside of my OnCalculate function, at the first tick after indicator is attached to the chart I get CopyRates<0 with error 4401 but I do not see log    "D1 prices not synched" 

At the second tick rates are successfully copied. It's not exactly I problem but I can't understand why it happens...

Forum on trading, automated trading systems and testing trading strategies

About SERIES_SYNCHRONIZED

attila.okcu, 2016.11.18 11:05

I've got the following reply from the service desk: "Actually, the SERIES_SYNCHRONIZED flag means that the local price history is synchronized with the server. It is not related to timeseries."

And, the service desk also said that the example code in the documentation will be checked and fixed if necessary.