BarsCalculated returning -1 for unknown reason

 

Can someone tell me why BarsCalculated gives me -1 in the following code when testing in the tester. It appears to work correctly on a live chart but not when in tester. Data goes back to 2007 and I'm testing from 2017 so there should be plenty of data for the calculation.


Any ideas?


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
double ma_buffer[];
int ma_handle;
int tick_count = 0;

                 int OnInit()
  {
//---

   ma_handle=iMA("NZDCAD", PERIOD_W1, 200, 0, MODE_SMA, PRICE_CLOSE);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   tick_count++;
//---
   int numBarsRequired = 4;

   int availableBars = BarsCalculated(ma_handle);
   int numAvailableBars = CopyBuffer(ma_handle, 0, 0, numBarsRequired, ma_buffer);
   ArraySetAsSeries(ma_buffer, true);
   
   if(tick_count==10)
     {
      Comment("MA=" + DoubleToString(ma_buffer[0]));
      tick_count = 0;
     }
  }
//+------------------------------------------------------------------+
 
EdFuk: Can someone tell me why BarsCalculated gives me -1 in the following code when testing in the tester. It appears to work correctly on a live chart but not when in tester. Data goes back to 2007 and I'm testing from 2017 so there should be plenty of data for the calculation.

You are not checking if the handle provided is in fact valid or not.

In the OnInit(), check if the "ma_handle" is valid or not, and if it's not, check the error code (_LastError).

Also check if BarsCalculated() function returns -1, then check the error code too.

Documentation on MQL5: Predefined Variables / _LastError
Documentation on MQL5: Predefined Variables / _LastError
  • www.mql5.com
_LastError - Predefined Variables - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

You always need to check the return value and if there was an error, then you need to print this error. Example:

//--- determine the number of values calculated in the indicator
   int calculated=BarsCalculated(handle);
   if(calculated<=0)
     {
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
      return(0);
     }
 
Vladimir Karputov #:

You always need to check the return value and if there was an error, then you need to print this error. Example:

Thankyou guys I will implement and get back to you. 

Standby. 

Ed
 

I implemented the checks as you suggested.


The indicator handle is fine but the calculated barrs returns -1 with the following:


2022.04.13 17:12:26.073 2017.01.02 00:01:00   BarsCalculated() returned -1, error code 4806


I check 4806 and that error is 

ERR_INDICATOR_DATA_NOT_FOUND

4806

Requested data not found



Any Ideas why this might be?
 
EdFuk # :


You need to carefully read the Documentation - since you are working on a non-native timeframe (on W1), the data on W1 may not be ready yet.

 
Vladimir Karputov #:

You need to carefully read the Documentation - since you are working on a non-native timeframe (on W1), the data on W1 may not be ready yet.

Thanks I will have to have a look at it. It seems to work with real time data so I suspect it's the way the test data is handled.


The tester setup was as shown below but the indicator uses W1 data. I will get searching now.

 
EdFuk #:

Thanks I will have to have a look at it. It seems to work with real time data so I suspect it's the way the test data is handled.


The tester setup was as shown below but the indicator uses W1 data. I will get searching now.

Did you solve? What was the problem? I too have the same difficulty
Reason: