How to call out the ticket number and close it?

 
for(cnt=0;cnt<total;cnt++)
	{
	OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
	if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
		{
		if(OrderType()==OP_BUY) // long position is opened
			{
			// should it be closed?
			if(isCrossed == 2)
				{
				OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
				// close position
				return(0); // exit
				}

We write Ordersend( ), return the ticket number, e.g. 

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);

 and close with above code, which part of the code that call out the ticket number and close correctly?

as we know 

for(cnt=0;cnt<total;cnt++)

 search all the opened trade, it correctly search the ticket we want?

 
FXEWEN: for(cnt=0;cnt<total;cnt++)

Don't count up++ when closing orders. Countdown--. https://www.mql5.com/en/forum/139654.

for(int i=OrdersTotal()-1; i>=0; i--)
Always put OrderSelect within an if(){statement}.
 
FXEWEN:

We write Ordersend( ), return the ticket number, e.g. 


You can't rely on retaining the ticket number and using that to select your order . . . what happens if MT4 has to be restarted and you have an open order ?  you will no longer know the ticket number . . .  you have to find it by identifying the correct order.
 
FXEWEN:

We write Ordersend( ), return the ticket number, e.g. 

 and close with above code, which part of the code that call out the ticket number and close correctly?

as we know 

 search all the opened trade, it correctly search the ticket we want?


Use OrderMagicNumber() in your code.

extern int Magic_Number=1234;

...

for (int pos = OrdersTotal() - 1; pos >= 0; pos --)
    {
    if (OrderSelect(SELECT_BY_POS, MODETRADES)
        && OrderSymbol() == Symbol 
        && OrderMagicNumber() == Magic_Number)
        {
        switch (OrderType())
           {
           case OP_BUY  : ...
           case OP_SELL : ...
           ...
           }
         }
      }
        

 
Thanks for the knowledge
 

I try to code as MACD < 0 then sell follow by OrderClose( ) to close the trade.

 

double MACD = iMACD(Symbol(),0,12,26,9,0,0,0);
if(total < 1) //if no trade open
   {
     if (MACD < 0 )
     OrderSend ( Sell );


 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         //if( ! OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) ) continue;
         if( OrderSymbol() == Symbol()         
            && ( OrderType() == OP_BUY
            ||   OrderType() == OP_SELL ) )    

                 Alert("Job Ticket:" ,OrderTicket(), " found");
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue); // close position
                 return(0); // exit
                }
//----
   return(0);
  }

At OrderClose ( ) I still not give any command e.g " MACD > 0" then close, but the OrderClose ( ) close the trade when MACD > 0.

How this happen? 

 
I cannot see all your codes. From the above it appears that for(loop) runs without any if(){condition}. The if (MACD < 0 ) within your sample seems to only apply to opening orders. After that the EA is free to close orders at will.
 
FXEWEN:

I try to code as MACD < 0 then sell follow by OrderClose( ) to close the trade.

 

At OrderClose ( ) I still not give any command e.g " MACD > 0" then close, but the OrderClose ( ) close the trade when MACD > 0.

How this happen? 

1. Your Order Select code is wrong - which obviously you do not read and understand my code

for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);                   //<<--- wrong
    //if( ! OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) ) continue; //<<--- correct but why you don't use this

 2. That because you using bar 0 for MACD

Reason: