Selecting an order based on position

 

Hi,

I'm trying to get my head around the OrderSelect function....specifically how to get the right index parameter.

The following code will select an order that is placed above the market, provided the order was the first one I placed (index=0).

if(Price > Bid && OrderSelect(0,SELECT_BY_POS,MODE_TRADES))

Likewise, the following code will select an order that is placed below the market, provided it is the second order I place (index=1).

if(Price < Ask && OrderSelect(1,SELECT_BY_POS,MODE_TRADES))

How can I modify these such that the correct order is selected based on it's position relative to the current price, when the index number is unknown ?

 

You can load the trades into an Array and then you can sort them anyway you like.

 
kennyhubbard:

You can load the trades into an Array and then you can sort them anyway you like.


Thanks for the reply. I'm a MQL newbie so wouldn't know where to start with an Array, however I got it work anyway by looping through it using

"for(int i=1; i<=OrdersTotal(); i++)"

and changing the functions to

"if (Price > Bid && OrderSelect(i-1,SELECT_BY_POS)==true)"

Thanks again.

 
Why don't you scan all open trades, find it's type and then decide what to do.
for(int pos = OrdersTotal() - 1; pos >= 0; pos--) if (
    OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
&&  OrderMagicNumber() == magic.number              // my magic number
&&  OrderSymbol()      == Symbol() ){               // and period and symbol
    if (OrderType() == OP_BUY) ...
Reason: