# How to handle and calculate positions on a netting account

Hello there! I'm converting my expert advisor to Metatrader 5. With hedging, the handling and calculation of positions is quite reasonable.
I might need to stay away from netting accounts. But how do you handle positions with netting accounts?

Example 1:
You buy 0.1 lots of GBPUSD at 9 am and close(sell) 0.1 lots of GBPUSD at 6 pm
09:00: Opening order -> opening deal -> position open
18:00: Closing order -> closing deal -> position closed
One position ticket, reasonable position results. Open time, close time and profit are obvious.

Example 2:
You buy 0.1 lots of GBPUSD at 9 am and another 0.1 lots at 3 pm. You close(sell) 0.1 lots at 6 pm and another 0.1 lots at 10pm.
09:00: Opening order -> opening deal -> position opened, 0.1 lots
15:00: Opening order -> opening deal -> position increased by 0.1 lots, now 0.2 lots. STILL same position ticket. What's now the position's open price? Is it a recalculated open price, the first or the last open price?
18:00: Closing order -> closing deal -> position decreased by 0.1 lots. How do you know what part of the position will be closed first? Those 0.1 lots you bought in the morning or the other 0.1 lots you bought in the afternoon? Or is a recalculated open price used to get the partly close profit?
22:00: Closing order -> closing deal -> position closed

One position ticket, several open times, several open prices, several close times, several close prices, several closing deals and profits.
How do you calculate the position's complete results and partly results? How can I create a table of trading results?

Example 3:
Three or more opening orders, same position ticket...

Partial closes and more opening orders, then close etc.

Thank you

Any solutions?

How to get mismatched error 0 in back testing an EA?

I found a solution.

Best Regards

Rafael Judson Lima Valle:

I found a solution.

Best Regards

What shall that be? There is no such function, at least not in Build 2755 (last stable).

------

HistorySelectByPosition() is your friend. See documentation for examples.

You just have to loop through all the deals of any position - no matter if the account is hedging or netting, cause there can also be partial closings in a hedging account. If you don´t, you will get wrong results.

Every deal of every positions gives you clear information about the volume and the result of any deal of such a position. You dont have to create a separate table, the table is already provided and it is the deal-history.

On netting accounts sometimes broker opens position by parts, for example you need to open 10 lots, and broker will open deals: 2 lot + 2 lot + 6 lot. In that case to get right open positions number following method can be used:

```COrderInfo oi;
CPositionInfo p;

int getPositionsTotalNetting()
{
p.Select(_Symbol);
long id = p.Identifier();
HistorySelectByPosition(id);
int totalOpenPositions= HistoryOrdersTotal();