Runtime error
4807 ERR_ INDICATOR_ WRONG_ HANDLE
Wrong indicator handle: the handle is invalid or released
What it means
A function that reads an indicator was given a handle that is not a valid indicator handle. The handle is usually INVALID_HANDLE because creating the indicator failed and nobody checked, or it was released before it was used.
Common causes
iCustom()with a wrong path or name: the path is relative toMQL5\Indicators\and has no extension, and the.ex5must exist.- An indicator's inputs passed in the wrong number or type, so the indicator is not created.
- A handle used after
IndicatorRelease(), or a handle variable that was never assigned. - MQL4 habits: in MQL5
iMA()andiCustom()return a handle, not a value; the values come fromCopyBuffer().
A real scenario
An EA calls iCustom(_Symbol, 0, "Indicators\\MySignal.ex5"). Because the path already starts in the Indicators folder and includes the extension, the indicator is not found, the handle is INVALID_HANDLE, and every CopyBuffer() call fails with 4807. Using "MySignal" and checking the handle in OnInit() fixed it; a product can also embed the indicator as a resource.
How to fix it
- Check every handle against
INVALID_HANDLEinOnInit()and fail the start with the error code. - Give
iCustom()the path belowMQL5\Indicators\without the extension, and the inputs in order. - Release handles only in
OnDeinit(), after their last use.
Fixed MQL5 code
int g_zz = INVALID_HANDLE;
int OnInit()
{
// the path is relative to MQL5\Indicators\ and has no extension
g_zz = iCustom(_Symbol, PERIOD_CURRENT, "Examples\\ZigZag", 12, 5, 3);
if(g_zz == INVALID_HANDLE)
{
PrintFormat("iCustom failed, error %d: check the path, the inputs and that the .ex5 exists", GetLastError());
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
void OnDeinit(const int reason)
{
if(g_zz != INVALID_HANDLE)
IndicatorRelease(g_zz); // release after the last use, not before
}
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
- 4806Indicator Data Not FoundERR_INDICATOR_DATA_NOT_FOUNDIndicator data not found: CopyBuffer() before the data is ready
- 4301Unknown SymbolERR_MARKET_UNKNOWN_SYMBOLUnknown symbol: the broker has no symbol with that name
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().
- 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 4807? 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.