Cannot delete pending order MQL5/ MT5

 

Can someone please check why I cannot delete an order on MT5 EA.

   int ord_total=OrdersTotal();
   if(ord_total > 0)
     {
      for(int i=ord_total-1;i>=0;i--)
        {
         ulong ticket=OrderGetTicket(i);
         if(OrderSelect(ticket) && OrderGetString(ORDER_SYMBOL)==Symbol())
           {
            CTrade *trade=new CTrade();
            trade.OrderDelete(ticket);
            delete trade;
           }
        }
     }

1. I have placed a stop loss order manually
2. Order shows up @ Trade Tab of the Toolbox Panel.
3. Using Debug Mode, variable ticket recognizes order number
4. After
delete trade; line execution, order still remains on Trade Tab

 
YouTrade:

Can someone please check why I cannot delete an order on MT5 EA.

1. I have placed a stop loss order manually
2. Order shows up @ Trade Tab of the Toolbox Panel.
3. Using Debug Mode, variable ticket recognizes order number
4. After
delete trade; line execution, order still remains on Trade Tab

Please use the SRC button while posting parts of code. This time I'll do that for you. ;-)
 

Problem fixed using a different approach (don´t know if is the best one but anyway ...)

void Apaga_Pending_Orders()
  {
   int ord_total=OrdersTotal();

   if(ord_total>0)
     {
      for(int i=ord_total-1;i>=0;i--)
        {
         ulong ticket=OrderGetTicket(i);

         ZeroMemory(mrequest);

         mrequest.action=TRADE_ACTION_REMOVE;                                   // immediate order execution
         mrequest.magic=0;
         mrequest.order=ticket;
         mrequest.symbol=_Symbol;                                               // currency pair
         mrequest.volume=1;
         mrequest.type=ORDER_TYPE_SELL_STOP;
         mrequest.comment="YouTrade";                                          // comment

         if(OrderSend(mrequest,mresult))
           {
            if(mresult.retcode==TRADE_RETCODE_PLACED || mresult.retcode==TRADE_RETCODE_DONE)
              {
               Print("[TICKET] Order ticket: ",mresult.order," retcode: ",mresult.retcode);
              }
            else
              {
               Print("Order error: ",GetLastError()," retcode: ",mresult.retcode);
               ResetLastError();
               return;
              }
           }
         else
           {
            Print("Order Send error.");
            ResetLastError();
            return;
           }
        }
     }
  }
 
YouTrade:

Problem fixed using a different approach (don´t know if is the best one but anyway ...)

Please take a look at this post. It's a kind of "best practice" to check not only the return codes from the server, but also the boolean value you get from your OrderDelete() function (in case you actually uses it).
 
YouTrade:

Can someone please check why I cannot delete an order on MT5 EA.

1. I have placed a stop loss order manually
2. Order shows up @ Trade Tab of the Toolbox Panel.
3. Using Debug Mode, variable ticket recognizes order number
4. After
delete trade; line execution, order still remains on Trade Tab

Hi Marcelo, note that the first code you post will filter all orders just by symbol name, so this is very risky, in my opinion, principally for BM&FBovespa.

The second code is not enough too, since don't test any symbol and if you just need do delete pending orders this is not right.

The big problem here is that several of this details are, in my opinion, very risk to BM&FBovespa, and not so critical to Forex market, but most of MQL5 site examples are regarding Forex.

 

A really good practice, if there is one, would be you double check all your OrderSend() removing pending orders commands, for instance, reading the deals history (this is very same the visual inspection you are doing).

But for BM&FBovespa, there is latency, and probably this is not so simple to code.

 
figurelli:

Hi Marcelo, note that the first code you post will filter all orders just by symbol name, so this is very risky, in my opinion, principally for BM&FBovespa.

The second code is not enough too, since don't test any symbol and if you just need do delete pending orders this is not right.

The big problem here is that several of this details are, in my opinion, very risk to BM&FBovespa, and not so critical to Forex market, but most of MQL5 site examples are regarding Forex.

Hi Rogerio,

Can you explain what you mean (I highlighted what is not clear to me) ? Thank you.

 
YouTrade:

Can someone please check why I cannot delete an order on MT5 EA.

1. I have placed a stop loss order manually

Do you mean a stop order ? (BUY_STOP or SELL_STOP). With MT5, a stoploss is not a pending order but is linked to an other order (market or pending).

2. Order shows up @ Trade Tab of the Toolbox Panel.
3. Using Debug Mode, variable ticket recognizes order number
4. After
delete trade; line execution, order still remains on Trade Tab

There are several reasons why this code doesn't delete your order, as said by Malacarne and Figurelli, you always have to check the return code, whatever method you are using to send trade request.

OrderGetTicket() already select your order, you don't need to use OrderSelect() too.

Returns ticket of a corresponding order automatically selects the order for further working with it using functions.

   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      ulong ticket=OrderGetTicket(i);
      //--- This code remove all pending orders for the chart's symbol
      //--- where you are running the code
      if(OrderGetString(ORDER_SYMBOL)==Symbol())
        {
         CTrade *trade=new CTrade();    // You don't really need to use pointer, a "normal" variable will be good
         bool res=trade.OrderDelete(ticket);
         //--- add error checking
         ...
         delete trade;
        }
     }
Documentation on MQL5: Trade Functions / OrderGetTicket
Documentation on MQL5: Trade Functions / OrderGetTicket
  • www.mql5.com
Trade Functions / OrderGetTicket - Documentation on MQL5
 
YouTrade:

Problem fixed using a different approach (don´t know if is the best one but anyway ...)

Some fields are not needed to delete a pending order. You don't print the error when OrderSend return false :

   mrequest.action=TRADE_ACTION_REMOVE;                                   // immediate order execution
   mrequest.magic=0;
   mrequest.order=ticket;
   mrequest.symbol=_Symbol;                                               // currency pair
   mrequest.volume=1;
   mrequest.type=ORDER_TYPE_SELL_STOP;
   mrequest.comment="YouTrade";                                          // comment

   if(OrderSend(mrequest,mresult) && (mresult.retcode==TRADE_RETCODE_PLACED || mresult.retcode==TRADE_RETCODE_DONE))
     {
      Print("[TICKET] Order ticket: ",mresult.order," retcode: ",mresult.retcode);
     }
   else
     {
      Print("Order error: ",GetLastError()," retcode: ",mresult.retcode);
      ResetLastError();
      return;
     }
P.S: I don't think you can get a return 1008 (TRADE_RETCODE_PLACED) when deleting a pending order. Not sure though, can someone confirm ?
 
angevoyageur:

Some fields are not needed to delete a pending order. You don't print the error when OrderSend return false :

P.S: I don't think you can get a return 1008 (TRADE_RETCODE_PLACED) when deleting a pending order. Not sure though, can someone confirm ?
I think you can only get TRADE_RETCODE_DONE as the desired result.
 
angevoyageur:

Hi Rogerio,

Can you explain what you mean (I highlighted what is not clear to me) ? Thank you.

For sure, this is just risk advices about the use of such codes, principally for the BM&FBovespa pilot project, that I suppose is the main idea of Marcelo, and regarding these codes managing his real accounts.

Unfortunately, for stock exchanges in general, and this is my opinion, MT5 is not so secure and portable yet as for Forex (despite we still have some minor problems at Forex, several of them discussed at this Forum).

In this sense, and if you want and have your time to contribute and discuss about it, I created a topic in the Portuguese area and you are welcome there (https://www.mql5.com/pt/forum/23409) to contribute with your critics and ideas (even in English).

Anyway, if possible, I would like to know your opinion about the use of such codes at BM&FBovespa, as maybe I'm wrong and there are no risks here.

Thank you.

Um checklist dos riscos dos robôs antes de operar em conta real
Um checklist dos riscos dos robôs antes de operar em conta real
  • www.mql5.com
Existe um modo de pânico no Expert Advisor para administrar situações de risco de perda de capital por falhas ou erros dos algoritmos?
Reason: