cannot load indicator 'Moving Average' [4002]

 

I have been troubleshooting this for hours now... I am trying to CopyBuffer the following custom indicator line:

   int testHandleICustom = iCustom(_Symbol, TFVal,  Toolbox + "iMA+ATR.ex5",
                             MAPeriodVal, MAShiftVal, MAMethodVal, AppliedPriceVal, ATRPeriodVal, ATRxPlierVal);

I get the runtime error here with [cannot load indicator 'Moving Average' [4002]] message. 

      int copiedICustom = CopyBuffer(testHandleICustom, 0, 0, buffer_sizeVal, ExtMapBuffer_IMA_mline_iCustom);

Based from the test script the testHandleICustom is != INVALID_HANDLE and I should have no problems in using the CopyBuffer function with it. 

Also the Print function from the iMA+ATR indicator was also called in the printout message window. 

   ResetLastError();
   handle_MA = iMA(NULL, PERIOD_CURRENT, InpPeriod, InpShift, InpMethod, InpAppliedPrice);

   if(handle_MA == INVALID_HANDLE) {
      Print("The iMA(1) object was not created: Error ", GetLastError());
      return INIT_FAILED;


I have no issues using the iMA+ATR as an indicator in the chart, and I can figure out what did I missed? Since I have been trying to fix this for also 8 hours now, I think I need some help with fresh eyes to review my code. I provided everything below. Any input and insight will be appreciated.  



Below is the printout messages of the test script.



Below is the test script:

// Sample testing Script
#define Toolbox "\\Indicators\\Toolbox\\"

void OnStart() {
   CIndicatorLibrary ind;
   int MAPeriodVal = 6;
   int MAShiftVal = 0;
   int ATRPeriodVal = 15;
   int buffer_sizeVal = 2;
   double ATRxPlierVal = 1.5;
   ENUM_TIMEFRAMES TFVal = PERIOD_M1;
   ENUM_MA_METHOD MAMethodVal = MODE_SMA;
   ENUM_APPLIED_PRICE AppliedPriceVal = PRICE_CLOSE;
   double ExtMapBuffer_IMA_mline_iMA[];
   double ExtMapBuffer_IMA_mline_iCustom[];


   int testHandleIMA = iMA(_Symbol, TFVal, MAPeriodVal, 0, MAMethodVal, AppliedPriceVal);

   if (testHandleIMA == INVALID_HANDLE)
      Print("Test iMA() failed: ", GetLastError());
   else {
      Print("Test iMA() success.");
      int copiedIMA = CopyBuffer(testHandleIMA, 0, 0, buffer_sizeVal, ExtMapBuffer_IMA_mline_iMA);

      if (copiedIMA <= 0) {
         // Log error message for failed copy operation
         PrintFormat("Fn: %s Ln: %d - Error code: %d - Unable to copiedIMA = %d", // For Error logging
                     __FUNCTION__, __LINE__, GetLastError(), copiedIMA);
      } else Print("ExtMapBuffer_IMA_mline_iMA: ", ExtMapBuffer_IMA_mline_iMA[1]);
   }


   int testHandleICustom = iCustom(_Symbol, TFVal,  Toolbox + "iMA+ATR.ex5",
                             MAPeriodVal, MAShiftVal, MAMethodVal, AppliedPriceVal, ATRPeriodVal, ATRxPlierVal);

   if (testHandleICustom == INVALID_HANDLE) {
      Print("Test iCustom() failed: ", GetLastError());

   } else {
      // testHandleICustom is a valid handle...
      Print("Test iCustom() success.");
      // gets the runtime error here...  
      int copiedICustom = CopyBuffer(testHandleICustom, 0, 0, buffer_sizeVal, ExtMapBuffer_IMA_mline_iCustom);

      if (copiedICustom <= 0) {
         // Log error message for failed copy operation
         PrintFormat("Fn: %s Ln: %d - Error code: %d - Unable to copiedICustom = %d", // For Error logging
                     __FUNCTION__, __LINE__, GetLastError(), copiedICustom);
      } else Print("ExtMapBuffer_IMA_mline_iCustom: ", ExtMapBuffer_IMA_mline_iCustom[1]);
   }
}


Below is the iMA+ATR.mp5 custom indicator. Its a simple Moving Average indicator with upper and lower bands of ATR value. For reference here is the code.

//+------------------------------------------------------------------+
//|                                                    iMA + ATR.mq5 |
//|                                                     Marteo Cosme |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   3
//--- plot SWMA
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot UpperATR
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_DASH
//--- plot LowerATR
//#property indicator_label3 "LowerATR"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrBlue
#property indicator_style3  STYLE_DASH

//--- input parameters
input group "iMA Settings"
input uint                 InpPeriod         =  14;            // iMA Period
input uint                 InpShift          = 0;              // iMA Shift
input ENUM_MA_METHOD       InpMethod         =  MODE_SMA;      // iMA Method
input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price
input group "ATR Settings"
input uint                 InpPeriodATR      =  15;            // ATR Period
input double               InpATRxPlier      =  1.5;           // ATR Multiplier

//--- indicator buffers
double         BufferMA[];
double         BufferATR[];
double         BufferMAline[];
double         BufferUpperATR[];
double         BufferLowerATR[];
//--- global variables
int            period_MA;
int            handle_MA;
int            period_ATR;
int            handle_ATR;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//--- set global variables
   period_MA = int(InpPeriod < 1 ? 1 : InpPeriod);
   period_ATR = int(InpPeriodATR < 1 ? 1 : InpPeriodATR);
//--- indicator buffers mapping
   SetIndexBuffer(0, BufferMAline, INDICATOR_DATA);
   SetIndexBuffer(1, BufferUpperATR, INDICATOR_DATA);
   SetIndexBuffer(2, BufferLowerATR, INDICATOR_DATA);   
   SetIndexBuffer(3, BufferATR, INDICATOR_CALCULATIONS);
   SetIndexBuffer(4, BufferMA, INDICATOR_CALCULATIONS);
   
//--- setting indicator parameters
   IndicatorSetString(INDICATOR_SHORTNAME, EnumToString(InpMethod) + "(" + (string)period_MA + ")");
   PlotIndexSetString(0, PLOT_LABEL, EnumToString(InpMethod) + "(" + (string)period_MA + ")");
   PlotIndexSetString(1, PLOT_LABEL, "upperATR" + "(" + IntegerToString(InpPeriodATR) + " x " + DoubleToString(InpATRxPlier, 1) + ")");
   PlotIndexSetString(2, PLOT_LABEL, "lowerATR" + "(" + IntegerToString(InpPeriodATR) + " x " + DoubleToString(InpATRxPlier, 1) + ")");

   IndicatorSetInteger(INDICATOR_DIGITS, Digits());
//--- setting buffer arrays as timeseries
   
   ArraySetAsSeries(BufferMAline, true);
   ArraySetAsSeries(BufferUpperATR, true);
   ArraySetAsSeries(BufferLowerATR, true);
   ArraySetAsSeries(BufferATR, true);
   ArraySetAsSeries(BufferMA, true);
   
//--- create MA's handle
   ResetLastError();
   handle_MA = iMA(NULL, PERIOD_CURRENT, InpPeriod, InpShift, InpMethod, InpAppliedPrice);

   if(handle_MA == INVALID_HANDLE) {
      // received this error handler from the test script
      Print("The iMA(1) object was not created: Error ", GetLastError());
      return INIT_FAILED;
   }
   handle_ATR = iATR(NULL, PERIOD_CURRENT, period_ATR);
   if(handle_ATR == INVALID_HANDLE) {
      Print("The iATR(1) object was not created: Error ", GetLastError());
      return INIT_FAILED;
   }
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {
//--- Checking for the minimum number of bars for calculation
   if(rates_total < period_MA) return 0;
//--- Checking and calculating the number of bars to be calculated
   int limit = rates_total - prev_calculated;
   if(limit > 1) {
      limit = rates_total - period_MA - 1;
      ArrayInitialize(BufferMA, EMPTY_VALUE);
      //ArrayInitialize(BufferMA, 0);
   }
//--- Data preparation
   int copied = 0, count = (limit == 0 ? 1 : rates_total);
   copied = CopyBuffer(handle_MA, 0, 0, count, BufferMA);
   if(copied != count) return 0;
   copied = CopyBuffer(handle_ATR, 0, 0, count, BufferATR);
   if(copied != count) return 0;
//--- Indicator calculation
   for(int i = limit; i >= 0 && !IsStopped(); i--) {
         //BufferMA[i]=(denominator!=0 ? numerator/denominator : 0);
         BufferMAline[i] = BufferMA[i];
         BufferUpperATR[i] = BufferMA[i] + (BufferATR[i] * InpATRxPlier);
         BufferLowerATR[i] = BufferMA[i] - (BufferATR[i] * InpATRxPlier);
   }

//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
 
Marteo Gonzales Cosme:

I have been troubleshooting this for hours now... I am trying to CopyBuffer the following custom indicator line:

I get the runtime error here with [cannot load indicator 'Moving Average' [4002]] message. 

Based from the test script the testHandleICustom is != INVALID_HANDLE and I should have no problems in using the CopyBuffer function with it. 

Also the Print function from the iMA+ATR indicator was also called in the printout message window. 


I have no issues using the iMA+ATR as an indicator in the chart, and I can figure out what did I missed? Since I have been trying to fix this for also 8 hours now, I think I need some help with fresh eyes to review my code. I provided everything below. Any input and insight will be appreciated.  



Below is the printout messages of the test script.



Below is the test script:


Below is the iMA+ATR.mp5 custom indicator. Its a simple Moving Average indicator with upper and lower bands of ATR value. For reference here is the code.

remove the groups from the indicator inputs

 
Lorentzos Roussos #:

remove the groups from the indicator inputs




btw... this was me 9 hours earlier... hahaha Thanks a lot!!! 


 
Marteo Gonzales Cosme #:



btw... this was me 9 hours earlier... hahaha Thanks a lot!!! 


ehehe