# Function Ticket[n] 219

Hi!
Exist a function analogous to Close[n] that returns the last ticket number of the opened position in function of n?
Like this:
Ticket = 12340002 (sell)
Ticket = 12340004 (sell)
Ticket = 12340005 (sell)
Ticket = 12340007 (sell)
And also for buy and sell, like this:

TicketSell = 12340002
TicketSell = 12340004
TicketSell = 12340005
TicketSell = 12340007 197740

JHenry:
Hi!
Exist a function analogous to Close[n] that returns the last ticket number of the opened position in function of n?
Like this:
Ticket = 12340002 (sell)
Ticket = 12340004 (sell)
Ticket = 12340005 (sell)
Ticket = 12340007 (sell)
And also for buy and sell, like this:

TicketSell = 12340002
TicketSell = 12340004
TicketSell = 12340005
TicketSell = 12340007

There is no such function

You have to go through the list of opened orders, save the ticket numbers in an array, sort the array and then you can have something like you want. It could be done similar to this :

```int ordersByTicket[];
for (int i=OrdersTotal()-1,i>=0, i--)
{
ArrayResize(ordersByTicket,ArraySize(ordersByTicket)+1);
ordersByTicket[ArraySize(ordersByTicket)-1] = OrderTicket();
}
ArraySort(ordersByTicket,WHOLE_ARRAY,MODE_DESCEND);
ordersByTicket == // last (biggest) ticket number and so on
``` 219

Mladen, could you create the code of the these 6 functions below for me?

TicketNumber = N[n]
TicketNumber = M[m]
TicketNumber = X[x]
TicketNumber = Y[y]
TicketNumber = V[v]

TicketNumber = W[w]

Order     = N[n] = M[m]
142370816 = N = M
142370788 = N = M
142370758 = N = M
:
:
:
142370606 = N = M
142370565 = N = M
142370543 = N = M

Order     = X[x] = Y[y]
142370816 = X = Y
142370788 = X = Y
142370701 = X = Y
:
:
:
142370606 = X = Y
142370565 = X = Y
142370543 = X = Y

SELL Positions:

Order     = V[v] = W[w]
142370758 = X = Y
142370743 = X = Y
142370711 = X = Y
:
:
:
142370679 = X = Y
142370645 = X = Y
142370631 = X = Y

Do you undestand?? 197740

JHenry:
Mladen, could you create the code of the these 6 functions below for me?

TicketNumber = N[n]
TicketNumber = M[m]
TicketNumber = X[x]
TicketNumber = Y[y]
TicketNumber = V[v]

TicketNumber = W[w]

ALL Positions:

Order     = N[n] = M[m]
142370816 = N = M
142370788 = N = M
142370758 = N = M
:
:
:
142370606 = N = M
142370565 = N = M
142370543 = N = M

Order     = X[x] = Y[y]
142370816 = X = Y
142370788 = X = Y
142370701 = X = Y
:
:
:
142370606 = X = Y
142370565 = X = Y
142370543 = X = Y

SELL Positions:

Order     = V[v] = W[w]
142370758 = X = Y
142370743 = X = Y
142370711 = X = Y
:
:
:
142370679 = X = Y
142370645 = X = Y
142370631 = X = Y

Do you undestand??

I don't (understand)

If you know the exact ticket numbers you want to access, then simply use OrderSelect(ticketNumber,SELECT_BY_TICKET,MODE_TRADES); function 219

No! I don't know the ticket number :S

WHat I want to do is the following:

I speaking for the EA:
"EA, close the first buy position!"
"EA, close the last sell position!"
"EA, close the three last position!"
"EA, close the last position and the 2nd of buy and 5nd of sell!"

In other words, I want to control which position the EA will close by "1st", "2nd", ..., "penultimate" and "last". 197740

JHenry:

No! I don't know the ticket number :S

WHat I want to do is the following:

I speaking for the EA:
"EA, close the first buy position!"
"EA, close the last sell position!"
"EA, close the three last position!"
"EA, close the last position and the 2nd of buy and 5nd of sell!"

In other words, I want to control which position the EA will close by "1st", "2nd", ..., "penultimate" and "last".

From the code I posted you can see which order is at what position in the list of currently opened orders - the rest you can get from positioning to desired position from the array that contains the ticket numbers 3231

JHenry:

No! I don't know the ticket number :S

WHat I want to do is the following:

I speaking for the EA:
"EA, close the first buy position!"
"EA, close the last sell position!"
"EA, close the three last position!"
"EA, close the last position and the 2nd of buy and 5nd of sell!"

In other words, I want to control which position the EA will close by "1st", "2nd", ..., "penultimate" and "last".

Aren't you making it too complicated? 219

Exist other form more simple? 3231

JHenry:
Exist other form more simple?
I was referring to the lack of rules (what order should be closed and when - just saying No.nnn or No.mmm means nothing)