CustomRatesReplace returning -1 with LastError = 0

 

Can anyone explain how CustomRatesReplace could return -1 and LastError set to 0?

 
Show short source code of the problem.
 

I`ve seen this very late, but I know the answer:

When you try to import MqlRates (bars array) and not all of the elements are filled up with data, you'll get result= -1, but GetLastError() shows 0. All you need to do is to resize the array, so to avoid empty elements you pass to CustomRatesReplace(). For example you want to read 100 bars from a file, but the file reaches the end after 86 read bars. You need to resize the array first, lets say in some script will look like this:

MqlRates custRates[];

void OnStart()

  {
	ArrayResize(custRates,100); ZeroMemory(custRates); ResetLastError();

	- - - - - - - - 

	//Read data from file, but only n=86 elements are filled up

	- - - - - - - - 

	ArrayResize(custRates, n);

	int result_imported_bars = CustomRatesReplace(myCustomSymbol,custRates[0].time,custRates[n-1].time,custRates);

	if (result_imported_bars < 0) { Print("No luck when importing bars, error: ",GetLastError()); return; }

  }


Importing ticks with CustomTicksReplace() is the same. You need to resize the MqlTick array, so to delete the empty elements first.

Alternatively, if you don`t want to resize the array, you can simply pass the number of elements to be imported. This way you can use statically defined array, where resize is not possible:


MqlRates custRates[100];

void OnStart()

  {

        int n=86;  //number of elements filled up with data

        int result_imported_bars = CustomRatesReplace(myCustomSymbol,custRates[0].time,custRates[n-1].time,custRates,(uint)n);

   }

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / History Data Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / History Data Structure
  • www.mql5.com
Constants, Enumerations and Structures / Data Structures / History Data Structure - Reference on algorithmic/automated trading language for MetaTrader 5