MT5- what is error 4116?

Hello,

I have an EA written in mql5, everything works properly in both live trading and strategy tester. However some times when trying to update local buffers of the indicators the ea is dependent on, it turns an error 4116.

This is totally strange to me as all the indicators were attached to the chart, and none has been removed since the last time the buffers were updated.

Am I missing anything?

You can check error code for MQL5 at https://www.mql5.com/en/docs/constants/errorswarnings/errorcodes

It looks to me the indicator can’t be found by your EA. This could happen if you did not set a correct location for your indicator handle. Use like below.

int h_ma1=iMA(Symbol(),Period(),8,0,MODE_SMA,PRICE_CLOSE);

   int h_ma2=iMA(Symbol(),Period(),16,0,MODE_SMA,PRICE_CLOSE);

   int h_stoh=iStochastic(Symbol(),Period(),5,3,3,MODE_SMA,STO_LOWHIGH);

   int h_macd=iMACD(Symbol(),Period(),12,26,9,PRICE_CLOSE);

   int h_pc=iCustom(Symbol(),Period(),"Price Channel",22);

   int h_acadx=iCustom(Symbol(),Period(),"AdaptiveChannelADX",14);

   int h_stoh=iStochastic(Symbol(),Period(),5,3,3,MODE_SMA,STO_LOWHIGH);

   int h_rsi=iRSI(Symbol(),Period(),14,PRICE_CLOSE);

   int h_cci=iCCI(Symbol(),Period(),14,PRICE_TYPICAL);

   int h_wpr=iWPR(Symbol(),Period(),14);

   int h_bb=iBands(Symbol(),Period(),20,0,2,PRICE_CLOSE);

   int h_sdc=iCustom(Symbol(),Period(),"StandardDeviationChannel",14,0,MODE_SMA,PRICE_CLOSE,2.0);

   int h_env=iEnvelopes(Symbol(),Period(),28,0,MODE_SMA,PRICE_CLOSE,0.1);

   int h_dc=iCustom(Symbol(),Period(),"Donchian Channels",24,3,-2);

   int h_sc=iCustom(Symbol(),Period(),"Silver-channels",26,38.2,23.6,0,61.8);

   int h_gc=iCustom(Symbol(),Period(),"PriceChannelGalaher");

   int h_nrtr=iCustom(Symbol(),Period(),"NRTR",40,2.0);

   int h_al=iAlligator(Symbol(),Period(),13,0,8,0,5,0,MODE_SMMA,PRICE_MEDIAN);

   int h_ama=iAMA(Symbol(),Period(),9,2,30,0,PRICE_CLOSE);

   int h_ao=iAO(Symbol(),Period());

   int h_ich=iIchimoku(Symbol(),Period(),9,26,52);

   int ZigZagHigh=iCustom(NULL,0,"ZigZag",MODE_HIGH,0);

Documentation on MQL5: Standard Constants, Enumerations and Structures / Codes of Errors and Warnings / Runtime Errors

