Improving a function that " Sends an alert when specific condition is met"

 

I  am facing a difficulty in sending an alert mail when a certain condition is met.

I want it as an alert mail for the manual trading so that I am with the trend . This mail has to be only once when MA50 is above MA200 for sell position and viceversa.

I have done workaround with the Ordermodify ().

Can anyone suggest a modification to the Code?


////////////////////////////////////////////////////////////////////////////////
void check_for_Position()
     {
     
     
           double MA200_previous = iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,1);
           double MA50_previous = iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,1);
     
      {
              for (int i = OrdersTotal(); i >=0; i--) 
              {
      
          OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
          {
            double TP = OrderTakeProfit();
            double STOP = OrderStopLoss();
         if (OrderSymbol() == Symbol())
           {
             if (  (OrderType() == OP_BUY) && ( MA50_previous < MA200_previous ) && (OrderTakeProfit()!=100) )
          { 
             bool Buy1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),100,0,CLR_NONE);
             if(!Buy1) 
               Print("Error in OrderModify. Error code=",GetLastError()); 
            else 
               Print("Order modified successfully.");  
              SendMail(Symbol(), "Time to close the Buy_5M" + "@" + Ask ); 
            // OrderClose(OrderTicket(),OrderLots(),Bid,30,Red);
          }
          
           }
          
         if (OrderSymbol() == Symbol())
          {
          if ( (OrderType() == OP_SELL) &&  ( MA50_previous > MA200_previous )&& (OrderTakeProfit()!=0) )              
           { 
              //  OrderClose(OrderTicket(),OrderLots(),Bid,30,Red);
               bool Sell1= OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),0,0,CLR_NONE);
                  if(!Sell1) 
               Print("Error in OrderModify. Error code=",GetLastError(),STOP); 
            else 
               Print("Order modified successfully."); 
               SendMail(Symbol(), "Time to close the Sell_5M" + "@" + Ask );
                 
           }
            }
            
            }
          
              }
              
               
        }       
              
}
///////////////////////////////////////////////////////////////////////////////////////
 

I don't know where you got this code, but that code is a mess...

Here is a solution:

#include <Arrays\\ArrayLong.mqh>

CArrayLong  ordersSent;

void check_for_Position(const string symbol)
{
   double MA200_previous = iMA(symbol,0,200,0,MODE_EMA,PRICE_CLOSE,1);                      //200 EMA
   double MA50_previous = iMA(symbol,0,50,0,MODE_EMA,PRICE_CLOSE,1);                        //50 EMA
  
   //--- Loop trough running orders
   for(int i = OrdersTotal()-1; i >= 0; i--)                                           
   {  
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { continue; }                       //Select Order
      if(OrderSymbol() != Symbol())                   { continue; }                       //Only current Symbol
      
      if(OrderType() == OP_BUY && MA50_previous < MA200_previous)                         //Buy order check EMA cross
      {
         if(ordersSent.Search(OrderTicket() != -1))   { continue; }                       //Check if already sent
         if(SendMail("Close " + Symbol() + " Buy", 
                     "Time to close the Buy_5M @" + DoubleToString(Bid, (int)MarketInfo(symbol, MODE_DIGITS))))   //Send Mail
         {
            ordersSent.Add(OrderTicket());
         }
      }
      else if(OrderType() == OP_SELL && MA50_previous > MA200_previous)                   //Sell order check EMA cross
      {
         if(ordersSent.Search(OrderTicket() != -1))   { continue; }                       //Check if already sent
         if(SendMail("Close " + Symbol() + " Sell", 
                     "Time to close the Sell_5M @" + DoubleToString(Bid, (int)MarketInfo(symbol, MODE_DIGITS)))) //Send Mail
         {
            ordersSent.Add(OrderTicket());
         }
      }    
   }
   
   //--- Delet closed orders from list
   for(int i = ordersSent.Total()-1; i >= 0; i--)
   {
      if(!OrderSelect((int)ordersSent.At(i), SELECT_BY_TICKET, MODE_TRADES))
      {
         ordersSent.Delete(i);
      }
   }
      
}//End of check_for_Position

CODE IS NOT TESTED!


It's just a quick solution! 
Code can be improved more. (f.e. search for closed orders)