Copy Indicator buffer error 4116

 

I have an EA written in MQL5 the EA depends on data from four indicators, sometimes it copies the data correctly most times I get an error 4116, as a result of this error trades that depend on the value of the indicator are closed because of the ridiculous values that are posted to the indicator array.

Here is the code 

int OnInit()
{
   ArraySetAsSeries(AngleMA,true);
   ArraySetAsSeries(ALMA,true);
   ArraySetAsSeries(Close,true);
   ArraySetAsSeries(Open,true);
   ArraySetAsSeries(High,true);
   ArraySetAsSeries(Low,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);
   Error = GetLastError();
   ChartIndicatorAdd(0,0,ALMAHandle);
   Error = GetLastError();
   ChartIndicatorAdd(0,(int)WindowsTotal,AngleMAHandle);
   Error = GetLastError();
  // Alert ("Error is ",Error);
//---
   ChartRedraw(0);
   return(INIT_SUCCEEDED);
  }

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

Some of the values I get for the indicator ALMA is 22919.31303361931,  22919.42770739924.

Whereas the value is supposed to be in the range of 1.18000 - 1.19000. 

 
holocast:

I have an EA written in MQL5 the EA depends on data from four indicators, sometimes it copies the data correctly most times I get an error 4116, as a result of this error trades that depend on the value of the indicator are closed because of the ridiculous values that are posted to the indicator array.

Here is the code 

Also this EA works perfectly in strategy tester but when it's run on a live account trades open and close in seconds
Reason: