Runtime error
4806 ERR_ INDICATOR_ DATA_ NOT_ FOUND
Indicator data not found: CopyBuffer() before the data is ready
What it means
The indicator data asked for is not available yet. The reference explains why for CopyBuffer(): when the timeseries are not built yet or must be downloaded, the download or calculation is started and the call returns what is ready, or -1 at once when called from an indicator. Code that does not check the count then reads an empty array.
Common causes
- Reading an indicator of another timeframe or symbol on the first ticks after start, before its history is loaded.
- Creating the indicator handle and reading it in the same call (for example inside
OnTick()on every tick). - Not checking the number of values
CopyBuffer()returned, which leads to "array out of range" and the EA stopping.
A real scenario
An EA on M5 filters entries with an H1 EMA. On the first tick after the terminal starts, H1 history is still loading: CopyBuffer() returns -1 with 4806, the EA reads buf[0] from an empty array, and the terminal stops it with "array out of range". Creating the handle once in OnInit() and skipping ticks until BarsCalculated() is enough fixed it.
How to fix it
- Create indicator handles once, in
OnInit(). - Before reading, check
BarsCalculated(handle)covers the bars you need. - Check the count
CopyBuffer()returns and skip the tick when it is short; never index an array you did not fill.
Fixed MQL5 code
int g_ma = INVALID_HANDLE;
int OnInit()
{
g_ma = iMA(_Symbol, PERIOD_H1, 50, 0, MODE_EMA, PRICE_CLOSE); // create once, not on every tick
return g_ma == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED;
}
bool ReadMa(double &value)
{
if(BarsCalculated(g_ma) < 2)
return false; // still loading or calculating: try on the next tick
double buf[];
ArraySetAsSeries(buf, true);
if(CopyBuffer(g_ma, 0, 1, 1, buf) != 1) // bar 1 = the last closed bar
{
PrintFormat("CopyBuffer failed, error %d", GetLastError());
return false;
}
value = buf[0];
return true;
}
A focused example for MetaTrader 5: adapt names, magic number and limits to your EA, and test it on a demo account first.
Related codes
- 4807Wrong Indicator HandleERR_INDICATOR_WRONG_HANDLEWrong indicator handle: the handle is invalid or released
- 4302Symbol Not SelectedERR_MARKET_NOT_SELECTEDSymbol not selected in Market Watch
EA Doctor rules that catch it
- HighIndicator handle created on every tickAn indicator function such as iMA() or iCustom() is called in OnTick(), so a handle is requested on every tick instead of once in OnInit().
- MediumCopyBuffer() or CopyRates() result not checkedA Copy function is called as a plain statement, so the code reads the array even when nothing was copied.
- MediumIndicator handle not checked for INVALID_HANDLEAn indicator handle is stored without checking for INVALID_HANDLE, so a missing custom indicator goes unnoticed and every read fails.
Sources
Get your EA fixed
Still failing with 4806? Send us the EA and the journal lines: we find the cause in the code and fix it. Or check it yourself first with the EA Doctor.