You'll need to scan the orders twice: first to discover whether or not there is the OP_BUY or OP_SELL trades that you want should cause canceling of pending orders. The second loop is to carry out the canceling. Something like the following:
PHP Code:
bool has_sell = false;
bool has_buy = false;
for (int i = OrdersTotal()-1; i >= 0; i-- ) {
if ( ! OrderSelect( i, SELECT_BY_POS ) )
continue;
has_sell |= OrderType() == OP_SELL;
has_buy |= OrderType() == OP_BUY;
}
for ( i = OrdersTotal()-1; i >= 0; i-- ) {
if ( ! OrderSelect( i, SELECT_BY_POS ) )
continue;
if ( has_sell && OrderType() == OP_BUYSTOP )
OrderDelete( OrderTicket(), DarkGoldenrod );
if ( has_buy && OrderType() == OP_SELLSTOP )
OrderDelete( OrderTicket(), DarkGoldenrod );
}