EA opens opposite trade immeadiately

 
extern int PeriodoEMARapida = 5;
extern int PeriodoEMALenta = 21;

int MaximoDeOrdenes = 0;
int BarsCount = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnTick()
  {
   double EMARapida = iMA(NULL,0,PeriodoEMARapida,0,1,0,0);
   double EMALenta = iMA(NULL,0,PeriodoEMALenta,0,1,0,0);
  
   //Conditions for buy                                                                        
   if(EMARapida > EMALenta && TicketCompra == 0)  
   if (OrdersTotal()==MaximoDeOrdenes)
   if (Bars > BarsCount)
                                                              //(x && y) = Si x e y son verdaderos, la expresion es verdadera
    {                                                          //(x == y) = x es igual a y    0 = No error returned 
       //Open Buy order
       TicketCompra = OrderSend(Symbol(),OP_BUY, so on...);
      
       TicketVenta = 0;     
       BarsCount = Bars;      
    }
       
   //Conditions for Sell
   if(EMARapida < EMALenta && TicketVenta == 0) 
   if (OrdersTotal()==MaximoDeOrdenes)  
   if (Bars > BarsCount)
 
    {     
      //Open Sell order
      TicketVenta = OrderSend(Symbol(),OP_SELL, so on....);
      
      TicketCompra = 0;  
      BarsCount = Bars;    
    }
    

Hi guys,

When my EA closes a trade, sometimes it opens an opposite trade immediately.

Basically, when the 5 period MA cross over the 21 period MA the EA opens a BUY position and vice versa. In certain conditions, when a trade is close, immediately the EA opens the opposite trade because the the 5 period MA was already above and the "cross" was not necessary.

The thing that I want to do is, for example, is that the 5 period MA should really cross the 21 period MA (coming from below), and then open the buy position.

Any  new suggestion is welcome

Cheers!

Files:
 
SalgadoCapital:

Hi guys,

When my EA closes a trade, sometimes it opens an opposite trade immediately.

Basically, when the 5 period MA cross over the 21 period MA the EA opens a BUY position and vice versa. In certain conditions, when a trade is close, immediately the EA opens the opposite trade because the the 5 period MA was already above and the "cross" was not necessary.

The thing that I want to do is, for example, is that the 5 period MA should really cross the 21 period MA (coming from below), and then open the buy position.

Any  new suggestion is welcome

Cheers!


   double EMARapidaLastBar = iMA(NULL,0,PeriodoEMARapida,0,1,0,0);
   double EMALentaLastBar = iMA(NULL,0,PeriodoEMALenta,0,1,0,0);
   double EMARapidaPreviousBar = iMA(NULL,0,PeriodoEMARapida,0,1,0,1); <----- 1, could be shifted for as many bar past as you wish 
   double EMALentaPreviousBar = iMA(NULL,0,PeriodoEMALenta,0,1,0,1);

if( (EMARapidaLastBar > EMALentaLastBar ) && (EMARapidaPreviousBar < EMALentaPreviousBar)) ...
 
SalgadoCapital: The thing that I want to do is, for example, is that the 5 period MA should really cross the 21 period MA (coming from below),
Don't look at bar zero, you'll get multiple cross/uncross as the bar forms.
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 and MetaTrader 4 - MQL4 programming forum
Reason: