What it means
The terminal couldn't create the indicator the EA asked for, so the function returned INVALID_HANDLE and GetLastError() returns 4802. Every CopyBuffer() on that handle then fails with 4807 (ERR_INDICATOR_WRONG_HANDLE, "Wrong indicator handle").
Usual causes
- The custom indicator isn't where
iCustom()looks. MetaQuotes' reference says it must be compiled (an .ex5 file) and stored in the terminal'sMQL5/Indicatorsfolder or a subfolder of it, with the path given relative to that folder. - The file is missing on another machine. The EA was copied to a VPS or another terminal without the indicator.
- The Strategy Tester doesn't know about it. The tester finds indicators named by a constant string in
iCustom()by itself. When the name comes from a variable, or the EA usesIndicatorCreate(), the program needs#property tester_indicator. - A symbol that doesn't exist on this account, for a standard indicator such as
iMA()on another symbol. That case has its own code, 4801 (ERR_INDICATOR_UNKNOWN_SYMBOL).
How to detect it
- The handle equals
INVALID_HANDLEright afteriCustom(),iMA()and the other indicator functions. ReadGetLastError()immediately, before any other call. - The Experts or Journal tab can show a "cannot load custom indicator" line with the indicator's name.
- Later,
CopyBuffer()fails with 4807.
Broken code
This EA compiles. Without MyTrend.ex5 it keeps running, copying nothing on every tick, without saying why.
int trendHandle;
int OnInit()
{
// No check: if MyTrend.ex5 isn't in MQL5/Indicators, the handle is INVALID_HANDLE
trendHandle = iCustom(_Symbol, PERIOD_CURRENT, "MyTrend", 20);
return INIT_SUCCEEDED;
}
void OnTick()
{
double value[];
CopyBuffer(trendHandle, 0, 1, 1, value); // fails with 4807, ignored
}Fixed code
The same program with the check that prevents the error. It compiles in MetaEditor with 0 errors and 0 warnings.
#property tester_indicator "MyTrend.ex5"
input int InpTrendPeriod = 20; // Period passed to MyTrend
int trendHandle = INVALID_HANDLE;
int OnInit()
{
ResetLastError();
trendHandle = iCustom(_Symbol, PERIOD_CURRENT, "MyTrend", InpTrendPeriod);
if(trendHandle == INVALID_HANDLE)
{
// Stop here with a clear message instead of running without data
PrintFormat("Cannot create MyTrend (error %d): put MyTrend.ex5 in MQL5/Indicators and compile it", GetLastError());
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
void OnTick()
{
if(BarsCalculated(trendHandle) < 2)
return;
double value[];
if(CopyBuffer(trendHandle, 0, 1, 1, value) != 1)
return;
Comment("MyTrend: ", DoubleToString(value[0], _Digits));
}
void OnDeinit(const int reason)
{
if(trendHandle != INVALID_HANDLE)
IndicatorRelease(trendHandle);
Comment("");
}Why the fix works
The handle is checked once, in OnInit, right after it's created. If the indicator can't be created, the EA prints the error with what to do and returns INIT_FAILED, so MetaTrader removes it instead of letting it run without data. #property tester_indicator makes sure the Strategy Tester takes the indicator along, and IndicatorRelease() frees it when the EA stops. Reading the values waits for BarsCalculated(), as explained for error 4806.
Questions
Where must a custom indicator be for iCustom() to find it?
Compiled, as an .ex5 file, in the terminal's MQL5/Indicators folder or one of its subfolders. The name you pass to iCustom() is the path relative to MQL5/Indicators, without the extension.
Why does my EA work on my computer but not on the VPS or in the tester?
The indicator file isn't there. Copy the .ex5 to MQL5/Indicators on the other terminal. For the Strategy Tester, a constant name in iCustom() is detected automatically; otherwise add #property tester_indicator with the indicator's file name.
What is MQL5 error 4807?
ERR_INDICATOR_WRONG_HANDLE, "Wrong indicator handle". It comes from passing a handle that isn't valid, usually INVALID_HANDLE from an indicator that couldn't be created, to CopyBuffer() or another function that reads an indicator.
The programs on this page are minimal examples written to show the error and its fix, not trading systems. Some of them send orders when they run: try them on a demo account. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.