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](https://c.mql5.com/36/55/documentation-on-mql5-predefined.png)
- www.mql5.com
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); }
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 |
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.
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.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?