Best way to filter Orders with Symbol and Magic Number - page 2

 
William Roeder #:
Thank-you for proving my point. It does reject all orders.

I was using OrderType() > OP_SELL on my code but @Fernando Carreiro told him it is a bad practices in this post : https://www.mql5.com/en/forum/462038 . So, i changed to if(OrderType() != OP_BUY && OrderType() != OP_SELL) but now your saying it wrong approach than what is the correct approach.


OLD Code (OrderType() > OP_SELL) :

void CloseOpenAndPendingTrades(int trade_close_magic)
  {
   for(int pos_0 = OrdersTotal() - 1; pos_0 >= 0; pos_0--)
     {
      OrderSelect(pos_0, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != trade_close_magic)
         continue;

      if(OrderType() > OP_SELL)
         OrderDelete(OrderTicket());
      else
         OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, CLR_NONE);
     }
  }

double CalculateTradeFloating(int CalculateTradeFloating_Magic)
  {
   double CalculateTradeFloating_Value = 0;
   for(int CalculateTradeFloating_i = 0; CalculateTradeFloating_i < OrdersTotal(); CalculateTradeFloating_i++)
     {
      OrderSelect(CalculateTradeFloating_i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderType() > OP_SELL)
         continue;
      if(CalculateTradeFloating_Magic == OrderMagicNumber())
         CalculateTradeFloating_Value += OrderProfit() + OrderSwap() + OrderCommission();
     }
   return (CalculateTradeFloating_Value);
  }


New code (OrderType() != OP_BUY && OrderType() != OP_SELL) :


void CloseOpenAndPendingTrades(int trade_close_magic)
  {
   for(int pos_0 = OrdersTotal() - 1; pos_0 >= 0; pos_0--)
     {
      OrderSelect(pos_0, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != trade_close_magic)
         continue;

      if(OrderType() != OP_BUY && OrderType() != OP_SELL)
        {
         ResetLastError();
         if(!OrderDelete(OrderTicket()))
            Print(__FUNCTION__ " => Pending Order failed to close, error code:", GetLastError());
        }
      else
        {
         ResetLastError();
         if(OrderType() == OP_BUY)
           {
            if(!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrBlue))
               Print(__FUNCTION__ " => Buy Order failed to close, error code:", GetLastError());
           }
         else
            if(!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clrYellow))
               Print(__FUNCTION__ " => Sell Order failed to close, error code:", GetLastError());
        }
     }
  }

double CalculateTradeFloating(int CalculateTradeFloating_Magic)
  {
   double CalculateTradeFloating_Value = 0;
   for(int CalculateTradeFloating_i = 0; CalculateTradeFloating_i < OrdersTotal(); CalculateTradeFloating_i++)
     {
      OrderSelect(CalculateTradeFloating_i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || (OrderType() != OP_BUY && OrderType() != OP_SELL))
         continue;
      if(CalculateTradeFloating_Magic == OrderMagicNumber())
         CalculateTradeFloating_Value += OrderProfit() + OrderSwap() + OrderCommission();
     }
   return CalculateTradeFloating_Value;
  }


If my both code is wrong, than what is the right way to do?
What does OrderType() > OP_SELL mean? - How to check if the order is a pending order type?
What does OrderType() > OP_SELL mean? - How to check if the order is a pending order type?
  • 2024.02.06
  • anuj71
  • www.mql5.com
I understand that ordertype() == op_sell means it should be equal to a sell order , and ordertype(). Edit:- but yes it is a wrong practice as mentioned in the post#1 as anything above op_sell could also be balance order
 
anuj71 #:
but now your saying it wrong approach than what is the correct approach.

Be more attentive

Forum on trading, automated trading systems and testing trading strategies

Best way to filter Orders with Symbol and Magic Number

William Roeder, 2024.03.27 13:00

This rejects all orders
if(OrderType() != OP_BUY || OrderType() != OP_SELL) continue;
Reason: