OrderClose problem

 

Hi there where is the mistake ? doesn't want to close the order :(


Thank you :)


if(h4==1 && h1==1 && m30==1 && m15==1){
                    
              if (ticket<=0){         
                           
              ticket=OrderSend(Symbol(), OP_BUY, 0.01, Ask, 1, 0, 0,"Buy", 1,0,clrAqua) ;
                            
   } 
    if(h4==-1 && h1==-1 && m30==-1 && m15==-1){
              
               OrderClose(OrderTicket(),OrderLots(),Ask, 1, clrSilver);              
                ticket=0;
              }
        } 
 
OrderClose - Trade Functions - MQL4 Reference
OrderClose - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderClose - Trade Functions - MQL4 Reference
 
OrderClose(OrderTicket(),OrderLots(),Ask, 1, clrSilver);
You can not use any Trade Functions unless you select an order first.
 
marek:

Hi there where is the mistake ? doesn't want to close the order :(


Thank you :)


int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
   if(h4==-1 && h1==-1 && m30==-1 && m15==-1)
   {
    OrderSelect(i, SELECT_BY_POS);
    if (OrderSymbol()!=Symbol()) continue;
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  }
  return(0);

corrected