OrderClosePrice() or Bid/Ask?

 

Should i use Bid/Ask for closing trade


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());
        }
     }
  }

or OrderClosePrice() ?


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(), OrderClosePrice(), Slippage, clrBlue))
               Print(__FUNCTION__ " => Buy Order failed to close, error code:", GetLastError());
           }
         else
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, clrYellow))
               Print(__FUNCTION__ " => Sell Order failed to close, error code:", GetLastError());
        }
     }
  }
 

In your example it doesn't matter. Both options are identical.

But the disadvantage of OrderClosePrice is that using OrderClosePrice requires that the order to be closed be previously selected using OrderSelect. If you start collecting information about orders into an array of structures/classes, then you will need to use bid/ask because the order being closed will not be preselected.

So I would suggest to always use bid/ask so that your code is the same regardless of whether an order is selected

 
On top of that, if you are closing/deleting several orders in a cycle (carrying out several trading operations in a row), then it would be a good idea to add RefreshRates before using bid/ask. Since during the execution of a trading operation the price may change, which will increase the risk of an error occurring when closing an order
 
anuj71: Should i use Bid/Ask for closing trade or OrderClosePrice() ?
Either works
         ResetLastError();
         if(OrderType() == OP_BUY)
           {
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, clrBlue))
               Print(__FUNCTION__ " => Buy Order failed to close, error code:", GetLastError());
           }
         else
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, clrYellow))
               Print(__FUNCTION__ " => Sell Order failed to close, error code:", GetLastError());
        }
Simplified
         ResetLastError();
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, clrYellow))
               Print(__FUNCTION__ " => " + string(OrderType()) + " Order failed to close, error code:", GetLastError());
        }
 
William Roeder #:
Simplified

Another way to simplify (using bid/ask while doing this):

if(!OrderClose(OrderTicket(), OrderLots(), OrderType() == OP_BUY ? Bid : Ask, Slippage, clrYellow))

The code can be made even shorter:

if(!OrderClose(OrderTicket(), OrderLots(), OrderType() ? Ask : Bid, Slippage, clrYellow))

But I wouldn't do that😄

 
Vladislav Boyko #:
OrderType() == OP_BUY ? Bid : Ask

ternary operator works inside functions like OrderClose(), OrderModify(), OrderSend(), etc?


William Roeder #:
Either works
Simplified

One confusion. My developer saying using OrderClosePrice() is wrong. It get the price of closed order but order is still open and not closed.



 
anuj71 #:
ternary operator works inside functions like OrderClose(), OrderModify(), OrderSend(), etc?

Why not? The ternary operator returns a value.

anuj71 #:
One confusion. My developer saying using OrderClosePrice() is wrong. It get the price of closed order but order is still open and not closed.

Using OrderClosePrice when closing an order is not wrong. And it works.

Above I described a couple of disadvantages of this method. But if you are sure that the desired order has been pre-selected, you can use OrderClosePrice.

 
Vladislav Boyko #:
Why not? The ternary operator returns a value.
Vladislav Boyko #:
Using OrderClosePrice when closing an order is not wrong

By the way, you can write a small test advisor to check this yourself.


[EDIT]

Vladislav Boyko #:
Above I described a couple of disadvantages of this method.

This method has only one disadvantage (which I described), and not a couple😄

 
OrderSelect+OrderClosePrice.
 
Vladislav Boyko #:
Why not? The ternary operator returns a value.

This is correct

OrderSend(Symbol(), OP_SELL, Lot_Size, Bid, Slippage, SL = SL == 0 ? 0 : (Bid + SL * Point), TP = TP == 0 ? 0 : (Bid - TP * Point), SellStopTradeComment, gSellMagic, 0, clrYellow);


Vladislav Boyko #:
This method has only one disadvantage (which I described), and not a couple😄

So, it always good to use Ask/Bid instead of OrderClosePrice()?


fxsaber #:
OrderSelect+OrderClosePrice.

Boyko, saying OrderClosePrice is not good approach.

 
anuj71 #:

Boyko, saying OrderClosePrice is not good approach.

He does not understand.

Reason: