EA hangs trying to close trade.

 

I have an EA that enters in a pyramid type grid. When DD hits a certain percentage it closes the first order opened in the basket, I tried coding this but it just seems to hang the EA. 

Can someone find the culprit in this code?

Thanks in advance.

//---
if(ch_b!=0){

      if(UseTrailing){Trailing();}
      
      if(Profit(OP_BUY)<AccountBalance()*(-max_dd/100))
      while(ch_b!=0){CloseBuyOrders();ch_b = check(OP_BUY);}

}
//---
if(ch_s!=0){

      if(UseTrailing){Trailing();}

      if(Profit(OP_SELL)<AccountBalance()*(-max_dd/100))
      while(ch_s!=0){CloseSellOrders();ch_s = check(OP_SELL);}

int EarliestOpenSellTicket()
{
    datetime EarliestSellTime  = TimeCurrent();        
    int      EarliestSellTicket = -1;                  
    for(int pos = OrdersTotal()-1; pos >= 0 ; pos--)
    {
      if (!OrderSelect(pos, SELECT_BY_POS))return (-1);   
      if (OrderOpenTime()>EarliestSellTime)          
      if (OrderSymbol() == Symbol())
      if (OrderMagicNumber()== Magic)
      if (OrderType()==OP_SELL)                                     
       EarliestSellTime = OrderOpenTime();
       EarliestSellTicket = OrderTicket();
    }
    
    return(EarliestSellTicket);
}
// ----------------------------- //
int EarliestOpenBuyTicket()
{
    datetime EarliestBuyTime  = TimeCurrent();        
    int      EarliestBuyTicket = -1;                  
    for(int pos = OrdersTotal()-1; pos >= 0 ; pos--)
    {
      if (!OrderSelect(pos, SELECT_BY_POS))return (-1);   
      if (OrderOpenTime()>EarliestBuyTime)         
      if (OrderSymbol() == Symbol())
      if (OrderMagicNumber()== Magic)
      if (OrderType()==OP_BUY)           
                                                       
       EarliestBuyTime = OrderOpenTime();
       EarliestBuyTicket = OrderTicket();
    }
    
    return(EarliestBuyTicket);
}
// ----------------------------- //
void CloseBuyOrders(){
    if(OrderSelect(EarliestOpenBuyTicket(),SELECT_BY_POS,MODE_TRADES)){
        ticket=OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNONE);}}
// ----------------------------- //
void CloseSellOrders(){
    if(OrderSelect(EarliestOpenSellTicket(),SELECT_BY_POS,MODE_TRADES)){
       ticket=OrderClose(OrderTicket(),OrderLots(),Ask,3,clrNONE);}}
 
  1.     datetime EarliestSellTime  = TimeCurrent();        
        int      EarliestSellTicket = -1;                  
        for(int pos = OrdersTotal()-1; pos >= 0 ; pos--)
        {
          if (!OrderSelect(pos, SELECT_BY_POS))return (-1);   
          if (OrderOpenTime()>EarliestSellTime)          
    When will you ever get an OpenOrderTime in the future?


  2.     if(OrderSelect(EarliestOpenBuyTicket(),SELECT_BY_POS,MODE_TRADES)){
    The function returns a ticket number. Do you expect it to ever select by position?
 
whroeder1:
  1. When will you ever get an OpenOrderTime in the future?


  2. The function returns a ticket number. Do you expect it to ever select by position?

Thanks whroeder I should have noticed these issues but they escaped me due to being somewhat of a novice to MQL.

Its now selecting the order but instead its closing the entire basket. Is there something simple I am missing here?

Thanks for your support.

//---
if(ch_b!=0){

      if(UseTrailing){Trailing();}
      
      if(Profit(OP_BUY)<AccountBalance()*(-max_dd/100))
      while(ch_b!=0){CloseBuyOrders();ch_b = check(OP_BUY);}

}
//---
if(ch_s!=0){

      if(UseTrailing){Trailing();}

      if(Profit(OP_SELL)<AccountBalance()*(-max_dd/100))
      while(ch_s!=0){CloseSellOrders();ch_s = check(OP_SELL);}
}

// ----------------------------- //
void CloseBuyOrders(){
      if (OrderSelect(EarliestOpenBuyTicket(),SELECT_BY_TICKET)){
      if (OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNONE))
      Alert("the order isn't close because Err no. ", GetLastError());
      }
      }
// ----------------------------- //
void CloseSellOrders(){
      if (OrderSelect(EarliestOpenSellTicket(),SELECT_BY_TICKET)){
      if (OrderClose(OrderTicket(),OrderLots(),Ask,3,clrNONE))
      Alert("the order isn't close because Err no. ", GetLastError());
      }
      }
// ----------------------------- //
int EarliestOpenSellTicket(){
   datetime CurrentTime=TimeCurrent();
   int EarliestSellTicket = -1;
   for(int pos = OrdersTotal()-1; pos >= 0 ; pos--)
   {
      if (!OrderSelect(pos,SELECT_BY_POS))return (-1);
      if (OrderSymbol()==Symbol())
      if (OrderOpenTime()<CurrentTime)
      
       EarliestSellTicket = OrderTicket();

        }
        return(EarliestSellTicket);
}
// ----------------------------- //
int EarliestOpenBuyTicket(){
   datetime CurrentTime=TimeCurrent();
   int EarliestBuyTicket = -1;
   for(int pos = OrdersTotal()-1; pos >= 0 ; pos--)
   {
      if (!OrderSelect(pos,SELECT_BY_POS))return (-1);
      if (OrderSymbol()==Symbol())
      if (OrderOpenTime()<CurrentTime)
      
       EarliestBuyTicket = OrderTicket();

        }
        return(EarliestBuyTicket);
}
 
I have solved the issues.