Skip to content

EA Doctor rule · Signals and data

Indicator handle not checked for INVALID_HANDLE

An indicator handle is stored without checking for INVALID_HANDLE, so a missing custom indicator goes unnoticed and every read fails.

Why it matters

An indicator function returns INVALID_HANDLE when the indicator cannot be created: a custom indicator file that is missing or named wrongly in iCustom(), wrong parameters, an unavailable symbol.

Every CopyBuffer() on that handle fails (4807, wrong indicator handle), and an EA that started anyway runs without its signal. OnInit() is the place to stop with INIT_FAILED and say why.

When the rule fires

A handle is assigned from iMA(), iCustom() or another indicator function, and the file never compares a handle with INVALID_HANDLE (or the variable with a negative number).

When it stays quiet

INVALID_HANDLE is compared anywhere in the file, or the handle is checked with < 0.

How to fix it

  1. After creating each handle in OnInit(), compare it with INVALID_HANDLE.
  2. When it fails, print GetLastError() and the indicator's name and return INIT_FAILED.

Code that fires it

MQL5 · fires this rule
int trendH;

int OnInit()
  {
   trendH = iCustom(_Symbol, PERIOD_CURRENT, "Examples\\ZigZag");
   return(INIT_SUCCEEDED);
  }

Fixed code

MQL5 · fixed
int trendH = INVALID_HANDLE;

int OnInit()
  {
   trendH = iCustom(_Symbol, PERIOD_CURRENT, "Examples\\ZigZag");
   if(trendH == INVALID_HANDLE)
     {
      PrintFormat("Cannot load Examples\\ZigZag: error %d", GetLastError());
      return(INIT_FAILED);
     }
   return(INIT_SUCCEEDED);
  }

The fixed code shows this rule’s fix and compiles in MetaEditor for MetaTrader 5 (with CTrade trade, InpLots and InpMagic declared, as in a typical EA). Other rules may still apply to it as a fragment: check your whole EA.

More rules on signals and data

Sources

Get your EA fixed

Check a whole EA for this and 29 other mistakes with EA Doctor, free and in your browser. Or send it to us: we confirm the price in writing, then fix it.