MA Crossover EA

 
Hi, I am having trouble with my MA Crossover EA. I wanted to enter sell positions only when the price (that is set up as 1 day MA) crosses 14 days MA from above. 
   extern int FastMA = 1;
   extern int SlowMA = 14;
   extern double LotSize = 0.01;
   
   extern int ticketSell;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  }
void OnTick()
  {
   double PreviousFast = iMA(NULL,0,FastMA,0,0,0,1);
   double CurrentFast = iMA(NULL,0,FastMA,0,0,0,0);
   double PreviousSlow = iMA(NULL,0,SlowMA,0,0,0,1);
   double CurrentSlow = iMA(NULL,0,SlowMA,0,0,0,0);
  
   if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow  && OrdersTotal()==0){
      ticketSell = OrderSend(Symbol(),OP_SELL, LotSize, Bid, 3, 0,0, NULL, 0, 0, Red);
   }     
   if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow){
      OrderClose(ticketSell,LotSize,Ask,3,Red);
   }
  }

Although it does it sometimes, it also enters positions where price is crossing 14 days MA from below. 


Do you know what is wrong with it and how to fix it?

 
  1. double PreviousFast = iMA(NULL,0,FastMA,0,0,0,1);
    double CurrentFast = iMA(NULL,0,FastMA,0,0,0,0);
    double PreviousSlow = iMA(NULL,0,SlowMA,0,0,0,1);
    double CurrentSlow = iMA(NULL,0,SlowMA,0,0,0,0);
    You are looking at the forming candle, you will get multiple crosses as price fluxuates during the day. Look for the cross at end of the day.

  2.  if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow  && OrdersTotal()==0){
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
 
whroeder1:
  1. You are looking at the forming candle, you will get multiple crosses as price fluxuates during the day. Look for the cross at end of the day.

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

1. Yes, I wanted to enter immediately as the ma crosses the price at the forming candle. To avoid getting multiple crosses, I limited the orders to be sent only once per candle. I did not intend to wait for the end of the day. Is it possible to do it this way and still prevent getting entered wrong sell positions?

Reason: