why it does not close the order? help please its my first EA

 

it doesnt close orders something wrong with my OrderClose any idea?

thanks

  ticketb=OrderSend(Symbol(),OP_BUY,lotSize,Ask,3, 0, 0,"izzy",MagicNumber ,0 ,Green);   //open
   
   
  

   
      if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)
      {
       Alert("LONG ORDER OPENED IN izzy"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
      Order = SIGNAL_TRBUY;
      
       
        
      }
      else
      {
      Print("OrderSelect returned the error of ",GetLastError());
      Alert("EROR LONG ORDER IN izzy "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
      Order= IDLE;
      }
   }
} 
/////////////////////////////////// END


/////////////////////////sell
if (trade==false)
{
  if (Order == SIGNAL_SELL  )
  {

  tickets=OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0,"izzy",MagicNumber,0,Green); // open sell 


      if(OrderSelect( tickets, SELECT_BY_TICKET )==true)
      {
       Alert("SHORT ORDER OPENED IN izzy"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
      Order = SIGNAL_TRSELL;
      
        
      }
      else
      {
      Print("OrderSelect returned the error of ",GetLastError());
      Alert("EROR SHORT ORDER IN izzy "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
      Order= IDLE;
      }
   }
} 
/////////////////////////////////// END

/////////////////check order status buy

if (Order==SIGNAL_TRBUY)
{
   if ( MA9<MA18 )
      bool anbuy= OrderClose(ticketb,lotSize,Bid,3,Red);
      
      if(anbuy== true)
      {
      Order=IDLE;
      Alert("ORDER CLOSED IN izzy"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
      }
      else
      {
      
      Print("OrderSelect returned the error of when trying to close ",GetLastError());
      Alert("EROR ORDER closed IN izzy "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
      }
      

}

if (Order==SIGNAL_TRSELL)
{
   if ( MA9>MA18 )
      bool ansell= OrderClose(tickets,lotSize,Bid,3,Green);
      
      if(ansell== true)
      {
      Order=IDLE;
      Alert("ORDER CLOSED IN izzy"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",tickets);
      }
      else
      {
      
      Print("OrderSelect returned the error of when trying to close ",GetLastError());
      Alert("EROR ORDER closed IN izzy "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
      }
      
}
   
//if (OrderSelect( ticketb, SELECT_BY_TICKET,MODE_HISTORY )==true)

 

This line needs an added bit.

if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)

This will return true even if the trade is in the history. Add this:

 if(OrderSelect( ticketb, SELECT_BY_TICKET )==true) {
      if(OrderCloseTime()==0) {

      //The rest of the code

      }
 }
 

You are misusing OrderSelect(). You are tring to select the ticket number instead of the order pool array index. Once you've selected the correct location, you can then retriev e the ticket number from that location.

In the section for closing, you should loop through the order pool from (high to low), select the ticket and then close.

have a read of these

https://www.mql5.com/en/forum/127908/page2#358436

https://book.mql4.com/trading/orderclose

Also, the for loop for closing orders in the book is wrong. Use...

for(int i=OrdersTotal()-1; i>=0; i--)                                              
      {
      if (OrderSelect(i,SELECT_BY_POS)==true)                           
         { 
         // Check criteria and close
         }
      }

hth

V

Reason: