MQL5 Expert Advisor won't laod into Tester because of 4 if clauses ? (Please Help)

 

Hi,

I have a problem with my EA.

It's calculateting the 21,13 and 8 eMA and it should give a Buy signal if the 8 - 13 - 21 are on top of each other in this sequence and a Sell signal if they are beneath each other.
I hope that you get what i am trying to say.

The thing is it works if i check for a Buy signal or a Sell signal individually, but when i try to check for both it wont load into the Tester.

Error messages:

MQL5    cannot open file 'Test.ex5'
Experts    loading of Test failed

Here's my code:

//This works.
void OnTick()
  {
  string signal="";
  double myMovingAverafeArray1[],myMovingAverafeArray2[],myMovingAverafeArray3[];
  int movingAverageDefinition1 = iMA(_Symbol,_Period,8,0,MODE_EMA,PRICE_CLOSE);
  int movingAverageDefinition2 = iMA(_Symbol,_Period,13,0,MODE_EMA,PRICE_CLOSE);
  int movingAverageDefinition3 = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE);
  ArraySetAsSeries(myMovingAverafeArray1,true);
  ArraySetAsSeries(myMovingAverafeArray2,true);
  ArraySetAsSeries(myMovingAverafeArray3,true);
  CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverafeArray1);
  CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverafeArray2);
  CopyBuffer(movingAverageDefinition3,0,0,3,myMovingAverafeArray3);
  double myMovingAverageValue1 = myMovingAverafeArray1[0];
  double myMovingAverageValue2 = myMovingAverafeArray2[0];
  double myMovingAverageValue3 = myMovingAverafeArray3[0];
  if (myMovingAverageValue1 < myMovingAverageValue2)
  {
   if (myMovingAverageValue2 < myMovingAverageValue3)
   {
      signal = "SELL";
   }
  }

   Comment("Signal: ", signal,"\n","myMovingAvarageValue1: ", myMovingAverageValue1,"\n","myMovingAvarageValue2: ", myMovingAverageValue2,"\n","myMovingAvarageValue3: ", myMovingAverageValue3,"\n");
   
  }
//This doesn't work.
void OnTick()
  {
  string signal="";
  double myMovingAverafeArray1[],myMovingAverafeArray2[],myMovingAverafeArray3[];
  int movingAverageDefinition1 = iMA(_Symbol,_Period,8,0,MODE_EMA,PRICE_CLOSE);
  int movingAverageDefinition2 = iMA(_Symbol,_Period,13,0,MODE_EMA,PRICE_CLOSE);
  int movingAverageDefinition3 = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE);
  ArraySetAsSeries(myMovingAverafeArray1,true);
  ArraySetAsSeries(myMovingAverafeArray2,true);
  ArraySetAsSeries(myMovingAverafeArray3,true);
  CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverafeArray1);
  CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverafeArray2);
  CopyBuffer(movingAverageDefinition3,0,0,3,myMovingAverafeArray3);
  double myMovingAverageValue1 = myMovingAverafeArray1[0];
  double myMovingAverageValue2 = myMovingAverafeArray2[0];
  double myMovingAverageValue3 = myMovingAverafeArray3[0];
  if (myMovingAverageValue1 < myMovingAverageValue2)
  {
   if (myMovingAverageValue2 < myMovingAverageValue3)
   {
      signal = "SELL";
   }
  }

  if (myMovingAverageValue1 > myMovingAverageValue2)
  {
   if (myMovingAverageValue2 > myMovingAverageValue3)
   {
      signal = "BUY"
   }
  }

   Comment("Signal: ", signal,"\n","myMovingAvarageValue1: ", myMovingAverageValue1,"\n","myMovingAvarageValue2: ", myMovingAverageValue2,"\n","myMovingAvarageValue3: ", myMovingAverageValue3,"\n");
   
  }


Thanks for your time and help. :)

 

The biggest mistake on your MQL5 code:

void OnTick ()
  {
   string signal= "" ;
   double myMovingAverafeArray1[],myMovingAverafeArray2[],myMovingAverafeArray3[];
   int movingAverageDefinition1 = iMA ( _Symbol , _Period , 8 , 0 , MODE_EMA , PRICE_CLOSE );
   int movingAverageDefinition2 = iMA ( _Symbol , _Period , 13 , 0 , MODE_EMA , PRICE_CLOSE );
   int movingAverageDefinition3 = iMA ( _Symbol , _Period , 21 , 0 , MODE_EMA , PRICE_CLOSE );

You on EACH TICK create several indicator handles!


The correct example is from the link: iMA

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
//|                                                     Demo_iMA.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                              https://www.mql5.com | "The method of creation of the handle is set through the 'type' parameter (function type...
 
Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
          How to call indicators in MQL5 - MQL5 Articles 12 March 2010
 
William Roeder:
Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
          How to call indicators in MQL5 - MQL5 Articles 12 March 2010

I'm getting really frustrated at this point.

So i put the iMA handler in OnInit() and it still doesnt really work. This time it tells its something with the Copybuffer function. (undeclared identifier)

//+------------------------------------------------------------------+
//|                                                       oninit.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
//---
   int EMA8 = iMA(_Symbol,_Period,8,0,MODE_EMA,PRICE_CLOSE);
   int EMA13 = iMA(_Symbol,_Period,13,0,MODE_EMA,PRICE_CLOSE);
   int EMA21 = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE);

