pending delete code (help)

 

Hi fellas

how can I delete a OP_BUYSTOP order if the last OP_SELL order it 's closed

and the same thing OP_SELLSTOP if the last OP_BUY it's closed

I tried this code but it didn't work

many thanks in advance for your help


void Delete()
{
        int cnt;
        datetime buy_time,sell_time;
        datetime buystop_time,sellstop_time;
        int total= OrdersTotal();
        if(OrdersTotal()>0)
        {
                for(int i=0;i<OrdersTotal();i++)
                {
                        OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
                        if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_Nu)
                        {
                                if(OrderType()==OP_BUY){buy_time=OrderCloseTime();}
                                if(OrderType()==OP_SELL){sell_time=OrderCloseTime();}
                                //----
                                if(OrderType()==OP_BUYSTOP){buystop_time=OrderOpenTime();}
                                if(OrderType()==OP_SELLSTOP){sellstop_time=OrderOpenTime();}                            
                        }
                }
        }
        //----
        if(buy_time > sellstop_time)
        {
                for (cnt = total-1 ; cnt >= 0 ; cnt--)
                {
                        OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
                        if(OrderMagicNumber() == magic_Nu && OrderSymbol()==Symbol() && (OrderType()==OP_SELLSTOP))
                        {
                                OrderDelete(OrderTicket());
                        }
                }
        }
        if(sell_time > buystop_time)
        {
                for (cnt = total-1 ; cnt >= 0 ; cnt--)
                {
                        OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
                        if(OrderMagicNumber() == magic_Nu && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP))
                        {
                                OrderDelete(OrderTicket());
                        }
                }
        }
}


 

If you select from the MODE_TRADES pool then you are selecting only Open orders so they won't have an OrderCloseTime()

I think you need to be clearer about what you are trying to do . . . for example, you said the last Sell or last Buy . . . does that mean that there are no Buy orders open or no Sell orders open ? or could the one before the last one still be open ?

 

Hi Mr. RaptorUK

Thank you for your help

for example, you said the last Sell or last Buy . . . does that mean that there are no Buy orders open or no Sell orders open ?

for every buy there is a pending sell (OP_SELLSTOP) 
and for every sell there is a pending buy (OP_BUYSTOP)
 
but if the buy order is closed In the case of the profit or loss 
I need to delete the pending sell orders
Same thing for the sell order if it s closed pending buy should be deleted 
 
  1. Why don't you simply delete the pending order, once its opposite opens?
    int openTicket=-1, pendingTicket = -1;
        for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
        &&  OrderMagicNumber()  == Magic.Number                 // my magic number
        &&  OrderSymbol()       == chart.symbol                 // and my pair.
        ){
            if (OrderType() <= OP_SELL) openTicket    = OrderTicket();
            else                        pendingTicket = OrderTicket();
        }
    if (openTicket > -1 && pendingTicket > -1){
       if (!OrderDelete(pendingTicket)) Alert("OrderDelect(", pendingTicket, ") failed: ", GetLastError());
    }

  2. Otherwise, if there is only one pending order and you always open two, the other must have triggered and closed.
    int openTicket = -1, pendingTicket = -1, nPending = 0;
        for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
        &&  OrderMagicNumber()  == Magic.Number                 // my magic number
        &&  OrderSymbol()       == chart.symbol                 // and my pair.
        ){
            if (OrderType() <= OP_SELL) openTicket    = OrderTicket();
            else{                       pendingTicket = OrderTicket(); nPending++; }
        }
    if (openTicket == -1 && nPending == 1){
       if (!OrderDelete(pendingTicket)) Alert("OrderDelect(", pendingTicket, ") failed: ", GetLastError());
    }
    Not compiled, not tested.

  3. int total= OrdersTotal();
            if(OrdersTotal()>0)
            {
                    for(int i=0;i<OrdersTotal();i++)
                    {
                            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    The IF is unnecessary, you must count down when closing/deleteing in the presence of multiple order (multiple charts). Always test return codes (orderSelect)
 
asta:

but if the buy order is closed In the case of the profit or loss

I need to delete the pending sell orders

Same thing for the sell order if it s closed pending buy should be deleted

OK, I understand that, but is there only one Buy and one Sell at at time or may there be many ?

If there is just ever one Buy or one Sell it is easy . . . loop through the open orders looking for a Buy, if you don't find one then simply OrderDelete() the pending buy order . . . and similarly for the Sell . . .

If there can be more than one then it gets more complicated and you need to be able to match up a Buy with a particular pending Buy and a Sell with a particular pending Sell.

 

Dear WHRoeder

Thank you for your help

But the issue is

For every opening order there is a one specific pending order, how can I delete this pending order if this opening order it s closed

 

Thanks again dear RaptorUK

really really I appreciate it

If there can be more than one then it gets more complicated and you need to be able to match up a Buy with a particular pending Buy and a Sell with a particular pending Sell.

How can I resolve this problem ?

I tried tickets numbers in vain

        if(OrdersTotal()>0)
        {
                for(i=0;i<OrdersTotal();i++)
                {
                        OrderSelect(i,SELECT_BY_TICKET,MODE_HISTORY);
                        if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_Nu)
                        {
                                if(OrderType()==OP_BUY){buy_ticket=OrderTicket();}
                                if(OrderType()==OP_SELL){sell_ticket=OrderTicket();}
         }
      }
      //----
                for(i=0;i<OrdersTotal();i++)
                {
                        OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES);
                        if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_Nu)
                        {                               
                                if(OrderType()==OP_BUYSTOP){buystop_ticket=OrderTicket();}
                                if(OrderType()==OP_SELLSTOP){sellstop_ticket=OrderTicket();}                            
                        }
                }
        }

