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
- After creating each handle in
OnInit(), compare it withINVALID_HANDLE. - When it fails, print
GetLastError()and the indicator's name and returnINIT_FAILED.
Code that fires it
int trendH;
int OnInit()
{
trendH = iCustom(_Symbol, PERIOD_CURRENT, "Examples\\ZigZag");
return(INIT_SUCCEEDED);
}Fixed code
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.
Related MQL5 errors
- 4807Wrong Indicator HandleERR_INDICATOR_WRONG_HANDLE
- 4806Indicator Data Not FoundERR_INDICATOR_DATA_NOT_FOUND
More rules on signals and data
- MediumSignal read from the bar that is still formingThe EA compares indicator values of bar 0, which change with every tick until the bar closes, so its entries differ from what the chart shows later.
- MediumCopyBuffer() or CopyRates() result not checkedA Copy function is called as a plain statement, so the code reads the array even when nothing was copied.
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.