//---
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double _ma1[],_ma2[],_ma3[];

   ArraySetAsSeries(_ma1, true);
   ArraySetAsSeries(_ma2, true);
   ArraySetAsSeries(_ma3, true);


   if(CopyBuffer(EMA8,0,0,5,_ma1)!=5) return;         
   if(CopyBuffer(EMA13,2,0,5,_ma2)!=5) return;         
   if(CopyBuffer(EMA21,3,0,5,_ma3)!=5) return;         
  }
//+------------------------------------------------------------------+
 
iSDK: So i put the iMA handler in OnInit() and it still doesnt really work. This time it tells its something with the Copybuffer function. (undeclared identifier)

Of course, it does. I said "you get that in OnInit." You did, and then through the values away.

In CopyBuffer you use an undeclared variable. So of course, that doesn't compile.

Put your handler variables outside of OnInit so you can pass them to CopyBuffer. Assign to the variables in OnInit.

 
iSDK :

I'm getting really frustrated at this point.

So i put the iMA handler in OnInit() and it still doesnt really work. This time it tells its something with the Copybuffer function. (undeclared identifier)

We read the help:

Call by the first position and the number of required elements

int  CopyBuffer(
   int       indicator_handle,     // indicator handle
   int       buffer_num,           // indicator buffer number
   int       start_pos,            // start position
   int       count,                // amount to copy
   double    buffer[]              // target array to copy
   );


Code:

//+------------------------------------------------------------------+
//|                                                       oninit.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
int handle_iMA8;
int handle_iMA13;
int handle_iMA21;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   handle_iMA8 = iMA(Symbol(),Period(),8,0,MODE_EMA,PRICE_CLOSE);
//--- if the handle is not created
   if(handle_iMA8==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   handle_iMA13 = iMA(Symbol(),Period(),13,0,MODE_EMA,PRICE_CLOSE);
//--- if the handle is not created
   if(handle_iMA13==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   handle_iMA21 = iMA(Symbol(),Period(),21,0,MODE_EMA,PRICE_CLOSE);
//--- if the handle is not created
   if(handle_iMA21==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double array_ima8[],array_ima13[],array_ima21[];
   ArraySetAsSeries(array_ima8, true);
   ArraySetAsSeries(array_ima13, true);
   ArraySetAsSeries(array_ima21, true);

   int start_pos=0,count=6;

   if(CopyBuffer(handle_iMA8,0,0,count,array_ima8)!=count)
      return;
   if(CopyBuffer(handle_iMA13,0,0,count,array_ima13)!=count)
      return;
   if(CopyBuffer(handle_iMA21,0,0,count,array_ima21)!=count)
      return;
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov:

We read the help:

Call by the first position and the number of required elements


Code:

Cool. So now the EMAs work, but its still very broken. My if clauses still dont seem to work. Also, why do you set int start_pos and count on the Ontick event handler and at the top where the int handles for the EMAs. These value are not suppose to change ontick, or am i missing out something ?

However, heres my code. Everything beyond the OnTick function is identical to your example:

void OnTick()
  {
//----
   double array_ima50[],array_ima100[],array_ima150[];
   ArraySetAsSeries(array_ima50, true);
   ArraySetAsSeries(array_ima100, true);
   ArraySetAsSeries(array_ima150, true);

   int start_pos=0,count=6;

   if(CopyBuffer(handle_iMA50,0,0,count,array_ima50)!=count){
   return;
   }
      
   if(CopyBuffer(handle_iMA100,0,0,count,array_ima100)!=count){
   return;
   }

   if(CopyBuffer(handle_iMA150,0,0,count,array_ima150)!=count){
   return;
   }

  if (array_ima50[0] < array_ima100[0]){
  if (array_ima100[0] < array_ima150[0]){
  signal = "SELL";
  } 
  }
  if (array_ima50[0] > array_ima100[0]) { 
  if (array_ima100[0] > array_ima150[0]) {
  signal = "BUY";
  }
  }
  Comment("Signal:", signal );
  }
  
 
What is the meaning of "count = 6" in your code? CopyBuffer() returns an integer which represents the number of elements copied (thus the length of the returned array). You only want to use the arrays if they have a length not equal to 6?
Reason: