Quick Answer
The MQL5 error 4806 (ERR_INDICATOR_DATA_NOT_FOUND, shown as "Requested data not found") means that CopyBuffer() asked an indicator for values and the terminal had none to give yet. In the forum threads it is almost always a timing problem, not a broken indicator. Three causes come back again and again:
- You read the buffer right after creating the handle. The indicator has not run. A moderator's answer in one thread: get the handle in
OnInit(), because the indicator has not run yet when you create it, and read the data later inOnTick()orOnCalculate(). - The data is for another symbol or timeframe. In a thread about 28 currency pairs, a moderator wrote that MT5 does not wait for data of other symbols or timeframes, so 4806 can appear at any time, and that the simple answer is to return and wait for the next tick.
- The request goes too far back. One poster read weekly data on a daily chart and got 4806 on the earliest bars. He started the loop at bar 10 instead of 0 and the error went away.
This post shows a broken EA, the fixed version with a BarsCalculated() check, a pattern for other timeframes, and a checklist. I have not compiled the examples, so compile and test them in MetaEditor before you rely on them.
From the author: I convert indicators into EAs, and 4806 is the error I see most when a converted EA "works on the second tick". The fix is not to sleep or to loop. It is to treat "no data yet" as a normal answer and try again on the next tick.
Key Facts
| What you see | Cause in the forum threads | What to do |
|---|---|---|
| 4806 right after iMA() or iCustom() | The indicator has not calculated yet | Create the handle in OnInit(), copy in OnTick() |
| 4806 only for another timeframe | The terminal does not wait for that data | Return and try again on the next tick |
| 4806 on the first bars of a loop | Not enough history for that timeframe at those bars | Start the loop at a later bar |
| Sleep(5000) in OnInit() did not help | A poster tried it and still got 4806 | Do not rely on sleeping |
| 4806 now and then, with correct code | Indicator data can lag behind the program | Skip this tick and copy again on the next |
The rows come from MQL5 forum threads. The table of error codes in the MQL5 reference could not be fetched for this post, so the name of the code is from the thread titles and answers.
A Broken EA
The EA creates an iMA() handle and copies the value on the very next line, in OnInit():
int maHandle = INVALID_HANDLE;
int OnInit()
{
maHandle = iMA(_Symbol, PERIOD_CURRENT, 20, 0, MODE_SMA, PRICE_CLOSE);
double ma[];
if(CopyBuffer(maHandle, 0, 0, 1, ma) <= 0)
Print("CopyBuffer failed, error ", GetLastError()); // 4806 in the threads
return(INIT_SUCCEEDED);
}
This is the shape of the first thread: the handle is created and the data is requested at once. A moderator's answer was that the indicator has not run yet at that point. The documentation point quoted in another thread explains the first call: when you ask an indicator for data that is not ready, the function returns -1 right away, but the building of the data is started. So the first call fails and a later call can succeed.
The Fix: Handle in OnInit, Data in OnTick
int maHandle = INVALID_HANDLE;
int OnInit()
{
maHandle = iMA(_Symbol, PERIOD_CURRENT, 20, 0, MODE_SMA, PRICE_CLOSE);
if(maHandle == INVALID_HANDLE)
{
Print("iMA failed, error ", GetLastError());
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
void OnTick()
{
// The indicator may still be calculating: wait for the next tick.
if(BarsCalculated(maHandle) <= 0)
return;
double ma[];
ArraySetAsSeries(ma, true);
ResetLastError();
if(CopyBuffer(maHandle, 0, 0, 1, ma) != 1)
{
PrintFormat("CopyBuffer: no data yet, error %d", GetLastError());
return; // try again on the next tick
}
Print("MA now: ", ma[0]);
}
Three habits are in this code. The handle is created once in OnInit() and checked against INVALID_HANDLE. BarsCalculated() is checked before the copy, as one poster suggested in a thread. And a failed copy ends in a return, not in a loop: the thread's moderator said that the simple way is to return and wait for the next tick. Do not call iMA() in OnTick() on every tick to "refresh" the handle. A moderator in the first thread said that this is the best way to have problems.
Other Timeframes and Other Symbols
Reading another timeframe is where 4806 appears most. One poster got it on an Ichimoku handle for H1 while the chart was on another period, and loading that timeframe on the chart first did not remove it. The thread has no answer, so I cannot confirm a fix for that case. The pattern from the answers in the other threads is the same:
int h1Handle = INVALID_HANDLE;
int OnInit()
{
h1Handle = iMA(_Symbol, PERIOD_H1, 50, 0, MODE_SMA, PRICE_CLOSE);
return(h1Handle == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED);
}
void OnTick()
{
if(BarsCalculated(h1Handle) <= 0)
return; // data of H1 is not ready
double ma[];
ArraySetAsSeries(ma, true);
if(CopyBuffer(h1Handle, 0, 1, 1, ma) != 1) // bar 1: the last closed H1 bar
return; // 4806: wait for the next tick
Print("H1 MA on the last closed bar: ", ma[0]);
}
A few notes from the threads:
- With several symbols, store the handles in an array and loop over it. In the 28-pair thread the poster did that, copied many candles on the first run and only the last candle after that. The moderator agreed with that design.
- Even so, 4806 still came now and then. The poster handled it by calculating the candle again on the next tick.
- A moderator warned against asking for one candle per call when you need history. It is fine for the live candle, and it is slow for history. Copy a block once.
Not Enough History at the Start
One poster read weekly values while the chart showed daily bars. At the oldest bars there was no weekly bar to match, and the call returned 4806. His fix was to start the loop at bar 10. The lesson is general: ask for the number of bars you need, check the number that CopyBuffer() returns, and do not assume that the oldest bars have data on every timeframe.
int need = 100;
double buf[];
int copied = CopyBuffer(h1Handle, 0, 1, need, buf);
if(copied < need)
PrintFormat("Got %d of %d values, error %d", copied, need, GetLastError());
The same idea, from the other side, is in MQL5 array out of range: read only the elements that exist.
A Script That Shows the Timing
This indicator-free EA prints what BarsCalculated() says on the first ticks. It does not trade:
int h = INVALID_HANDLE;
int ticks = 0;
int OnInit()
{
h = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE);
return(h == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED);
}
void OnTick()
{
if(ticks++ > 5)
return;
int calculated = BarsCalculated(h);
double v[];
ResetLastError();
int copied = CopyBuffer(h, 0, 0, 1, v);
PrintFormat("tick %d: calculated=%d copied=%d error=%d", ticks, calculated, copied, GetLastError());
}
On a symbol whose H1 data is already loaded, I expect calculated to be positive from the first tick. On a fresh chart or after a restart, you may see a first line with copied=-1 and error=4806, then lines that work. I have not run it, so treat that as the thing to check, not a promise.
What Not to Do
| Idea | Why it does not help |
|---|---|
| Sleep() in OnInit() | A poster tried 5 seconds and still got 4806 |
| Creating the handle in OnTick() on every tick | A moderator called it the best way to have problems |
| Changing the chart period from the code to "load" data | The thread's ChartSetSymbolPeriod attempt did not remove the error |
| Ignoring the return value | You then use an empty array and read garbage or fail with an index error |
Similar Messages
| Message | What it means | Where to look | |---|---|---| | 4806 Requested data not found | The indicator has no data yet | This post | | Array out of range | You read an element that does not exist | Array out of range | | 4014 System function is not allowed to call | A function is used in the wrong program type | Error 4014 | | OnCalculate not found | A custom indicator has no entry function | OnCalculate not found | | Undeclared identifier | A name the compiler does not know | Undeclared identifier | | Trade retcodes | The server's answer to an order | Trade retcodes guide |
Checklist: What to Check When You See 4806
- Is the handle valid? Compare it with
INVALID_HANDLE. - Was it created in
OnInit(), not just before the copy? - Do you call
BarsCalculated()beforeCopyBuffer()? - Does a failed copy return and wait for the next tick?
- Is it another timeframe or symbol? Expect the error on the first ticks.
- Do you check the copied count, not only the error?
- Do you start at a bar that has data, not at the oldest bar?
- Do you use
ResetLastError()before the call you test? - Test on a demo account and in the Strategy Tester.
Common Mistakes
- Reading the buffer in
OnInit()right after the handle. - Calling
iMA()oriCustom()on every tick. - Using
Sleep()to wait for data. - Treating one 4806 as a fatal error instead of waiting a tick.
- Asking for one candle per call when you need history.
- Not checking the returned count of
CopyBuffer().
FAQ
Related Guides
- MQL5 CopyBuffer guide: the full pattern for reading an indicator.
- Convert an MT5 indicator to an EA: one program instead of two.
- MQL5 array out of range: read only the elements that exist.
- MQL5 OnCalculate function not found: an indicator's entry point.
- MQL5 error 4014: system function is not allowed to call: a function in the wrong program type.
- MQL5 undeclared identifier: a name the compiler does not know.
- MQL5 trade retcodes guide: the server's answers to orders.
- What to check in a trading bot before going live: a pre-live checklist.
Fix Compile Errors Without Guessing
If an Expert Advisor or indicator does not compile, paste the code and the error messages into the CodeFlowOS Error Fixer. It suggests a minimal fix, explains each error and shows what changed. A fix uses 1 credit, with up to 3 attempts included, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong trading logic, so always review the result. The Code Review is free during the early-bird period. CodeFlowOS also converts strategies between Pine Script, MQL5 and cTrader C#: try the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always compile and test code in MetaEditor and the Strategy Tester before using it on a live account.