Unable to identify which position is closed/mt5.history_deals_get

 
Hi,  I am using  mt5.history_deals_get to download all deals. One issue I am facing is when multiple position is opened , and if I close them in a different order, it is quite difficult to identify which one is closed. There is no common identifier for the same trade (deal showing as as separate open , and closed trade with different identifiers). L know deals has to be separated that way. But is there anything I am missing or any other alternative to connect the open and closed positionwith script , on python. Thanks
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Forum on trading, automated trading systems and testing trading strategies

how can Orders, Positions and Deals be linked together?

Fernando Carreiro, 2023.11.18 13:31

They are linked by their respective tickets and especially overall by their Position Identifier ticket which is usually the same as the Order ticket that first created the position.

All subsequent orders and deals that affect that position, will reference that unique identifier that is assigned to all orders and deals executed for the entire lifetime of the position.

ENUM_POSITION_PROPERTY_INTEGER

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.

long


ENUM_ORDER_PROPERTY_INTEGER

ORDER_POSITION_ID

Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of exactly this position is set to the executed order at this moment.

long

ENUM_DEAL_PROPERTY_INTEGER

DEAL_POSITION_ID

Identifier of a position, in the opening, modification or closing of which this deal took part. Each position has a unique identifier that is assigned to all deals executed for the symbol during the entire lifetime of the position.

long

Also, please read the following ...

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

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.
 
The previous post is about MQL5, but the same applies to Python, using the equivalent functions for Python instead of MQL5.
 
Fernando Carreiro #:
Thanks for reply.
 
Fernando Carreiro #:
The previous post is about MQL5, but the same applies to Python, using the equivalent functions for Python instead of MQL5.
I cannot see any matching identifiers for those deals ...all four were opened  positions before started closing any  one of them. But how can I retrieve those info via python or other alternative...when there is no common identifier..If I am missing some thig ..please notify..Thanks for your time,.
Files:
deals_2.png  60 kb
deals_1.csv  1 kb
 
1057102143 #: I cannot see any matching identifiers for those deals ...all four were opened  positions before started closing any  one of them. But how can I retrieve those info via python or other alternative...when there is no common identifier..If I am missing some thig ..please notify..Thanks for your time,.

Where is the "position_id"?

Your table only has the order id and deal id, but not the Position identifier. That is the whole point of my previous post.

If you need help with your code, then please show it.