Testing Code and found OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES) does not change

 

Testing Code and found OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES) does not change after selected ticket is closed. Anyone know the reason?

extern int Ticket=0 ;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   Ticket=GetTicket() ;  
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
  bool test ;    
  string endmsg ;
  
  test=OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES) ;
  
  Comment("Ticket = ",Ticket," TEST  ",test) ;
  
  if ( OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES) == false ) //When order closed this remains true????
                                                                   //yes I know I can use the not operator rather than ==
    {
     endmsg=StringConcatenate("Ticket ",Ticket," Now Closed - EA removed from chart") ;
     RemoveEA(endmsg) ;
    }  
}//end of Start() 
//+------------------------------------------------------------------+

int GetTicket()
{
int result=-1 ;
int i,active ;

active = OrdersTotal() ;
for (i=active-1; i>=0; i--)
    {
     if ( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) )
       {
        if ( Symbol() == OrderSymbol() )
          {
           result=OrderTicket() ;
           break ;           
          }
       }
    }  
return(result) ;
}
//+------------------------------------------------------------------+
void RemoveEA(string msg, bool DoAlert=true, bool DoPrint=true) //Don't forget to use #include <WinUser32.mqh> for this function
{
  if( DoAlert ) Alert(msg) ;
  if( DoPrint ) Print(msg ) ;
  
  int h = WindowHandle(Symbol(), Period());
  if (h != 0) PostMessageA(h, WM_COMMAND, 33050, 0);
}
I can change it something else to make it work but it is a bit annoying when something does not behave as documentation suggests it will.
 
Ickyrus:

Testing Code and found OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES) does not change after selected ticket is closed. Anyone know the reason?

I can change it something else to make it work but it is a bit annoying when something does not behave as documentation suggests it will.

Read the Documentation . . . MODE_TRADES is only applicable when SELECT_BY_POS

pool - Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any of the following values:
 

Ah "The pool parameter is ignored if the order is selected by the ticket number" Does NOT mean only selects from the trading pool (as I automatically interprit that) but selects from both the History pool and trading pool.

and hence the sentance that follows. The way explained above only confuses me more

 

Pool is ONLY used for selecting by position, irrelevant when selecting by ticket number.

If you select by ticket, the OrderCloseTime will be zero/non-zero depending on if it closed or not. For non-closed orders OrderClosePrice is Bid (Buy) or Ask (Sell)

Your GetTicket means your EA in incompatible with every other including manual trading. Always test magic number, and pair, (and TF)

int GetTicket()
{
int result=-1 ;
int i,active ;

active = OrdersTotal() ;
for (i=active-1; i>=0; i--)
  {
   if ( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) )
     {
      if ( Symbol() == OrderSymbol() )
        {
         result=OrderTicket() ;
         break ;           
        }
     }
  }  
return(result) ;
int GetTicket()
{
   for (int iPos=OrdersTotal(); iPos>=0; iPos--) if(
       OrderSelect(iPos, SELECT_BY_POS, MODE_TRADES) 
   &&  OrderMagicNumber() == magic.number
   &&  Symbol()           == OrderSymbol()
   ){  return( OrderTicket() ); }
   return(-1);
}  
 

WHRoeder the ea is to track a manually entered trade and is only used in the init() part of the ea.

SELECT_BY_POS - therefore use 0,1,2,3 to select ticket MODE_TRADES the current active orders MODE_HISTORY the closed orders list.

Probability is we are dealing with a database queries so the ticket query is set to fetch from all your ticket numbers where by pos creates a list of trades with no close time.

Makes sense to me now.

 
Thanks a lot, helped me too.
Reason: