Unable to close order

 

Hi Gents, I ran out of ideas to debug this as I couldn't close the last order (always closed most of them except the last active trade). This isn't happening often. It only happens very late at night. When I was monitoring it, it wouldn't happen. so strange. Would you please help me double check? I feel I missed some knowledge. 


The error code is:  


void CloseOrDeleteAllOrders(){
   
   RefreshRates();
      
   // Start a loop to scan all the orders.
   // The loop starts from the last order, proceeding backwards; Otherwise it would skip some orders.
   for (int i = (OrdersTotal() - 1); i >= 0; i--){
      // If the order cannot be selected, throw and log an error.
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false){
         LogError("Unable to select the order. %d" + GetLastError());
         ResetLastError(); 
         continue;
      }
      
      if(OrderSymbol() != MarketSymbol) continue;
      if(OrderMagicNumber() != BotOrderMagicNumber) continue;
      
      // Create the required variables.
      // Result variable - to check if the operation is successful or not.
      bool res = false;
      
      // Update the exchange rates before closing the orders.
      // Bid and Ask prices for the instrument of the order.
      double BidPrice = MarketInfo(MarketSymbol, MODE_BID);
      double AskPrice = MarketInfo(MarketSymbol, MODE_ASK);
      
      // Closing the order using the correct price depending on the type of order.
      if (OrderType() == OP_BUY && OrderCloseTime()!=0){
         res = OrderClose(OrderTicket(), OrderLots(), BidPrice, 0);
      } else if (OrderType() == OP_SELL && OrderCloseTime()!=0){
         res = OrderClose(OrderTicket(), OrderLots(), AskPrice, 0);
      } else if(OrderType() == OP_SELLSTOP || OrderType() == OP_BUYSTOP) {
         res = OrderDelete(OrderTicket());
      }
      
      // If there was an error, log it.
      if (res == false){ 
         string errorMsg="Unable to close the order";
         int theErrorCode = GetLastError();  
         if(OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_HISTORY)){
            errorMsg += " as it is closed by market";
         }
         LogWarning(StringFormat("%s. Ticket=%d. %d", errorMsg, OrderTicket(), theErrorCode));
      }
   }
} 

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

 if (OrderType() == OP_BUY && OrderCloseTime()!=0)
should be
 if (OrderType() == OP_BUY && OrderCloseTime()==0)

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.


Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford #:

if (OrderType() == OP_BUY && OrderCloseTime()!=0)

should be

if (OrderType() == OP_BUY && OrderCloseTime()==0)


Damn, how stupid I missed that. 

Thanks legend.

 
  1.  if (OrderType() == OP_BUY && OrderCloseTime()==0)

    You are reading the open orders pool. There will never be an entry with OCT non-zero. Unnecessary test.

  2. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

 
Keith Watford #:

if (OrderType() == OP_BUY && OrderCloseTime()!=0)

should be

if (OrderType() == OP_BUY && OrderCloseTime()==0)


Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.


Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

OrderCloseTime() == 0 

you test that for an opened trade?  of course is 0... it is still open...

 
Daniel Cioca #:

you test that for an opened trade?  of course is 0... it is still open...

Why are you telling me?

It is not my code, I was just pointing out a mistake in the OP's code!