Delete a pending order when other order with the same magic number touch SL/TP point

 
Hello,
I'm trying to create an expert which open 2 pending order (buy and sell) on every new candle. When one of the 2 pending orders get triggered, let say the buystop, and touch it's SL/TP, the other pending order will be deleted. How to do that? If the order closed because of it's SL/TP, can MT4 read it's magic number and delete another pending order with the same magic number?

note: This EA will open 2 pending order on every new candle. So it's possible to have 6 pending orders after 3 new candle. Lets say only one order of the first candle get triggered, after it's closed because of SL/TP, only the other order from the 1st candle get deleted, so there're 4 pending orders remaining. I was trying to do it using magic numbers, but it seems I'm lost in space .
Anybody could help me? at least give me a clue, please? :)

Thanks
 
Hello? anybody could help me?
I'm used to work with one order or more at a time, but not with many pending orders and not deleting a specific pending order with a specific magic number.
Can I get the latest closed order's magic number then find a pending order with the same magic number and delete it?
Or it's just not possible to do this?
 

It is possible.

 
phy:

It is possible.

Then would you mind to tell me how to do it Phy? And how to automatically generate a magic number for each bar?
Thank you
 

I might use the time of the bar for the magic number for the pair of orders.

That would cause problems for backtest, so it may not be the best solution for you.

Set up a GlobalVariable as a counter, increment with each pair of orders.

You'll probably have to continously loop through open and historical orders to find out what is open and what has
been closed, if you are closing by stop loss or take profit.

 
phy:

I might use the time of the bar for the magic number for the pair of orders.

I was thinking the same, but the owner of the idea wants 1-1000 as it's magic number.
Any clue?
Thanks
 

Well, whatever you do, there will be a lot of searching through open and historical orders to do matching
and status update.

 
int trades[10000];
int max = -1;
 
void closeSingletons()
{
    // Build up table of tickets to close (those alone on a bar)
    for ( int i = OrdersTotal() - 1; i >= 0; i-- ) {
        if ( ! OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ) continue;
        if ( OrderMagicNumber() != magic ) continue;
        if ( OrderSymbol() != Symbol() ) continue;
        int b = iBarShift( Symbol(), Period(), OrderOpenTime(), false );
        max = MathMax( max, b );
        if ( trades[ b ] == 0 ) trades[ b ] = OrderTicket(); else trades[ b ] = 0;
    }
 
    // Close the ones being alone and clear the table to be ready for next time
    for ( i = 0; i <= max; i++ ) {
        if ( trades[ b ] == 0 ) continue;
        if ( OrderSelect( trades[ b ], SELECT_BY_TICKET, MODE_TRADES ) ) {
            double price = Bid;
            if ( OrderType() == OP_SELL ) price = Ask; 
            OrderClose( OrderTicket(), OrderLots(), price, 0, Yellow );
        }
        trades[ b ] = 0;
    }
    max = -1;
}
 
richplank:
int trades[10000];
int max = -1;
 
void closeSingletons()
{
    // Build up table of tickets to close (those alone on a bar)
    for ( int i = OrdersTotal() - 1; i >= 0; i-- ) {
        if ( ! OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ) continue;
        if ( OrderMagicNumber() != magic ) continue;
        if ( OrderSymbol() != Symbol() ) continue;
        int b = iBarShift( Symbol(), Period(), OrderOpenTime(), false );
        max = MathMax( max, b );
        if ( trades[ b ] == 0 ) trades[ b ] = OrderTicket(); else trades[ b ] = 0;
    }
 
    // Close the ones being alone and clear the table to be ready for next time
    for ( i = 0; i <= max; i++ ) {
        if ( trades[ b ] == 0 ) continue;
        if ( OrderSelect( trades[ b ], SELECT_BY_TICKET, MODE_TRADES ) ) {
            double price = Bid;
            if ( OrderType() == OP_SELL ) price = Ask; 
            OrderClose( OrderTicket(), OrderLots(), price, 0, Yellow );
        }
        trades[ b ] = 0;
    }
    max = -1;
}
It seems not working for me, but thanks anyway
 
Now I'm trying this way, with this code I try to define magic number of the latest closed order

extern string  CurrencyPairs = "GBPUSD";
   int last_trade=OrdersHistoryTotal(); 
   if(last_trade>0) 
    {
      if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
       {
         if(OrderSymbol()==CurrencyPairs)
          {
             mag=OrderMagicNumber();             
          }
       }
    }

and use this to delete a pending order with the same magic number
   int total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
    {
      OrderSelect(cnt,SELECT_BY_TICKET,MODE_TRADES);
      if(OrderType()>OP_SELL && OrderSymbol()==CurrencyPairs && OrderMagicNumber()==mag)
       {
          closeticket=OrderTicket();
          OrderDelete(closeticket);
       }
    }

but it's still not working, which one is wrong?
Thanks
 
yes, sorry about that. I misunderstood what you needed.
I'm not sure what's wrong in your code snippet; maybe the expectation that the "fired" pending order appears last in the order history?

You can adapt my code snippet for pending orders as follows:
 
int trades[10000];
int max = -1;
 
void closeSingletons()
{
    // Build up table of tickets to close (those alone on a bar)
    for ( int i = OrdersTotal() - 1; i >= 0; i-- ) {
        if ( ! OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) ) continue;
        if ( OrderSymbol() != Symbol() ) continue;
        if ( OrderType() == OP_BUY || OrderType() == OP_SELL ) continue;
        int b = iBarShift( Symbol(), Period(), OrderOpenTime(), false );
        max = MathMax( max, b );
        if ( trades[ b ] == 0 ) trades[ b ] = OrderTicket(); else trades[ b ] = 0;
    }
 
    // Close the ones being alone and clear the table to be ready for next time
    for ( i = 0; i <= max; i++ ) {
        if ( trades[ b ] != 0 )
            OrderDelete( trades[ b ] );
        trades[ b ] = 0;
    }
    max = -1;
}
The idea is still to pair up trades by virtue of iBarShift(..) i.e. which bar the order is opened at, and then delete all pending orders that are singletons on their bars.