What happens here ?

 
OrderSelect(OrdersTotal()-1, SELECT_BY_POS, MODE_TRADES);//
Does this point to the last placed (or 2nd last) active order, or to the first or what ?
 
The last placed.
 

I'd expect that to select the order in the current orders pool which was opened first. However, if this functionality is what you need, then a more reliable way would be to cycle through all orders in the pool, comparing OrderOpenTime().


CB

 

I would tend to agree with Roger because I assume the first or oldest order in the pool would have the lowest index (0 or 1, dunno if the index starts at 0 or 1??).

Still confused (and the Book doesn't address these important issues at all), does the code above point to the last or 2nd last order? Can you guys come to an agreement on this ?

Another confusing issue ( I THINK I know whats right, but I have to be absolutley certain I KNOW whats right) :

Obviously I Buy at Ask, and Sell at Bid, but am I using Ask and Bid correctly in this HiddenTP code?

      for(int i = OrdersTotal(); i >= 0; i--)
         {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol() == Symbol() )
            {                                                
            if(OrderType() == OP_SELL)
               {               
               if(   (OrderOpenPrice()) <= (Bid - TakeProfit * Point)   )
                  {
                  OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
                  return (0);
                  }              
               }                              
            if(OrderType() == OP_BUY)
               {              
               if(   (OrderOpenPrice() ) >= (Ask + TakeProfit * Point)   )
                  {
                  OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
                  return (0);
                  }                                                                  
               }                              
            }
         }
 
cloudbreaker:

I'd expect that to select the order in the current orders pool which was opened first.

Not sure you're right about this one. "Last" seems more plausible than "first"...


cloudbreaker wrote >>

[...] a more reliable way would be to cycle through all orders in the pool, comparing OrderOpenTime().

...But, anyway, that's right on the money. There's been previous discussion on this forum about the fact that user interface changes seem to affect what EAs see in terms of order history (https://www.mql5.com/en/forum/123269). In the light of that, I wouldn't rely on MT4 returning the list of open trades in any particular sort-order. Even if it works now, the prior discussion suggests that it might break in future. And that wouldn't contravene the documentation, because the documentation doesn't promise a sort-order for OrderSelect().

 
jjc:

Not sure you're right about this one. "Last" seems more plausible than "first"...

the order, which is opened first, is the oldest and therefore the last in the list/index.

the orders are sorted, at least at my MT4, the first added/opened to the list is the last/oldest (OrdersTotal()-1), the last added/opened is the first/newest (0)

2 different ways to describe the same. confusing, yes.

 
DayTrader:

Obviously I Buy at Ask, and Sell at Bid, but am I using Ask and Bid correctly in this HiddenTP code?

 for(int i = OrdersTotal(); i >= 0; i--)
         {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

The use of Ask and Bid looks correct, but the above code isn't. For example, if OrdersTotal() equals 10, then orders 0 through 9 exist. The above code starts the loop with i set to 10, not 9, and the first OrderSelect() call will fail. I think the code's behaviour is then a bit unpredictable because you will then be inspecting whatever is the most recent order which you've previously queried using OrderSelect().


(As a side issue, since we're already being paranoid in this topic, I'd personally check the return value from OrdersTotal(). I've had it fail in real life for supposedly valid calls, about once every three months on average. Presumably down to precise timing issues, I guess. But I still think it's worth checking the return value.)

 
meikel:

the order, which is opened first, is the oldest and therefore the last in the list/index.

To put it another way, I think it's fair to say that CB is questioning the reliability of the word "therefore" in that sentence, and I'd agree with him because it's very little extra work to remove that inherent assumption.

 

jjc wrote >>

(As a side issue, since we're already being paranoid in this topic, I'd personally check the return value from OrdersTotal(). I've had it fail in real life for supposedly valid calls, about once every three months on average. Presumably down to precise timing issues, I guess. But I still think it's worth checking the return value.)

I have seen the same in rare cases, hence I always compare the total number of orders before and after looping on OrderSelect() and if it fails then either restart the loop or flag it as failed:

int total = OrdersTotal();

for( int i=total-1; i>=0; i--)
   {
   OrderSelect(i, SELECT_BY_POS);
   // do whatever...  
   // if we are closing all orders then on every successful order we do total--
   }

if (total!=OrdersTotal())
   {
   // last loop "failed", do something...
   // for example we can put the whole thing in a while loop and restart the for loop again...
   }

 
DayTrader:
Does this point to the last placed (or 2nd last) active order, or to the first or what ?

I had similar question in the past -> https://www.mql5.com/en/forum/121861

 
DayTrader:
Does this point to the last placed (or 2nd last) active order, or to the first or what ?

Last or first is not documented. Normally it points to the last, still active (open or pending) (not closed by TP or SL) order placed by any EA or human. If no orders are open then it is in error: select(-1).


Your EA should only handle order that IT has placed:

for(int index = OrdersTotal() - 1; index >= 0; index--) if (
	OrderSelect(index, SELECT_BY_POS)			// Only my orders w/
&&	OrderMagicNumber()	== MagicNumber + Period.index	// my magic number
&&	OrderSymbol()		== Symbol() ) {			// and period and symbol
   //...
An if the order of placement is important you'll need further checks.
Reason: