# Select lower buy / higher sell order

1405

Have seen response to select last open order, but not if we need to select last buy or last sell. I have this functions but no way.

```// Code for LowerBuy

double LastBuy(){
double Price=0;
for(int i=0;i<BuyOrders();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderType()==OP_BUY){Price=OrderOpenPrice();}
}
}
return(Price);
}

// Code for HigherSell

double LastSell(){
double Price=0;
for(int i=0;i<SellOrders();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderType()==OP_SELL){Price=OrderOpenPrice();}
}
}
return(Price);
}```

Hope someone can help, thank you in advance.

1209

David Diez:

Have seen response to select last open order, but not if we need to select last buy or last sell. I have this functions but no way.

Hope someone can help, thank you in advance.

I assume that your last buy is also the buy with the lowest open price? Then you can do this:

```double LastBuy()
{
double Price=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_BUY)
{
double OP = OrderOpenPrice();
if (OP<Price || Price==0)
Price = OP;
}
}
}
return(Price);
}
```

From this you should be able to do likewise for Sell.

1405

Seng Joo Thio:

I assume that your last buy is also the buy with the lowest open price? Then you can do this:

From this you should be able to do likewise for Sell.

It worked. Also in order to reduce the code I need to take this two to one single function:

```double LastBuyLot(){
double Lot=0;
for(int i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderType()==OP_BUY){
if(OrderLots()>Lot){
Lot=OrderLots();
}
}
}
}
return(Lot);
}

double LastSellLot(){
double Lot=0;
for(int i=0;i<OrdersTotal();i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderType()==OP_SELL){
if(OrderLots()>Lot){
Lot=OrderLots();
}
}
}
}
return(Lot);
}```

I tried to get both values from a single loop but didn't worked, gave me the last higher open lot being buy or sell.

1209

David Diez:

It worked. Also in order to reduce the code I need to take this two to one single function:

I tried to get both values from a single loop but didn't worked, gave me the last higher open lot being buy or sell.

From your code the returned value will correspond to the LARGEST Buy lot, and LARGEST Sell lot, rather than the last, or highest/lowest for each.

I believe that the sequence of orders retrieved by OrderSelect() may not be fixed - so it makes sense to compare price to get highest/lowest, and compare time to get the latest or oldest.

1405

Seng Joo Thio:

From your code the returned value will correspond to the LARGEST Buy lot, and LARGEST Sell lot, rather than the last, or highest/lowest for each.

I believe that the sequence of orders retrieved by OrderSelect() may not be fixed - so it makes sense to compare price to get highest/lowest, and compare time to get the latest or oldest.

Well, it works as I need, just looking for the way to simplify code.
To add comments, please log in or register