How to close all pending orders?

 

Hi,

I am back with another question.

Do you have any solution to close all stop orders? I've been searching for a while but still haven't found the answer.

below is my Pseudocode

if(OrdersTotal() < 2)
   {
      //close all pending orders
   }

Thank you very much for spending time to help me.

 

Hi, below I show you an example from documentation. What you do right now, is only add a comment. Regards Greg

#define EXPERT_MAGIC 123456 // MagicNumber of the expert
//+------------------------------------------------------------------+
//| Deleting pending orders |
//+------------------------------------------------------------------+
void OnStart()
{
//--- declare and initialize the trade request and result of trade request
MqlTradeRequest request={0};
MqlTradeResult result={0};
int total=OrdersTotal(); // total number of placed pending orders
//--- iterate over all placed pending orders
for(int i=total-1; i>=0; i--)
{
ulong order_ticket=OrderGetTicket(i); // order ticket
ulong magic=OrderGetInteger(ORDER_MAGIC); // MagicNumber of the order
//--- if the MagicNumber matches
if(magic==EXPERT_MAGIC)
{
//--- zeroing the request and result values
ZeroMemory(request);
ZeroMemory(result);
//--- setting the operation parameters
request.action=TRADE_ACTION_REMOVE; // type of trade operation
request.order = order_ticket; // order ticket
//--- send the request
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code
//--- information about the operation
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
}
}
}
//+------------------------------------------------------------------+
 
@Grzegorz Pawlak , Was anh tuan2  referring to MQL4 or MQL5, solution ?
 
MThompson:
@Grzegorz Pawlak , Was anh tuan2  referring to MQL4 or MQL5, solution ?
Ah, Sorry for didn't clarify the question. I am referring to MQL4.
 
Grzegorz Pawlak:

Hi, below I show you an example from documentation. What you do right now, is only add a comment. Regards Greg

Could you please give me a solution on MQL4? 
 
anh tuan2:
Could you please give me a solution on MQL4? 

You just call the function!!!...

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
DeleteOrder(OP_BUYSTOP,"");

  }
//+------------------------------------------------------------------+

void DeleteOrder(int type, string ordername) //delete pending orders of "type"
  {
   if(!IsTradeAllowed()) return;
   bool success = false;
   int err = 0;
   string ordername_ = ordername;
   if(ordername != "")
      ordername_ = "("+ordername+")";
   int total = OrdersTotal();
   int orderList[][2];
   int i,orderCount = 0;
   for( i = 0; i < total; i++)
     {
      while(IsTradeContextBusy()) Sleep(100);
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderMagicNumber() != MagicNumber || OrderSymbol() != Symbol() || OrderType() != type) continue;
      orderCount++;
      ArrayResize(orderList, orderCount);
      orderList[orderCount - 1][0] = (int) OrderOpenTime();
      orderList[orderCount - 1][1] = OrderTicket();
     }
   if(orderCount > 0)
      ArraySort(orderList, WHOLE_ARRAY, 0, MODE_ASCEND);
   for(i = 0; i < orderCount; i++)
     {
      if(!OrderSelect(orderList[i][1], SELECT_BY_TICKET, MODE_TRADES)) continue;
      while(IsTradeContextBusy()) Sleep(100);
      RefreshRates();
      success = OrderDelete(OrderTicket());
      if(!success)
        {
         err = GetLastError();
        }
     }
  }
 
Abubakar Saidu: You just call the function!!!...

Simplify by counting down.

void DeleteOrder(int type) //delete pending orders of "type"
  {
   for(int i = OrdersTotal()-1; i>=0; --i) if(
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
   && OrderMagicNumber() == MagicNumber 
   && OrderType()        == type
   && OrderSymbol()      == Symbol() 
   ){
      while(IsTradeContextBusy()){ Sleep(100); RefreshRates(); }
      success = OrderDelete(OrderTicket());
      if(!success)
        {
         int err = GetLastError(); …
        }
     }
  }