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

 
richplank:
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.

Thanks Richplank,
But your code is deleting the other pending order when one of them get triggered, what I need is delete the other pending order when the other order closed because of it's SL or TP. Or there's any other way to use your code?
 
DONE!!
Thanks for the code Richplank, I do it this way
   int last_trade=OrdersHistoryTotal(); 
   if(last_trade>0) 
    {
      if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
       {
         if(OrderSymbol()==CurrencyPairs && OrderCloseTime()>=Time[0])
          {
             closeSingletons();         
          }
       }
    }
But I'm still curious why my own code doesn't work, if anybody could fix it, we can control it using magic number.

Thanks
 
Good that it works. Though I obviously didn't really grasp your intention, and maybe what you want is beyond MT4, because I don't think there is any direct link kept between a pending order and the order it seeds apart from the position profile (price, SL and TP). The seeded buy/sell order is a different order, and I don't think it inherits the magic number of the pending order. (I may be wrong, of course)

Your method is good, but requires a trace-back from the recently closed buy/sell order to the pending order that seeded it, and then a trace-back from that to the order it was paired with. The first trace-back could perhaps be done by matching the open time of the buy/sell order with the close time of the pending order, while the second could be done using magic number. Alternatively you might be able to use the comment field for both trace-backs, if (as I think) the comment is inherited from a pending order to the seeded order, although MT4 will add to and change the tail part of the comment.

Or, if you are sure about the account activity, you might be able to rely on that the pending order is the one closed before the most recently closed buy/sell order, and thus look at and use the magic of last_trade-2 instead. That would probably work no less incidentally than the current solution.
 
Thanks for your suggestion.

Btw, My code is working fine now, i've change it a little bit, although I don't quiet understand why a minor change could make it works.
This is the correct code
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); 
 

Be careful with Select by position, as it is affected by the sorting order of your trades in the terminal window.

***********

I may be confused about this, sorry.. Will retest

***********

 
phy:

Be careful with Select by position, as it is affected by the sorting order of your trades in the terminal window.

I'm sorry I don't really understand, do you mean these?
Constant      Value   Description 
OP_BUY          0   Buying position. 
OP_SELL         1   Selling position. 
OP_BUYLIMIT     2   Buy limit pending position. 
OP_SELLLIMIT    3   Sell limit pending position. 
OP_BUYSTOP      4   Buy stop pending position. 
OP_SELLSTOP     5   Sell stop pending position.
Or if you don't mind give me an example please?
Thank you