Close MA Cross, can anyone help me for my EA code please?

 
Hello everyone, I would like my EA to close the orders at CROSSING moving averages, simply.
But I can not find the "formula" that can perform this function.
However, I tried a lot of solutions, such as: if (Ask <= (ma5 (1) <ma8 (1) && ma5 (0)> ma8 (0))) ..... CloseAll (); etc ... and many others,
but it gives nothing. At each modification, the compiler tells me: 0 errors / 0 warning! But on the test, nothing happens.
The result is always the same, as if I did not add anything or write in the code.
Otherwise, the EA works perfectly, it executes the Buy / Sell orders, closes the TP and SL, without problem.
I probably forget about some procedure or step.
Can you help me find this bug please?

I thank you in advance.


input string  EAsettings = "[EA settings]";    //Paramètres
input double  lots = 0.1;                      //Lot size
input double  TakeProfit = 20;                 //Take profit 
input double  StopLoss = 0;                    //SoptLoss
input double  distance= 0;                     //X Pips (Distance)
input int     MagicNumber = 55545;             //Magic Nb
input string  EA_Comment = "";                 // ~~~~
//-------------
input string  Rsi  = "RSI Settings";           //RSI
input int     rsi_period = 14;                 //Rsi period
input ENUM_APPLIED_PRICE  rsi_applied_price=PRICE_CLOSE;//~~
//-------------
input string  Ma1 = "[===MA1===]";             //Fast MA
input int     ma_period1 = 5;                  //MA 1 period
input  ENUM_MA_METHOD ma_type1 = MODE_EMA;     //MA 1 type
//-------------
input string  Ma2 = "[===MA2===]";             //Low MA
input int     ma_period2 = 8;                  //MA 2 period
input ENUM_MA_METHOD ma_type2 = MODE_EMA;      //MA 2 type

datetime time;
double _point=1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   string _sym=Symbol();
   double _digits=MarketInfo(_sym,MODE_DIGITS);
   if(_digits==5||_digits==3) _point=1/MathPow(10,(_digits-1));
   if(_digits==4||_digits==2) _point=1/MathPow(10,(_digits));
   if(_digits==1) _point=0.1;
   EventSetTimer(60);
   time=Time[0];
   
    { return (0); }

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
  }
  
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(   time==Time[0])return;
   time=Time[0];
   
//-----Fonctions Achat et Vente ----------------
   if(OrdersTotalT(OP_BUY)==0 && ma1(1)>ma2(1) && Ask>ma1(0)+distance*_point && rsi(1)>=50) SendOrder(OP_BUY);
   if(OrdersTotalT(OP_SELL)==0 && ma1(1)<ma2(1) && Bid<ma1(0)-distance*_point && rsi(1)<50) SendOrder(OP_SELL);
   
//-------- Fonction de fermeture d'ordres au croisement des moyennes mobiles --------------   
   double   Cr1 = (ma1(1)<ma2(1)&&ma1(0)>ma2(0));  //(buy)
   double   Cr2 = (ma1(1)>ma2(1)&&ma1(0)<ma2(0));  //(sell)
   
   if (Ask == Cr1)   CloseAll();
   if (Bid == Cr2)   CloseAll();
//-----------------------------------------------------------------------------------------  

  } 
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

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

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool SendOrder(int type,double _price=0)
  {
   while(IsTradeContextBusy());           
   int ticket=-1;                         
   double SL,TP; 
 
   if(type==OP_BUY)
     {    
      if(StopLoss==0) {SL=0;}else{SL=Ask-StopLoss*_point;}
      if(TakeProfit==0) {TP=0;} else {TP=Ask+TakeProfit*_point;}
      ticket=OrderSend(Symbol(),OP_BUY,NormalizeLots(LotManage(),Symbol()),Ask,3,SL,TP,EA_Comment,MagicNumber,0,clrBlue);
     }
   if(type==OP_SELL)
     {
      if(StopLoss==0){SL=0;}else{SL=Bid+StopLoss*_point;}
      if(TakeProfit==0) {TP=0;} else {TP=Bid-TakeProfit*_point;}
      ticket=OrderSend(Symbol(),OP_SELL,NormalizeLots(LotManage(),Symbol()),Bid,3,SL,TP,EA_Comment,MagicNumber,0,clrRed);
     }
   if(ticket<0)
     {
      Print("OrderSend  failed with error #",GetLastError());
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double LotManage()
  {
   return (NormalizeDouble(lots,2));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double NormalizeLots(double _lots,string pair="")
  {
   if(pair=="") pair=Symbol();
   double  lotStep=MarketInfo(pair,MODE_LOTSTEP),
   minLot=MarketInfo(pair,MODE_MINLOT);
   _lots=MathRound(_lots/lotStep)*lotStep;
   if(_lots<MarketInfo(pair,MODE_MINLOT)) _lots=MarketInfo(pair,MODE_MINLOT);
   if(_lots>MarketInfo(pair,MODE_MAXLOT)) _lots=MarketInfo(pair,MODE_MAXLOT);
   return(_lots);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double rsi(int _shift)
    { return(iRSI(NULL,0,rsi_period,rsi_applied_price,_shift)); }

double ma1(int _shift=0)
    { return(iMA(NULL,0,ma_period1,0,ma_type1,PRICE_CLOSE,_shift));}
    
double ma2(int _shift=0)
    { return(iMA(NULL,0,ma_period2,0,ma_type2,PRICE_CLOSE,_shift));}
    
//+------------------------------------------------------------------+
//                                                                   |
//-------------------------------------------------------------------+   

int OrdersTotalT(int _type)
  {
   int _total=0;
   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {

      bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==_type)
        {
         _total++;
        }
     }
   return(_total);
  }
//........................................................................................................
void CloseOrders(int type)
  {
   bool result=false;
   int count=0,total=OrdersTotal();
   for(count=total-1; count>=0; count--)
     {
      if(OrderSelect(count,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==type)
           {
            if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE);
            else result=OrderDelete(OrderTicket());

           }
     }
  }
//......................................................................................................
void CloseAll()
  {
   bool result=false;
   for(int _count=OrdersTotal()-1; _count>=0; _count--)
     {
      RefreshRates();
      if(OrderSelect(_count,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
           {
            if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrYellow);
           }
        }
     }
  } 
//........................................................................................................



 
   double   Cr1 = (ma1(1)<ma2(1)&&ma1(0)>ma2(0));  //(buy)
   double   Cr2 = (ma1(1)>ma2(1)&&ma1(0)<ma2(0));  //(sell)
   
   if (Ask == Cr1)   CloseAll();
   if (Bid == Cr2)   CloseAll();
  1. True is one and false is zero. So Cr1/2 is either zero or one. When will price ever be exactly that? Why are you assigning a boolean to a double?

    Cr1 is meaningless.

    This is why I say: You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
    Had you used a meaningful name like hasCrossedUp then your if statement would have made no sense.

    Doubles are rarely equal.
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. if(OrderType()<2) result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrYellow);

    Don't hard code numbers, use the proper constants.

    You must RefreshRates after server calls (before the next select) when processing multiple orders.

    Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    ticket=OrderSend(Symbol(),OP_BUY,NormalizeLots(LotManage(),Symbol()),Ask,3,SL,TP,EA_Comment,MagicNumber,0,clrBlue);

  3. double LotManage()
      {
       return (NormalizeDouble(lots,2));
      }
    Why do you have a NormalizeLots function and then not use it?