Delete pending orders for one magic number

 

I am trying to delete pending orders when I open a position on my chart.

The problem is that if another strategy (same EA, different magic) on same symbol has an open order, the pending order will be deleted instantly.


I am probably missing something, but cant find what.

What I can understand, it is my other strategy that are killing the pending orders that are open with main strategy.

First I check if I have open orders for this EA. Here I understand the code will run for deletePending on that other strategy. I guess that is OK, but best thing is to not do that ofcourse.

But, deletePending sould take magic into play, but it doesnt seem to do that. It is deleting pending orders with magic that doesnt belong I guess.


Any suggestion on how to fix this?


Code:

This is under OnTick()

if(checkOpen()>0)
      {
      deletePending();
      }

This is checkOpen()

double checkOpen()
  {
   int total=PositionsTotal();
   int total1=0;
   double profit=0;
   for(int i=PositionsTotal()-1; i>=0; i--)
      {
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==Symbol() && m_position.Magic()==magic1)
        {
        total1+=1;
        }
      }
   return(total1);
  }

And this is deleting pending orders

void deletePending()
  {
   
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      long magica=PositionGetInteger(POSITION_MAGIC);
      if(OrdersTotal()>0 && magica==magic1)
      {
      Print("Deleting pending orders for ", magica);
      ulong OrderTicket=OrderGetTicket(i);
      trade.OrderDelete(OrderTicket);    
     }
     }
  }
 
use orderselect(), then done~
 
Pak Hong Poon #:
use orderselect(), then done~

You mean like this?

void deletePending()
  {

   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      long magica=PositionGetInteger(POSITION_MAGIC);
      ulong ticket=PositionGetTicket(i);
      if(OrderSelect(ticket)==true)
        {
         if(OrdersTotal()>0 && magica==magic1)
           {
            Print("Deleting pending orders for ", magica);
            ulong OrderTicket=OrderGetTicket(i);
            trade.OrderDelete(OrderTicket);
           }
        }
     }
  }
 

Delete Pending Orders:

//+------------------------------------------------------------------+
//| Delete all pending orders                                        |
//+------------------------------------------------------------------+
void DeleteAllPendingOrders(void)
  {
   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
         if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==InpMagic)
             if(!m_trade.OrderDelete(m_order.Ticket()))
                if(InpPrintLog)
                   Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.OrderDelete ",m_order.Ticket())
  }
 
Vladimir Karputov #:

Delete Pending Orders:

Super thanks! Will try that out!

 
Vladimir Karputov #:

Delete Pending Orders:

Had to work the code a bit. Got quite a lot of errors when compiling. 

Now it works like a charm! Thanks again!

void deletePending()
{
   //--- variables for returning values from order properties 
   ulong    ticket; 
   double   open_price; 
   double   initial_volume; 
   datetime time_setup; 
   string   symbol; 
   string   type; 
   long     order_magic; 
   long     positionID; 
   uint     total=OrdersTotal(); 
//--- go through orders in a loop 
   for(uint i=0;i<total;i++)
   { 
      //--- return order ticket by its position in the list 
      if((ticket=OrderGetTicket(i))>0) 
        { 
         //--- return order properties 
         open_price    =OrderGetDouble(ORDER_PRICE_OPEN); 
         time_setup    =(datetime)OrderGetInteger(ORDER_TIME_SETUP); 
         symbol        =OrderGetString(ORDER_SYMBOL); 
         order_magic   =OrderGetInteger(ORDER_MAGIC); 
         positionID    =OrderGetInteger(ORDER_POSITION_ID); 
         initial_volume=OrderGetDouble(ORDER_VOLUME_INITIAL); 
         type          =EnumToString(ENUM_ORDER_TYPE(OrderGetInteger(ORDER_TYPE))); 
            if(symbol==_Symbol && order_magic==magic1)
            {
            Print("Deleting pending orders for ", order_magic);
            ulong OrderTicket=OrderGetTicket(i);
            if(!trade.OrderDelete(OrderTicket))
               Print("Could not delete pending order for ", m_order.Magic());
           }
        } 
   }     
}
 
Jose Daniel Stromberg Martinez # :

Had to work the code a bit. Got quite a lot of errors when compiling. 

Now it works like a charm! Thanks again!

Please insert the code correctly: FIRST press the button Code, and THEN paste the code in the pop-up window.

 

Still had some issues if it was more than 2 open orders. 

If i count upwards, from 0, the first order was cancelled, but the second ones position was transfered to 0.

If I then counted backwards in that loop, only the first one was cancelled because I dont want to count to 0 in the loop, because that will be an endless loop.


I did like this instead, please tell if there is a better solution. But this works (but not optimal code I guess).


