is it possible to coding an autoclose position when crossing ?

 

is it possible to coding an autoclose position when crossing ?

let say

//----------------------------- 
if ( a < b )
{
CloseBuy();
}
if( a1 > b2)
{
CloseSell();
} 
//----------------------------- 
void CloseBuy() 
{
   int cnt = OrdersTotal();
   for (int i=cnt-1; i>=0; i--) 
   {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

      //if (OrderSymbol() != Symbol()) continue;

      //if (OrderMagicNumber() != Magic) continue;

      if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, Slippage);

   }
}
//+------------------------------------------------------------------+
void CloseSell() 
{
   int cnt = OrdersTotal();
   for (int i=cnt-1; i>=0; i--) 
   {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

      //if (OrderSymbol() != Symbol()) continue;

      //if (OrderMagicNumber() != Magic) continue;


      if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage);
   }
}
//+------------------------------------------------------------------+
 

why it doesnt close any trade ??

just only buy/sell trade is opened.

 
Check your return codes and find out WHY. What are Function return values ? How do I use them ? - MQL4 forum
 
WHRoeder:
Check your return codes and find out WHY. What are Function return values ? How do I use them ? - MQL4 forum

thanks for the link.

Reason: