Download MetaTrader 5

delete pending ( problem with order ticket )

To add comments, please log in or register
Have you installed MetaTrader 5? Find out more about it!
Mehrdad Shiri
4480
Mehrdad Shiri 2013.12.23 19:59 

Hi;

can you please help to fix,

it's can not delete pending:

//--- input expert parameters
/*
#include <Trade/Trade.mqh>
#include <Trade/PositionInfo.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
*/
MqlTradeRequest trReq;
MqlTradeResult trRez;
.
.
int MAGIC=999;              // MAGIC number
long ORDER_TICKET;
.
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   MqlTick tick;
   if(!SymbolInfoTick(Symbol(),tick))
     {
      Print("Failed to get Symbol info!");
      return;
     }
/*   
   //--- make sure that the account is demo
   if(AccountInfoInteger(ACCOUNT_TRADE_MODE)==ACCOUNT_TRADE_MODE_REAL)
     {
      Alert("Script operation is not allowed on a live account!");
      return;
     }
*/         
   if(...) 
     {
      //DeleteAllOrdersByMagic();
      Delete_Pending_Orders_Sub_Program();
      Buy_Stop_Trade_Sub_Program();
     }
Comment("" 
       );
   return;
   }   
//--- End of OnTick()   
//*************************************************************     
//------------------------------------- Buy_Stop_Trade_Sub_Program --------------------------------
void Buy_Stop_Trade_Sub_Program()
    {
      trReq.symbol=Symbol();
      trReq.action=TRADE_ACTION_PENDING;                         // setting a pending order
      trReq.magic=MAGIC;
      trReq.deviation=10;                                        // deviation in 5 points
      trReq.price=max_high;  
      trReq.sl=0;         
      trReq.tp=0;           
      trReq.type=ORDER_TYPE_BUY_STOP;              
      trReq.volume=LOT;
      OrderSend(trReq,trRez);
    }
//---------------------------------------------------------------------------------------------------------------|

void Delete_Pending_Orders_Sub_Program()
    {
     for(int i=OrdersTotal()-1;i>=0;i--)
        {
         //trReq.symbol=Symbol();
         trReq.order=OrderGetTicket(0);
         trReq.action=TRADE_ACTION_REMOVE; 
         OrderSend(trReq,trRez);

        }
    }    
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
30752
Alain Verleyen 2013.12.23 20:10  
TIMisthebest:

Hi;

can you please help to fix,

it's can not delete pending:

void Delete_Pending_Orders_Sub_Program()
    {
     for(int i=OrdersTotal()-1;i>=0;i--)
        {
         //trReq.symbol=Symbol();
         trReq.order=OrderGetTicket(i);
         trReq.action=TRADE_ACTION_REMOVE; 
         OrderSend(trReq,trRez);

        }
    }    
I strongly suggest you to check the returned value when you are using a function.
Mehrdad Shiri
4480
Mehrdad Shiri 2013.12.23 20:15  
angevoyageur:
I strongly suggest you to check the returned value when you are using a function.

thank you ;

but also with this correction ; still does not work.

Alain Verleyen
Moderator
30752
Alain Verleyen 2013.12.23 20:31  
TIMisthebest:

thank you ;

but also with this correction ; still does not work.

So, what is the returned value of the OrderSend() function ? What is the value of trRez.retcode ?
Documentation on MQL5: Trade Functions / OrderSend
Documentation on MQL5: Trade Functions / OrderSend
  • www.mql5.com
Trade Functions / OrderSend - Documentation on MQL5
Mehrdad Shiri
4480
Mehrdad Shiri 2013.12.23 20:54  
angevoyageur:
So, what is the returned value of the OrderSend() function ? What is the value of trRez.retcode ?
sorry,

10009

TRADE_RETCODE_DONE

Request completed

Mehrdad Shiri
4480
Mehrdad Shiri 2013.12.23 21:09  
angevoyageur:

You don't call your function ?

      //Delete_Pending_Orders_Sub_Program();

:) sorry

thank you.

Alain Verleyen
Moderator
30752
Alain Verleyen 2013.12.23 21:15  
TIMisthebest:
sorry,

10009

TRADE_RETCODE_DONE

Request completed

Please note that you have to initialize your structure, or you can also have some errors.

//---  Remove All Pending Orders

   MqlTradeRequest request={0};
   MqlTradeResult  result;

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      request.order=OrderGetTicket(i);
      request.action=TRADE_ACTION_REMOVE;
      bool isOk=OrderSend(request,result);
      if(!isOk || !(result.retcode==10008 || result.retcode==10009))
        {
         Print("Remove pending order #",OrderGetTicket(i)," error #",result.retcode);
        }
     }
Mehrdad Shiri
4480
Mehrdad Shiri 2013.12.23 21:19  
angevoyageur:
thank you.
Kourosh Davallou
40401
Kourosh Davallou 2013.12.24 15:48  

hi mehrdad

void DeletePendingOrder(int magic, string sym ,ENUM_ORDER_TYPE type =ORDER_TYPE_BUY_LIMIT )
{
    
    long order_ticket;
    
    HistorySelect(0,TimeCurrent());
   
    for (int i=OrdersTotal()-1;i>=0;i--)
    if (order_ticket=OrderGetTicket(i))
 
    if (OrderGetInteger(ORDER_MAGIC) == magic && OrderGetString(ORDER_SYMBOL) == sym && OrderGetInteger(ORDER_TYPE) == type )
    {
        MqlTradeResult result;
        MqlTradeRequest request;
        request.order=order_ticket;
        request.action=TRADE_ACTION_REMOVE;
        OrderSend(request,result);
     
    }
    
Mehrdad Shiri
4480
Mehrdad Shiri 2013.12.24 16:23  
kourosh1347:

hi kourosh:

thank you.
To add comments, please log in or register