void deletePending()
  {
//--- variables for returning values from order properties
   ulong    ticket;
   double   open_price;
   double   initial_volume;
   datetime time_setup;
   string   symbol;
   string   type;
   long     order_magic;
   long     positionID;
   uint     total=OrdersTotal();
//--- go through orders in a loop
   for(uint i=total; i>=0; i--)
     {
      if(OrdersTotal()==0)
      {
      break;
      }
      //--- return order ticket by its position in the list
      if((ticket=OrderGetTicket(i))>0)
        {

         //--- return order properties
         open_price    =OrderGetDouble(ORDER_PRICE_OPEN);
         time_setup    =(datetime)OrderGetInteger(ORDER_TIME_SETUP);
         symbol        =OrderGetString(ORDER_SYMBOL);
         order_magic   =OrderGetInteger(ORDER_MAGIC);
         positionID    =OrderGetInteger(ORDER_POSITION_ID);
         initial_volume=OrderGetDouble(ORDER_VOLUME_INITIAL);
         type          =EnumToString(ENUM_ORDER_TYPE(OrderGetInteger(ORDER_TYPE)));
         if(symbol==_Symbol && order_magic==magic1)
           {
            Print("Deleting pending orders for ", order_magic, " ",i, " ", ticket);
            ulong OrderTicket=OrderGetTicket(i);
            if(!trade.OrderDelete(ticket))
               Print("Could not delete pending order for ", m_order.Magic());
           }
        }
     }
} 
 
Jose Daniel Stromberg Martinez #:

Still had some issues if it was more than 2 open orders. 

If i count upwards, from 0, the first order was cancelled, but the second ones position was transfered to 0.

If I then counted backwards in that loop, only the first one was cancelled because I dont want to count to 0 in the loop, because that will be an endless loop.


I did like this instead, please tell if there is a better solution. But this works (but not optimal code I guess).



Hi Jose,

I see you have declared m_order, instead of using OrderGetInteger(), it should be more efficient using m_order.  isn't it?

 
Jose Daniel Stromberg Martinez #:
irst one was cancelled becau
//--- go through orders in a loop
   for(uint i=total; i>=0; i--)
     {
      if(OrdersTotal()==0)

I changed your code to:

//--- go through orders in a loop (with minus 1)
   for(uint i=total-1; i>=0; i--)
     {
      if(OrdersTotal()==0)


@Jose Daniel Stromberg Martinez your code now working perfectly for me.

;)

This reply is over 2 years later , thanks lot.

regards,


void deletePending(long MagicNum)
  {
//--- variables for returning values from order properties
   ulong    ticket;
   
   double   initial_volume;
   
   string   symbol;
   string   type;
   long     magic_number;
   long     positionID;
   uint     total=OrdersTotal();
//--- go through orders in a loop
//for(uint i=total-1; i>=0; i--)
   for(uint i=total-1; i>=0; i--)
     {
      if(OrdersTotal()==0)
      {
      break;
      }
      //--- return order ticket by its position in the list
      if((ticket=OrderGetTicket(i))>0)
        {

         //--- return order properties
         
         symbol        =OrderGetString(ORDER_SYMBOL);
         magic_number   =OrderGetInteger(ORDER_MAGIC);
         positionID    =OrderGetInteger(ORDER_POSITION_ID);
         initial_volume=OrderGetDouble(ORDER_VOLUME_INITIAL);
         type          =EnumToString(ENUM_ORDER_TYPE(OrderGetInteger(ORDER_TYPE)));
         if(symbol==_Symbol && magic_number==MagicNum)
           {
            Print("Deleting pending orders for magic number=", magic_number, " ",i, " ","ticket number id=", ticket);
            ulong OrderTicket=OrderGetTicket(i);
            if(!trade.OrderDelete(ticket))
               Print("Could not delete pending order for order id: ",positionID ," ","symbol=", symbol," " ,"type=",type," ","Volume=",initial_volume," ",  "magic number=",magic_number );
           }
        }
     }
} 
 
Jose Daniel Stromberg Martinez #:

Still had some issues if it was more than 2 open orders. 

If i count upwards, from 0, the first order was cancelled, but the second ones position was transfered to 0.

If I then counted backwards in that loop, only the first one was cancelled because I dont want to count to 0 in the loop, because that will be an endless loop.


I did like this instead, please tell if there is a better solution. But this works (but not optimal code I guess).


You must count to 0, because 0 is the index of the first ticket of 'Open Pending Orders'.

At i=0 the pending order will have the lowest ticket number id as it was opened before all other orders.

If you see yellow highlighted ticket where i=0 was deleted last, with ticket number id=120.

There were 4 'open pending order' tickets in  total that needed deleting. i=3, i=2, i=1, i=0.


Reason: