Find related orders and positions

 

Hi,

i try to understand the logic of mt5 how to do and handle trades ( Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles)

At the moment i'm really confused.

If i see it correctly, there is no kind of id, wich marks the first trade to open a order and his positions and the positions of the trade to close a order - is it correctly?

How i can find the related position for example:

I have a ticketid "2" this is a buy order.
Later the position is closed by a new opposite, sell order.

How i get the ticketid and informations of the last closing order/position?

I see the different kind of "HistorySelect()", "HistorySelectByPosition()" or "HistoryDealSelect()" etc. but im totaly confused about all this thinks...

May its simple, but i need to understand the logic after all these...

Hope someone can help and explain a bit.

Thank a lot!

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • www.mql5.com
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 
ReLor2: If i see it correctly, there is no kind of id, wich marks the first trade to open a order and his positions and the positions of the trade to close a order - is it correctly?

For every single there is a ticket for that position, but there is also a unique identifier ticket ( POSITION_IDENTIFIER ), which also happens to be the ticket number of the first order that created the original position. This identifier is used by all orders, deals and positions that affect the original position.

POSITION_IDENTIFIER

Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.

Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.

When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.

So, in essence each order has its own ticket, each deal has its own ticket, and each position has its own ticket, but they are all connected by the Position Identifier ticket.
 
Fernando Carreiro #:

For every single there is a ticket for that position, but there is also a unique identifier ticket ( POSITION_IDENTIFIER ), which also happens to be the ticket number of the first order that created the original position. This identifier is used by all orders, deals and positions that affect the original position.

POSITION_IDENTIFIER

Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.

Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.

When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.

So, in essence each order has its own ticket, each deal has its own ticket, and each position has its own ticket, but they are all connected by the Position Identifier ticket.

Hello Fernando,

ok thank you for your explantation. So with the "Position_identifier" i can find all orders, deals and position wich belong to "one complete trade" - correct?


With wich function i can find the datas form order,deals,positions?

If i start with a ticketid (example "2") , what will be the next steps to get the "Position_identifier" and how i can find with this the other Positions, Orders, Deals and also get their own datas?

Also i read thats important to select position/deals/orders at first...!?


 
ReLor2 #: Hello Fernando, ok thank you for your explantation. So with the "Position_identifier" i can find all orders, deals and position wich belong to "one complete trade" - correct? with wich function i can find the datas form order,deals,positions? If i start with a ticketid (example "2") , what will be the next steps to get the "Position_identifier" and how i can find with this the other Positions, Orders, Deals and also get their own datas? Also i read thats important to select position/deals/orders at first...!?

If "ticketid" is for the very first order, that created the position, then it is also the Position_identifier.

So, obtain your history by that identifier ...

HistorySelectByPosition

Requests the history of deals with a specified position identifier.

You can then process all the Orders and Deals for that position ...

Scarning the orders with:

HistoryOrdersTotal

Returns the number of orders in the history

HistoryOrderGetTicket

Return order ticket of a corresponding order in the history

Scanning the deals with:

HistoryDealsTotal

Returns the number of deals in the history

HistoryDealGetTicket

Returns a ticket of a corresponding deal in the history

Reason: