How to delete pending order?

 

Hi: I have this first loop which scans for open orders and pending orders.  If there is an open order and a pending order, I want to delete this pending order.  What codes should I use below?

int PosTotal=PositionsTotal();
   int pnd=0,opn=0;
   
   for(int i=PosTotal-1; i>=0; i--)
      if(PositionGetSymbol(i)==Symbol())
         if(MagicNumber==PositionGetInteger(POSITION_MAGIC))
            if(PositionGetInteger(POSITION_TYPE)>1)pnd++;else opn++;

   if(opn>0 && pnd>0){
      for(int i=PosTotal-1; i>=0; i--)
      if(PositionGetSymbol(i)==Symbol())
         if(MagicNumber==PositionGetInteger(POSITION_MAGIC))
            if(PositionGetInteger(POSITION_TYPE)>1){//<----------------- then, I want to delete this pending order
   
   
   }

thanks!:-)
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
 

Ive tried this but doesnt work

for(int i=PosTotal-1; i>=0; i--)
      if(PositionGetSymbol(i)==Symbol())
         if(MagicNumber==PositionGetInteger(POSITION_MAGIC))
            if(PositionGetInteger(POSITION_TYPE)>1){
               ulong tkt=OrderGetTicket(i);
               CTrade *trade=new CTrade;
                trade.OrderDelete(tkt);
               
            }
 

If I had wanted to delete all pending orders on symbol which has already had a position I would have made something like this. 

#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   if(PositionSelect(Symbol()))
     {
      int ord_total=OrdersTotal();
      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;
           }
        }
     }
//---
   return;
  }
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
 

I tried your code, didnt work, its not even entering the loop to print.  Im working on the code from this article: https://www.mql5.com/en/articles/59. the code+yours is below. many thanks for your help, alex.

void OnTick()
  {//---
   if(time2trade(Hr,MagicNumber))
     { 
         MqlTradeRequest BigDogBuy;
         MqlTradeRequest BigDogSell;
         BigDogBuy.action=TRADE_ACTION_PENDING;
         // Set the pending order
         BigDogBuy.magic = MagicNumber;
         BigDogBuy.symbol=Symbol();
         BigDogBuy.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)+Price*Point();
         //Price by which the order will be set
         BigDogBuy.volume=Lots;
         BigDogBuy.sl=BigDogBuy.price-TrailingStop*Point();
         //if the stop loss is not established, then we set by the strategy
         BigDogBuy.tp=BigDogBuy.price+TakeProfit*Point();
         //set the take profit
         BigDogBuy.deviation=dev;
         //Minimum deviation from the requested price, 
         //in other words, by how much the executed price can differ from the specified price
         BigDogBuy.type=ORDER_TYPE_BUY_STOP;
         //order type, which is executed based on the specified price or by a higher than specified price
         //in this case the order is set to a higher or equal amount to the specified price 
         //if the order type was buy_limit, then it would be executed 
         //by the specified price, or prices lower than the specified price
         BigDogBuy.type_filling=ORDER_FILLING_AON;
         //the given parameter demonstrates how the order acts  
         //with partial execution of the scope   
         BigDogBuy.expiration=TimeTradeServer()+6*60*60;
         //by the strategy text the order life span only for the current work day
         //since it has been 2 hours since the opening of the American market, and the work day is 8 hours, we have 8-2 = 6
         BigDogSell.action=TRADE_ACTION_PENDING;

         // Set the pending order
         BigDogSell.magic = MagicNumber;
         BigDogSell.symbol=Symbol();
         BigDogSell.price=SymbolInfoDouble(Symbol(),SYMBOL_BID)-Price*Point();
         //Price by which the order will be set
         BigDogSell.volume=Lots;
         BigDogSell.sl=BigDogSell.price+TrailingStop*Point();
         //Stop loss set by the strategy
         BigDogSell.tp=BigDogSell.price-TakeProfit*Point();
         //Set take profit
         BigDogSell.deviation=dev;
         //Minimum deviation from the requested price, 
         //in other words, by how much the executed price can differ from the specified price
         BigDogSell.type=ORDER_TYPE_SELL_STOP;
         //order type, which is executed based on the specified price or by a higher than specified price
         //in this case the order is set to a higher or equal amount to the specified price  
         //if the order type was buy_limit, then it would be executed
         //by the specified price, or prices lower than the specified price
         BigDogSell.type_filling=ORDER_FILLING_AON;
         //the given parameter demonstrates how the order acts  
         //with partial execution of the scope 
         BigDogSell.expiration=TimeTradeServer()+6*60*60;
         //by the strategy text the order life span only for the current work day
         //since it has been 2 hours since the opening of the American market, and the work day is 8 hours, we have 8-2 = 6
         MqlTradeResult ResultBuy,ResultSell;
         OrderSend(BigDogBuy,ResultBuy);
         OrderSend(BigDogSell,ResultSell);
     }

   int PosTotal=PositionsTotal();
   int pnd=0,opn=0;
  
  if(PositionSelect(Symbol())) <------- I inserted your code here. If any of the above BigDog buy/sell stop is triggered, the other must be deleted.
     {Print("here");
      int ord_total=OrdersTotal();
      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;
           }
        }
     }
An Example of a Trading Strategy Based on Timezone Differences on Different Continents
  • 2010.06.08
  • Vasily
  • www.mql5.com
Surfing the Internet, it is easy to find many strategies, which will give you a number of various recommendations. Let’s take an insider’s approach and look into the process of strategy creation, based on the differences in timezones on different continents.
 

Did you manage to figure out how to delete all the pending orders for a symbol? I'm trying to do the same and the code suggested above doesn't seem to work for me.

Cheers

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
 

This topic was started in Oct 2011, 

Try this one, see if this works, and always check for error :

   MqlTradeRequest req = {0};
   MqlTradeResult  res = {0};

   int orders = OrdersTotal();
   req.action = TRADE_ACTION_REMOVE;

   for(int i = orders - 1; i >= 0 ;i--)
     {
     req.order  = OrderGetTicket (i);
      if (OrderGetString(ORDER_SYMBOL) == _Symbol)
        {
        ResetLastError();
        if(!OrderSend(req,res))
          {
           Print("Fail to delete ticket ",req.order  ,": Error ",GetLastError(),", retcode = ",res.retcode);
          }
        }
     }

With little effort we can correct alexvd's code below. BTW I didn't compile nor check this code below.

#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   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;
           }
        }
     }
//---
   return;
  }
 
phi.nuts:

Try this one, see if this works, and always check for error :

With little effort we can correct alexvd's code below. BTW I didn't compile nor check this code below.

Great thanks, thats works.
 
good one
 

hmm, why create a new Trade object in every for-loop?

 
Alexey Da:

If I had wanted to delete all pending orders on symbol which has already had a position I would have made something like this. 

Thanks

 
Alexey Da #:

If I had wanted to delete all pending orders on symbol which has already had a position I would have made something like this. 


Good code. To make the system work more efficiently, lets make the trade declaration is done just once in a global area.