# Return highest buy or lowest sell ticket number

Hello Fellow coders,

I'm getting myself into a bit of a flap. I am trying to write a function so that it will return the ticket number of my highest buy trade or lowest sell trade, so they can be isolated from the basket and closed.

I know I need to find the correct ticket number of the above trades so they can be returned into the function to close that ticket. I'm just not sure how to write it correctly any help or pointing into the right direction would be much appreciated.

Hello Fellow coders,

I'm getting myself into a bit of a flap. I am trying to write a function so that it will return the ticket number of my highest buy trade or lowest sell trade, so they can be isolated from the basket and closed.

I know I need to find the correct ticket number of the above trades so they can be returned into the function to close that ticket. I'm just not sure how to write it correctly any help or pointing into the right direction would be much appreciated.

Code:

```//+------------------------------------------------------------------+
//| Calculate all positions                                          |
//+------------------------------------------------------------------+
{
ticket_lowest_sell   = ULONG_MAX;
//--- auxiliary variables
double price_lowest_sell   = DBL_MAX;
//---
for(int i=PositionsTotal()-1; i>=0; i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
{
{
continue;
}
else
if(m_position.PositionType()==POSITION_TYPE_SELL)
{
if(m_position.PriceOpen()<price_lowest_sell) // the lowest position of "SELL" is found
ticket_lowest_sell=m_position.Ticket();
}
}
}```

Code:

Perfect that gives me everything I need to put into my own code.. thank you

```               if(m_position.PriceOpen()>price_highest_buy) { // the highest position of "BUY" is found
}```

same for price_lowest_sell

lippmaje #:

same for price_lowest_sell

Can you write same code for mql4, please

Jagdish Vasave - # :
Can you write same code for mql4, please

You are on the MQL5 forum - all questions about MQL5 are discussed here.