Two simple errors (My first EA)

 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   int crosses(){
         
      double current_ma = iMA("EURUSD",PERIOD_M1,30,0,MODE_EMA,PRICE_CLOSE,0);
      double previous_ma = iMA("EURUSD",PERIOD_M1,30,0,MODE_EMA,PRICE_CLOSE,1);
   
      double current_price_close = Close[0];
      double previous_price_close = Close[1];
   
      double trend_ma = iMA("EURUSD",PERIOD_M1,200,0,MODE_EMA,PRICE_CLOSE,0);
   
      if (previous_price_close<previous_ma && current_price_close>current_ma && current_price_close>trend_ma)
         return(
                  OrderSend("EURUSD",OP_BUY,0.05,Ask,3,0,0,NULL,0,0,clrGreen)
               );
      
      if (previous_price_close>previous_ma && current_price_close<current_ma && current_price_close<trend_ma)
         return(
                  OrderSend("EURUSD",OP_SELL,0.05,Bid,3,0,0,NULL,0,0,clrRed)
               );    
   }
  
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  
  }
//+------------------------------------------------------------------+


Hello, there! I need some help regarding my first EA. I may look stupid, but I keep getting these errors:

  • 'crosses' - function can be declared only in the global scope    Cruce media precio. Filtro de tendencia (EMA200).mq4    35    8
  • '}' - not all control paths return a value    Cruce media precio. Filtro de tendencia (EMA200).mq4    54    4

Could someone tell me where the problem is? How could I solve them?

Thank you very much in advance. Hopefully one day, I will be able to programm the right way, and I can help others instead of disturb them ;)

 
Nickrich:
  • 'crosses' - function can be declared only in the global scope    Cruce media precio. Filtro de tendencia (EMA200).mq4    35    8
  • '}' - not all control paths return a value    Cruce media precio. Filtro de tendencia (EMA200).mq4    54    4
  1. void OnTick()
      {
    //---
    
       int crosses(){
    Move your crosses function outside of the OnTick.
  2.    int crosses(){
        :
          if (previous_price_close>previous_ma ...)
             return(...);    
    // <<<<What are you returning here?
       }

 

Thank you very much for your quick answer, WHRoeder. Here is the result of correcting those mistakes. What I would like it to open a long position when the price crosses the EMA(30) upwards AND the price is over the EMA(200); and to open a short position when the price crosses the EMA(30) downwards AND the price is over the EMA(200). I have activeted this EA in Metatrader (EUR/USD 1M), I've also activated "AutoTrading". I get no errors compiling and no errors in Metatrader, but it does not open any position, although these conditions happen. Could you tell me, what do I still have to change or make?


Thank you very much in advance.


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  }
//---

   int crosses(){
         
      double current_ma = iMA("EURUSD",PERIOD_M1,30,0,MODE_EMA,PRICE_CLOSE,0);
      double previous_ma = iMA("EURUSD",PERIOD_M1,30,0,MODE_EMA,PRICE_CLOSE,1);                                      
   
      double current_price_close = Close[0]; 
      double previous_price_close = Close[1];                                                                        
   
      double trend_ma = iMA("EURUSD",PERIOD_M1,200,0,MODE_EMA,PRICE_CLOSE,0);
   
      if (previous_price_close<previous_ma && current_price_close>current_ma && current_price_close>trend_ma)   
         {
         OrderSend("EURUSD",OP_BUY,0.05,Ask,3,0,0,NULL,0,0,clrGreen);                                          
         }
         return(NULL);
      
      if (previous_price_close>previous_ma && current_price_close<current_ma && current_price_close<trend_ma)      
         {
         OrderSend("EURUSD",OP_SELL,0.05,Bid,3,0,0,NULL,0,0,clrRed);                                           
         }
         return(NULL);    
      } 
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  
  }
//+------------------------------------------------------------------+
 

The function has to be called before it can do anything

You have nothing in OnTick 

 

What about now? I have changed the name of "crosses()" to "price_ema30_trend()", in case you find it strange.

Thank you very much for your help. I think that I should have started practising earlier.

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
    price_ema30_trend();
  }
//---

   int price_ema30_trend(){
         
      double current_ma = iMA("EURUSD",PERIOD_M1,30,0,MODE_EMA,PRICE_CLOSE,0);                                      
      double previous_ma = iMA("EURUSD",PERIOD_M1,30,0,MODE_EMA,PRICE_CLOSE,1);                                  
   
      double current_price_close = Close[0];                                                                     
      double previous_price_close = Close[1];                                                                      
   
      double trend_ma = iMA("EURUSD",PERIOD_M1,200,0,MODE_EMA,PRICE_CLOSE,0);                                        
   
      if (previous_price_close<previous_ma && current_price_close>current_ma && current_price_close>trend_ma)        
         {
         OrderSend("EURUSD",OP_BUY,0.05,Ask,3,0,0,NULL,0,0,clrGreen);                                                
         Alert("Open Buy");
         }
         return(EMPTY_VALUE);
      
      if (previous_price_close>previous_ma && current_price_close<current_ma && current_price_close<trend_ma)        
         {
         OrderSend("EURUSD",OP_SELL,0.05,Bid,3,0,0,NULL,0,0,clrRed);                                                
         Alert("Open Sell");
         }
         return(EMPTY_VALUE);  
      } 
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  
  }