The name of the custom symbol is invalid. The symbol name can only contain Latin letters without punctuation, spaces or special characters (may only contain “.”, “_”, “&” and “#”). It is not recommended to use characters , :, ", /, |, ?, *. The path of…

That’s exactly how I did it, note that the EA calls the indicator correctly and gets the values, however sometimes it fails.

Let’s say for example the EA calls the indicator 20 times, about 6 will return error 4116 while the rest will return successfully

Show your code if you need coding help.

int OnInit()
  {
   ArraySetAsSeries(AngleMA,true);
   ArraySetAsSeries(ALMA,true);
   ArraySetAsSeries(Time,true);
   ArraySetAsSeries(UpBB,true);
   ArraySetAsSeries(LowBB,true);
//---
   int Error = EMPTY;
   AngleMAHandle=iCustom(_Symbol,PERIOD_CURRENT,"angle_of_averages",AngleMAPeriod
                         ,CalculationType,AngleMaPrice,AngleMaBars,AngleMaLevel);
    Error = GetLastError();
   if(AngleMAHandle == INVALID_HANDLE)
     {Print ("Error Code ",Error);
      Print("Indicator error! Please confirm the indicator Angle of Averages exists and it's name is \"angle_of_averages\"");
      return(INIT_FAILED);
     }
   ResetLastError();
   ALMAHandle=iCustom(_Symbol,PERIOD_CURRENT,"ALMA_v2",AlmaTF,AlmaPrice,AlmaWindowSize
                      ,AlmaSigma,AlmaOffset,AlmaShift,AlmaColorMode);
   Error = GetLastError();
   if(ALMAHandle == INVALID_HANDLE)
     {Print ("Error Code ",Error);
      Print("Indicator error! Please confirm the indicator ALMA v_2 exists and it's name is \"ALMA_v2\"");
      return(INIT_FAILED);
     }
     BBandsStopHandle = iCustom(_Symbol,PERIOD_CURRENT,"bbands_stop_v1",BollingerPeriod
                         ,BBandsDeviation,BBandsOffsetFactor,BBandsSignal,BBandsLine);
   Error = GetLastError();
   if(BBandsStopHandle == INVALID_HANDLE)
     {Print ("Error Code ",Error);
      Print("Indicator error! Please confirm the indicator bbands_stop_v1 exists and it's name is \"bbands_stop_v1\"");
      return(INIT_FAILED);
     }
   long WindowsTotal = ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   for(long i=0;i<WindowsTotal;i++)                   //Runs through all available windows
     {for(int k = ChartIndicatorsTotal(0,(int)i);k>=0;k--)  //Gets all available indicators in the window
        {string shortname = ChartIndicatorName(0,(int)i,k);
         if (StringSubstr(shortname,0,5)== "Angle" || StringSubstr(shortname,0,4) == "ALMA" || StringSubstr(shortname,0,5) == "BBand" )
         {ChartIndicatorDelete(0,(int)i,shortname);
         }
        }
      
     }
   WindowsTotal = ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   ChartIndicatorAdd(0,0,BBandsStopHandle);
   ChartIndicatorAdd(0,0,ALMAHandle);
   ChartIndicatorAdd(0,(int)WindowsTotal,AngleMAHandle);
//---
   ChartRedraw(0);
   return(INIT_SUCCEEDED);
  }

void UpdateIndicators()
{int Error;
 ArrayInitialize(ALMA,EMPTY_VALUE);
 ArrayInitialize(AngleMA,EMPTY_VALUE);
 ArrayInitialize(UpBB,EMPTY_VALUE);
 ArrayInitialize(LowBB,EMPTY_VALUE);
 Error = 0;
//------------------------------------------- 
 CopyBuffer(ALMAHandle,0,0,count,ALMA);
 Error = GetLastError();
 if (Error != 0)
 Print ("Error Occured while copying ALMA buffer ",Error);
//----------------------------------------------------------
 CopyBuffer(AngleMAHandle,0,0,count,AngleMA);
 Error = GetLastError();
 if (Error != 0)
 Print ("Error Occured while copying AngleMa buffer ",Error);
//-------------------------------------------------------------
 CopyBuffer(BBandsStopHandle,1,0,count,UpBB);
  Error = GetLastError();
 if (Error != 0)
 Print ("Error Occured while copying UpBB buffer ",Error);
//----------------------------------------------------------
 CopyBuffer(BBandsStopHandle,4,0,count,LowBB);
  Error = GetLastError();
 if (Error != 0)
 Print ("Error Occured while copying LowBB buffer ",Error);

I use the user defined function UpdateIndicators() to update the local array for the indicator buffers anytime I need them

long WindowsTotal = ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   for(long i=0;i<WindowsTotal;i++)                   //Runs through all available windows
     {for(int k = ChartIndicatorsTotal(0,(int)i);k>=0;k--)  //Gets all available indicators in the window
        {string shortname = ChartIndicatorName(0,(int)i,k);
         if (StringSubstr(shortname,0,5)== "Angle" || StringSubstr(shortname,0,4) == "ALMA" || StringSubstr(shortname,0,5) == "BBand" )
         {ChartIndicatorDelete(0,(int)i,shortname);
         }
        }
      
     }

If one of your indicator was already on the chart, you remove it. You have an handle to an inexistent indicator. So error 4116.

Okay, I’ll move the part that deletes old indicator copies above the section that gets indicator handles. So indicators are first deleted before iCustom calls. I trust that applies your proposed solution?

Thanks for the feedback