Cannot load indicator 'Moving Average' [4002]

Stefan Hanssen  

Hello everyone,


I am completely new at programming in mql5 and unfortunately I have some problems to build my code based on a simple moving average. 

I am basically trying to :

  • buy the position when the closed price < SMA
  • sell the position when the closed price > SMA

When I compile my code, there is no error. But when I backtest it the following error appear in the journal :

"Cannot load indicator 'Moving Average' [4002]"


Could someone tell me why I have this "input"?


Thank you a lot for your time

//input variables:

input double volumeTrade=0.1;
input int MAPeriod=10;
input double stopLoss=0;
input double takeProfit=0;


// Global variables

bool glBuyPlaced=false;
bool glSellPlaced=false;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);


// Moving average

   double ma[];
   ArraySetAsSeries(ma,true);

   int MAHandle=iMA(_Symbol,0,MAPeriod,MODE_SMA,0,PRICE_CLOSE);
   CopyBuffer(MAHandle,0,0,1,ma);




// Closing Price

   double close[];
   ArraySetAsSeries(close,true);

   CopyClose(_Symbol,0,0,1,close);


// Curent position information


   bool openPosition=PositionSelect(_Symbol);
   long positionType=PositionGetInteger(POSITION_TYPE);



   double currentVolume=0;

   if(openPosition)
      currentVolume=PositionGetDouble(POSITION_VOLUME);

//Place a buy position

   if(close[0]<ma[0] && glBuyPlaced==false &&(openPosition==false && positionType!=POSITION_TYPE_BUY))
     {

      request.action=TRADE_ACTION_DEAL;
      request.type=ORDER_TYPE_BUY;
      request.sl=0;
      request.tp=0;
      request.volume=currentVolume+volumeTrade;
      request.type_filling=ORDER_FILLING_FOK;
      request.price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      request.deviation=50;
      request.comment=_Symbol+": Volume :"+request.volume+" SL :"+request.volume+" TP :"+request.volume+" PRICE :"+request.price;

      OrderSend(request,result);

      glBuyPlaced=true;
      glSellPlaced=false;


     }


//Place a sell position


   else
      if(close[0]>ma[0] && glSellPlaced==false &&(openPosition==false && positionType!=POSITION_TYPE_SELL))
        {

         request.action=TRADE_ACTION_DEAL;
         request.type=ORDER_TYPE_SELL;
         request.sl=0;
         request.tp=0;
         request.volume=currentVolume+volumeTrade;
         request.type_filling=ORDER_FILLING_FOK;
         request.price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         request.deviation=50;
         request.comment=_Symbol+": Volume :"+request.volume+" SL :"+request.volume+" TP :"+request.volume+" PRICE :"+request.price;

         OrderSend(request,result);

         glBuyPlaced=false;
         glSellPlaced=true;
        }



  }
//+------------------------------------------------------------------+
Hao Tan  
Alain Verleyen:

You should read the documentation, specially when you are "new at programming in mql5.

You can't call iMA (or a function to get an indicator handle in general) and in the next statement use CopyBuffer(), the indicator is not ready yet. Use iMA in OnInit() and CopyBuffer() in Ontick(), as shown in the documentation example.

i have the same problem! your reply is so helpful ! thanks a lot.

Muneeb00  
Alain Verleyen #:

You should read the documentation, specially when you are "new at programming in mql5.

You can't call iMA (or a function to get an indicator handle in general) and in the next statement use CopyBuffer(), the indicator is not ready yet. Use iMA in OnInit() and CopyBuffer() in Ontick(), as shown in the documentation example.

Can You Help Me, I Have Same Error In Mq5
MOHAMED AMR MOHAMED OSAMA I ABDELWAHAB  
Alain Verleyen #:

You should read the documentation, specially when you are "new at programming in mql5.

You can't call iMA (or a function to get an indicator handle in general) and in the next statement use CopyBuffer(), the indicator is not ready yet. Use iMA in OnInit() and CopyBuffer() in Ontick(), as shown in the documentation example.

But next code working fine at my end

double getIndicator(int v)
  {
   int handle = iCustom(curSymbol,Indicator_Period,"Market\\Entry Points Pro for MT5");
//,1000,true);
// ,false,false,500,false,"09:00","23:59",false
//,30,255,3329330,14772545,9,true,true,true,"alert2.wav"
   double myArray[];
   int copy=CopyBuffer(handle,v,0,1,myArray);
   return (double)myArray[0];
  }
Vladimir Karputov  
MOHAMED AMR MOHAMED OSAMA I ABDELWAHAB # :

But next code working fine at my end

This code is a gross mistake! According to the MQL5 style, the indicator handle MUST be received ONCE and this must be done in OnInit!!!

douglas14  
Vladimir Karputov #:
MQL5 style, the indicato

This is very bad. Because if I am monitoring several symbols, like, 20 symbols, I am making a function like:


double arrayADXmainline[];
double arrayPlusADXline[];
double arrayMinusADXline[];

double ADX(string symbol, ENUM_TIMEFRAMES timeframe, int comprimento, int modo, int posicao) //ADX(_Symbol,_Period,4,4,0)
{



int adxHandle=iADX(symbol,timeframe,8);

ArrayResize(arrayADXmainline,comprimento);
ArrayResize(arrayPlusADXline,comprimento);
ArrayResize(arrayMinusADXline,comprimento);

ArraySetAsSeries(arrayADXmainline,true);
ArraySetAsSeries(arrayPlusADXline,true);
ArraySetAsSeries(arrayMinusADXline,true);

CopyBuffer(adxHandle,MAIN_LINE,0,comprimento,arrayADXmainline);
CopyBuffer(adxHandle,PLUSDI_LINE,0,comprimento,arrayPlusADXline);
CopyBuffer(adxHandle,MINUSDI_LINE,0,comprimento,arrayMinusADXline);

int sign=1;
double variavelRetorno=0;

if(modo==1)
{
   if(arrayPlusADXline[posicao]<arrayMinusADXline[posicao])
      {
      sign=-1;
      return sign;
      }
   else
      {
      sign=1;
      return sign;
      }
}//if modo == 1  

if(modo==2)
{
      return arrayPlusADXline[posicao];
}//if modo == 2  

if(modo==3)
{
      return arrayMinusADXline[posicao];
}//if modo == 3    

if(modo==4)
{
      Print("TESTE1");
      variavelRetorno = arrayPlusADXline[0]-arrayMinusADXline[0];
      Print("TESTE");
      return variavelRetorno;
      
}//if modo == 4  

if(modo==5)
{

   if(arrayPlusADXline[1]-arrayMinusADXline[1]>0)
      if(arrayPlusADXline[2]-arrayMinusADXline[2]<0)
         return 1;

   if(arrayPlusADXline[1]-arrayMinusADXline[1]<0)
      if(arrayPlusADXline[2]-arrayMinusADXline[2]>0)
         return 1;    
         
   return 0;              
}//if modo == 5  

return 0;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

How I am supposed to split the code between onInit() and onTick() function ?

By the way, because what I want is just to monitor, I am not even using ontick(). I call the funcion I did in ontimer() every 60 seconds.

Reason: