Delete STOP pending order when opposite STOP order activated

 
Hello, Im starting with MQL4 language and I'm trying to put togerther my first simple breakout trading system. It works on breakout of range principle, so BUY stop on top of the price range and SELL stop on the bottom of the price range. But, I dont found effective solution how to delete the opposite STOP order when some order is activated. Can you help me? :-)
 
Martazz:
Hello, Im starting with MQL4 language and I'm trying to put togerther my first simple breakout trading system. It works on breakout of range principle, so BUY stop on top of the price range and SELL stop on the bottom of the price range. But, I dont found effective solution how to delete the opposite STOP order when some order is activated. Can you help me? :-)

https://www.mql5.com/en/code/22009

OCO Order on MetaTrader4 Platform , MT4 (One cancels the other)
OCO Order on MetaTrader4 Platform , MT4 (One cancels the other)
  • www.mql5.com
Places One-Cancels-the-Other Order (OCO Order), One pip Limit Order and One Pip Stop Order on MetaTrader 4 (MT4) platform. Global Variables : Press function key F3" Change OCO_BUY_LIMIT      : Buy Limit Price OCO_BUY_STOP       : Buy Stop Price OCO_SELL_LIMIT     : Sell Limit Price OCO_SELL_STOP      : Sell Stop Price OCO_confirmation   : After...
 
Go through your list of orders (your chart/MN). Remember if you find an open order and the ticket number of any pending. Delete the pending if you found an open.
 
William Roeder:
Go through your list of orders (your chart/MN). Remember if you find an open order and the ticket number of any pending. Delete the pending if you found an open.

Yes that sound simple! I tried to put together code below. But it seems my logic is bad, it doesnt work :/.

for (p=OrdersTotal(); p>=0;p--) // go through all orders
      
{if(OrderSelect(q, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY) // if we have active BUY order which belongs to our EA
   {for (m=OrdersTotal(); m>=0;m--) // start another counter, go through all orders
 
      {if(OrderSelect(q, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber()==MagicNumber && OrderType() == OP_SELLSTOP) //if we have SELLSTOP, which belongs to our EA
         {delete = OrderDelete(OrderTicket(), Yellow);}}}} // delete it.
         

Also Im curious, when ppl use operator For, why someone uses OrdersTotal()-1 and other group of people using it without -1?

I am very grateful for your advices, I don't programmed anything before I started with MQL :)

 

I find code made by Fernando Carreiro on this forum, which solves this problem. https://www.mql5.com/en/forum/159380

void CheckMarketDeletePending()
{
   for( int i = OrdersTotal() - 1; i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
      {
         if( ( OrderMagicNumber() == MagicNumber ) && ( OrderSymbol() == _Symbol ) )
         {
            if( ( OrderType() == OP_BUY ) || ( OrderType() == OP_SELL ) )
            {
               DeletePendingOrders();
               break;
            }
         }
      }
   }
}

void DeletePendingOrders()
{
   for( int i = OrdersTotal() - 1; i >= 0; i-- )
   {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
      {
         if( ( OrderMagicNumber() == MagicNumber ) && ( OrderSymbol() == _Symbol ) )
         {
            if( ( OrderType() != OP_BUY ) && ( OrderType() != OP_SELL ) )
            {
               if( !OrderDelete( OrderTicket() ) )
               {
                  // Delete failed (need to deal with situation)
                  // Check Error Codes
               }
            }
         }
      }
   }
} 


It seems my logic was right, but Im not exiting the For loop with "break;". Im not sure why its needed, I must study the MQL4 little bit more :-). Also, he is starting his For loop from OrdersTotal() -1 and not just OrdersTotal(), can someone please explain me, why?

 
Martazz: tarting his For loop from OrdersTotal() -1 and not just OrdersTotal(), can someone please explain me, why?

If there are 4 orders, their positions are 0 … 3. OrderSelect(OrdersTotal()… will always fail.

Reason: