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

To add comments, please log in or register
iSDK
11
iSDK  

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. :)

Vladimir Karputov
Moderator
192048
Vladimir Karputov  

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...
William Roeder
21724
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
iSDK
11
iSDK  
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;         
  }
//+------------------------------------------------------------------+
William Roeder
21724
William Roeder  
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.

To add comments, please log in or register