How to chain partially closed posiions with remaining opened positions in MQL5?

 

Wohoo .. sorry for the title, but I don´t know how to explain in one sentence what I am looking for ;)

Well, in MT4, when a position is closed partially, MT4 creates a new position with a new ticket. The partially closed one keeps alive, is tagged as closed and the commend field contains an information like "to #123456". Means, the original position is closed entirely, contains the profit information, closing time etc. while the comment field leads me to a new position with a new ticket (to #ticket). This new position then has the size/volume of the remaining part which is not closed. Furhtermore, the new position contains a comment such as "from #123455" and shows me from which original position it was created.

This way I can easily track the profits, the execution quality and so on.

In MT5 I cannot find any way to track partial closings. When an opened position is closed, no matter if partially or entirely, MQL5 creates an entry in the history-table with a new ticket number, not containing any informations from which original ticket this comes. Currently I have no (good) idea how to figure out, where and how to find the original ticket which belongs to the history-ticket. Do I overlook something here?

 
Doerk Hilger :

Wohoo .. sorry for the title, but I don´t know how to explain in one sentence what I am looking for ;)

Well, in MT4, when a position is closed partially, MT4 creates a new position with a new ticket. The partially closed one keeps alive, is tagged as closed and the commend field contains an information like "to #123456". Means, the original position is closed entirely, contains the profit information, closing time etc. while the comment field leads me to a new position with a new ticket (to #ticket). This new position then has the size/volume of the remaining part which is not closed. Furhtermore, the new position contains a comment such as "from #123455" and shows me from which original position it was created.

This way I can easily track the profits, the execution quality and so on.

In MT5 I cannot find any way to track partial closings. When an opened position is closed, no matter if partially or entirely, MQL5 creates an entry in the history-table with a new ticket number, not containing any informations from which original ticket this comes. Currently I have no (good) idea how to figure out, where and how to find the original ticket which belongs to the history-ticket. Do I overlook something here?


You need to collect the positions from the trading history.

HistoryPositionInfo version 2.

 

when close position, I code EA write comment on position like "partial#1"

 
Doerk Hilger:

In MT5 I cannot find any way to track partial closings. When an opened position is closed, no matter if partially or entirely, MQL5 creates an entry in the history-table with a new ticket number, not containing any informations from which original ticket this comes.

This sounds like a bug in your code. Can you provide more details?

 
Nguyen Nga:

when close position, I code EA write comment on position like "partial#1"


And when the position or a part of it becomes closed with MT5 directly or with MT5 mobile? How do you know that this closing belongs to which position? Its not a problem to handle it somehow in the EA when the EA closes positions, but I need to track them no matter how these positions have been closed.

 
Doerk Hilger:

And when the position or a part of it becomes closed with MT5 directly or with MT5 mobile? How do you know that this closing belongs to which position? Its not a problem to handle it somehow in the EA when the EA closes positions, but I need to track them no matter how these positions have been closed.


Each position have one ID (POSITION_IDENTIFIER), when it closed, you can get it from history with DEAL_POSITION_ID

 
Nguyen Nga:

Each position have one ID (POSITION_IDENTIFIER), when it closed, you can get it from history with DEAL_POSITION_ID


I know, but the question is how to connect it to the original position afterwards?

Example, I open a position with 10 contracts and its in the market for one week. In between I close it particullary 20 times with different sizes. I added further 30 particular positions and closed again 30. This means, I have 51 deals, but ONE trade. How can I figure out at what profit this ONE trade is, including all the particular closings and opens in between etc.?

My current answer is: In MT5 I either cannot or I am forced to scan the whole history table during each single OnTick(), in such case this means 50 additional loops through the history. Correct?

And if you think 50 is a lot - it´s not. Some of my users trade with grids, and all is connected, here we talk about several hundred loops. 

 
Doerk Hilger: I know, but the question is how to connect it to the original position afterwards?

Example, I open a position with 10 contracts and its in the market for one week. In between I close it particullary 20 times with different sizes. I added further 30 particular positions and closed again 30. This means, I have 51 deals, but ONE trade. How can I figure out at what profit this ONE trade is, including all the particular closings and opens in between etc.?

My current answer is: In MT5 I either cannot or I am forced to scan the whole history table during each single OnTick(), in such case this means 50 additional loops through the history. Correct?

And if you think 50 is a lot - it´s not. Some of my users trade with grids, and all is connected, here we talk about several hundred loops. 

I think you may be mixing up the concepts of Orders, Deals and Positions.

Lets say you sell at market for 0.10 volume, producing - Order #101: Sell for 0.10, Deal #201: Sell/In for 0.10, Position #301: Sell for 0.10.

Now let's say you do a partial close of 0.07, producing - Order #102: Buy for 0.07, Deal #202: Buy/Out for 0.07, Position: none (no extras, it still the same one as above).

Then finally you close the remaining 0.03, producing - Order #103: Buy for 0.03, Deal #203: Buy/Out for 0.03, Position: none (no extras, it still the same one as above).

So in the end you only had 1 Position, 3 Deals, and 3 Orders!

EDIT: So by querying the Order Properties or the Deal Properties, you can obtain the Position ID (ORDER_POSITION_ID or DEAL_POSITION_ID respectively).

EDIT2: Please note that even though I used #301 for the Position, it will probably be #101 as per the documentation:

Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening.

EDIT3: To simplify things just ignore the Deals in the above example, as there can be several deals per Order. Consider only the Orders and the Position!
 

Yes, but I loose the chain then. For my understanding as a trader, the PL of a position is  not interesting at all, the only thing that matters is the PL of a trade, and a trade can consist of many closings and openings in between, a trade is calculated from the first position until I am flat. And all this InBetween needs to be calculated properly.

And what I am looking for is a method which does not force me to check all the history with every tick, where I can find the deals which lead to positions. Such a calculation is pretty complex and eats a lot of CPU time which I cannot afford in volatile markets.

 
Doerk Hilger:

Yes, but I loose the chain then. For my understanding as a trader, the PL of a position is  not interesting at all, the only thing that matters is the PL of a trade, and a trade can consist of many closings and openings in between, a trade is calculated from the first position until I am flat. And all this InBetween needs to be calculated properly.

And what I am looking for is a method which does not force me to check all the history with every tick, where I can find the deals which lead to positions. Such a calculation is pretty complex and eats a lot of CPU time which I cannot afford in volatile markets.

How can you loose the "chain"? All the Orders and Deals keep a pointer to the Position and the Deals keep pointers to the Orders. It is a Linked list!

You don't have to keep checking the history every tick. Just keep a "running tab" - an organised structure/class of what is needed. The History (of closed items) does not change either, it just gets appended to, so just keep a time/date pointer into the last data analysesd. As for current data, there is no need to check every tick. Either spread out the loop over several ticks, for example one element per tick, or you could also, only loop through the current data, when price has moved more that x pips/points.

Also, don't forget the OnTrade() and OnTradeTransaction() events which will also help reduce the amount of checking.

In essence, don't think in terms of MT4/MQL4. Rethink this entire trading part as specific to MT5/MQL5 and then if possible find the MT4 work-around or completely separate the two in separate libraries and create wrappers for them for the shared code.

 

I never thought in terms of MT4, my class lib for MT4 was always able to handle netting and hedging, I am just looking for a way which is as less complex as possible. This class is already a kind of a wrapper class. But youre right, the OnTrade() events could make their job here. Thanks for your effort, Fernando. 

Reason: