Skip to content

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

  1. Create indicator handles once, in OnInit().
  2. Before reading, check BarsCalculated(handle) covers the bars you need.
  3. Check the count CopyBuffer() returns and skip the tick when it is short; never index an array you did not fill.

Fixed MQL5 code

MQL5
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.

EA Doctor rules that catch it

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.