//+------------------------------------------------------------------+
 
      if (previous_price_close<previous_ma && current_price_close>current_ma && current_price_close>trend_ma)   
         {
         OrderSend("EURUSD",OP_BUY,0.05,Ask,3,0,0,NULL,0,0,clrGreen);                                          
  1. Check your return codes (OrderSend) and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. If that doesn't work then print your variables used in the if and find out why.
  3. If your chart isn't exactly "EURUSD" then it can't work. Broker's use a variety of naming patterns: EURUSD, EURUSDm, EURUSDi, "EURUSD.", "EURUSD..", "EUR.USD", "EUR/USD", "EURUSD.stp", EURUSDct, "EURUSD.G", "EURUSD+", and EURUSDpro at least. Don't hard code things; just use the predefined _Symbol.
 

I have repair some of the problems. Now it opens a new trade everytime any of the if conditions occur. The problem is, that when this conditions take place, the do for a period of time (the duration of a chart bar), and therefore it keeps opening positions every tick during this time. How could I avoid that?

Moreover, I haven't been able to achieve that it closes trades. Is it because of the "loop" that takes place? Or is there any other problem?

input int ShortEMA = 30;
input int LongEMA = 200;
input double LotSize = 0.05;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  price_ema30_trend();
  }
  
//+------------------------------------------------------------------+
//| Price-EMA30 Cross. EMA200 Filter                                 |
//+------------------------------------------------------------------+
   int price_ema30_trend(){
//----Variables Declaration----------------------------------------------------------------------------------
      double current_ma = NormalizeDouble(iMA(Symbol(),Period(),ShortEMA,0,MODE_EMA,PRICE_CLOSE,1),5);                                      
      double previous_ma = NormalizeDouble(iMA(Symbol(),Period(),ShortEMA,0,MODE_EMA,PRICE_CLOSE,2),5);                                  
    
      double current_trend_ma = NormalizeDouble(iMA(Symbol(),Period(),LongEMA,0,MODE_EMA,PRICE_CLOSE,1),5);
      double previous_trend_ma = NormalizeDouble(iMA(Symbol(),Period(),LongEMA,0,MODE_EMA,PRICE_CLOSE,2),5);     
      
      int buy_order_pet;
      int sell_order_pet;
//----Order Openings-----------------------------------------------------------------------------------------   
      if (Open[1]<current_ma && Close[1]>current_ma && Close[1]>current_trend_ma && buy_order_pet<1)        
         {
         buy_order_pet = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,"Price Cross Upwards",0,0,clrGreen);
         Alert("Open Buy",buy_order_pet);
         }
         return(true);
      
      if (Open[1]>current_ma && Close[1]<current_ma && Close[1]<current_trend_ma && sell_order_pet<1)       
         {
         sell_order_pet = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,0,0,"Price Cross Downwards",0,0,clrRed);
         Alert("Open Sell",sell_order_pet);
         } 
         return(true);
//----Order Closing------------------------------------------------------------------------------------------ 
      bool cross_against_buy = Open[1]>current_ma && Close[1]<current_ma;
      bool cross_against_sell = Open[1]<current_ma && Close[1]>current_ma;
      
      bool cross_emas_upwards = previous_ma<=previous_trend_ma && current_ma>current_trend_ma;
      bool cross_emas_downwards = previous_ma>=previous_trend_ma && current_ma<current_trend_ma;
      
      bool close_buy_order_pet;
      bool close_sell_order_pet;
            
      if (cross_against_buy==true || cross_emas_downwards==true)
         {
         close_buy_order_pet = OrderClose(buy_order_pet,LotSize,Bid,3,clrGray);
         Alert("Close Buy",buy_order_pet);
         buy_order_pet = 0;
         }
         return(false);
         
      if (cross_against_sell==true || cross_emas_upwards==true)
         {
         close_sell_order_pet = OrderClose(sell_order_pet,LotSize,Ask,3,clrGray);
         Alert("Close Sell",sell_order_pet);
         sell_order_pet = 0;
         }
         return(false);
      }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  
  }
//+------------------------------------------------------------------+

Thank you very much in advance. Have a nice day.

 
Nickrich:

therefore it keeps opening positions every tick during this time. How could I avoid that?

Moreover, I haven't been able to achieve that it closes trades.

  1. Check or remember if you have open orders. (OrderSelect loop.)
    In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  2. OrderClose(buy_order_pet,LotSize,Bid,3,clrGray);
    If you don't open a trade that tick, then your ticket number has been lost and you can't close it. See #1
Reason: