order close function modification

 

I need a profit order close function for a EA, but this one will close all working orders

anyone can help to allow close for symbol and magic only?

if (AccountProfit()>= My_Money_Profit_Target)
   {
    for(i=OrdersTotal()-1;i>=0;i--)
       {
       OrderSelect(i, SELECT_BY_POS);
       int type   = OrderType();
               
        bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), g_slippage_172, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), g_slippage_172, Red );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
}
 
fulltilt:
I need a profit order close function for a EA, but this one will close all working orders
anyone can help to allow close for symbol and magic only?
  1. All Only
        for(i=OrdersTotal()-1;i>=0;i--)
           {
           OrderSelect(i, SELECT_BY_POS);
        for(i=OrdersTotal()-1;i>=0;i--)
          if (OrderSelect(i, SELECT_BY_POS)
          &&  OrderMagicNumber() == magic.number
          &&  OrderSymbol()      == Symbol()
          ){

    Was that so hard?

    What are Function return values ? How do I use them ? - MQL4 forum

  2. Also if you use OrderClosePrice() you do not have to differentiate Buy/Sell and Bid/Ask in the OrderClose
  3.  MarketInfo(OrderSymbol(), MODE_BID)
    Why use a function call when you can use Bid/Ask or OrderClosePrice()
 
many thanks, exactly what I was looking for ;-)
Reason: