Problems with a script which should delete a pending order

 

Hey guys,

I always get error #4756 and I have no idea why... can anyone help?

void OnStart() {
   int totalOrders=OrdersTotal();
   for (int i=totalOrders;i>=0;i--) {
      ulong ticket=OrderGetTicket(i);
      if (OrderSelect(ticket)) {
         string orderSymbol=OrderGetString(ORDER_SYMBOL);
         if ("EURCHF"==orderSymbol) {
            MqlTradeRequest   request; 
            MqlTradeResult    result;
            ZeroMemory(request);
            ZeroMemory(result);
            request.action  =TRADE_ACTION_REMOVE;
            request.position=ticket;
            if (!OrderSend(request,result)) {
               PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code 
            }
            PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
         } 
      }
   }                
}
 
Marbo: Hey guys, I always get error #4756 and I have no idea why... can anyone help?

ERR_TRADE_SEND_FAILED

4756

Trade request sending failed

for( int i = totalOrders - 1; i >= 0; i-- )

I only scanned your code quickly. I did not analyse it in depth. There may be other issues.

 
Fernando Carreiro #:

ERR_TRADE_SEND_FAILED

4756

Trade request sending failed

I only scanned your code quickly. I did not analyse it in depth. There may be other issues.

Hello Fernando,

I modified my script a bit so I can see if the script finds the correct orders and I implemented your modification. The script finds all pending orders but it doesn't delete any of them. Always error 4756 and I don't understand this error. Additionally I receive this error in the journal: "failed cancel order #0 buy 0 at market (invalid request)".

void OnStart() {
   int totalOrders=OrdersTotal();
   for (int i=totalOrders-1;i>=0;i--) {
      ulong ticket=OrderGetTicket(i);
      if (OrderSelect(ticket)) {
         string orderSymbol=OrderGetString(ORDER_SYMBOL);
         Print(orderSymbol+" "+(string)ticket);
         MqlTradeRequest request; 
         MqlTradeResult result;
         ZeroMemory(request);
         ZeroMemory(result);
         request.action=TRADE_ACTION_REMOVE;
         request.position=ticket;
         if (!OrderSend(request,result)) {
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code 
         }
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
      }
   }                
}
 
Marbo #: I modified my script a bit so I can see if the script finds the correct orders and I implemented your modification. The script finds all pending orders but it doesn't delete any of them. Always error 4756 and I don't understand this error. Additionally I receive this error in the journal: "failed cancel order #0 buy 0 at market (invalid request)".
  1. You are not checking the return code for OrderGetTicket() to see if it is valid.
  2. No need to select the order. It is already selected by OrderGetTicket() .
  3. Just initialise the structure as empty instead of using ZeroMemory()
  4. Set the Order ticket and not the Position ticket.
 ulong ticket = OrderGetTicket( i );
 if( ticket > 0) {
    MqlTradeRequest request = {}; 
    MqlTradeResult  result  = {};
    request.action = TRADE_ACTION_REMOVE;
    request.order  = ticket;
     
    Fernando Carreiro #:
    1. You are not checking the return code for OrderGetTicket() to see if it is valid.
    2. No need to select the order. It is already selected by OrderGetTicket() .
    3. Just initialise the structure as empty instead of using ZeroMemory()
    4. Set the Order ticket and not the Position ticket.

      Thank you so much, Fernando! Now it works perfect. The problem was that I copied this code from my SL to breakeven script and there I need to use position instead of order.

       
      Marbo #: Thank you so much, Fernando! Now it works perfect. The problem was that I copied this code from my SL to breakeven script and there I need to use position instead of order. Thanks again!!
      You are welcome!