Skip to content

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 to MQL5\Indicators\ and has no extension, and the .ex5 must 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() and iCustom() return a handle, not a value; the values come from CopyBuffer().

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

  1. Check every handle against INVALID_HANDLE in OnInit() and fail the start with the error code.
  2. Give iCustom() the path below MQL5\Indicators\ without the extension, and the inputs in order.
  3. Release handles only in OnDeinit(), after their last use.

Fixed MQL5 code

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

EA Doctor rules that catch it

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.