What about this solution how can I code it

If the Ask cross the stop loss of the buy ( win case) delete the pending sell

Or if the Bid cross the stop loss of the sell (win case) delete the pending buy

 
asta:

What about this solution how can I code it

If the Ask cross the stop loss of the buy ( win case) delete the pending sell

Or if the Bid cross the stop loss of the sell (win case) delete the pending buy

Sorry I misunderstood . . .

You still haven't said if you have more than one Buy or more than one Sell . . . if you just have one you can do this . . .

void Delete()
   {
   int cnt, i;
   int total= OrdersTotal();
   bool DeletePendingSell = true, DeletePendingBuy = true;
   
   if(OrdersTotal()>0)
      {
      for( i=0;i<OrdersTotal();i++)
         {
         if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            {
            Print("Order Select failed: ", GetLastError());
            return;
            }
         
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_Nu)
            {
            if(OrderType()==OP_BUY) DeletePendingSell = false;
            if(OrderType()==OP_SELL) DeletePendingBuy = false;
            }
         }
      
      for( i=0;i<OrdersTotal();i++)
         {
         if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            {
            Print("Order Select failed: ", GetLastError());
            return;
            }
            
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_Nu)
            {
            if(OrderType()==OP_SELLSTOP && DeletePendingSell )
               {
               if(!OrderDelete(OrderTicket()))
                  Print("Delete Pending SellStop failed", GetLastError());
               }
            
            if(OrderType()==OP_BUYSTOP && DeletePendingBuy )
               {
               if(!OrderDelete(OrderTicket()))
                  Print("Delete Pending BuyStop failed", GetLastError());
               }
            }
         }
      
      }

   }

NOT TESTED ! !

 
asta:

Dear WHRoeder

Thank you for your help

But the issue is

For every opening order there is a one specific pending order, how can I delete this pending order if this opening order it s closed

Why are you opening multiple pending orders in the same direction per pair? There's no point in having 2 buy stops.

If you still must, you're going to have to save the grouping in a file and restore on startup.

 

Thank you so much dear RaptorUK

You are a Angel

Your function is exact and helpful

You still haven't said if you have more than one Buy or more than one Sell . . . if you just have one you can do this . . .

Yes sometime there is more than one buy or sell but I can make the expert allow only one trade every time

Thanks again and Best regards

 
WHRoeder:

Why are you opening multiple pending orders in the same direction per pair? There's no point in having 2 buy stops.

If you still must, you're going to have to save the grouping in a file and restore on startup.

Dear WHRoeder

Thank you for your interest

Why are you opening multiple pending orders in the same direction per pair? There's no point in having 2 buy stops.

Let's take a real example

AUDNZD

Suppose this pair will be go down

If I make a order sell 1.25900

Take profit in 1.2390

in place of stop loss I will putt a buy stop in 1.2640

suppose after one hour I make a another order sell automatically I need a another stop loss in my case that s mean I need a another buy stop

in this point I have 2 sell orders and 2 buy stop pending orders

what I need, if price cross the take profit of one of them automatically his stop loss should be deleted in my case the stop loss is the pending order

Hope you get what I mean

Reason: