# issue with calculating total profit

Hi,

I tried to find a total profit that comes from buy and sell order. To calculate total buy and total sell I use this code,

```//calculating total buy profit
totalBuyProfit += position.Profit() + position.Swap() + position.Commission();

//calculating total sell profit
totalSellProfit += position.Profit() + position.Swap() + position.Commission();

//calculating total profit

When I looked at the calculation there's something wrong with the result of total profit, here's result from log file,

CS 0 18:28:45.409 Core 1 2021.01.04 05:50:00   buy profit: 0.26

CS 0 18:28:45.409 Core 1 2021.01.04 05:50:00   total lock profit: -0.5

CS 0 18:28:45.409 Core 1 2021.01.04 05:50:00   delta profit: 0.76

....

CS 0 18:28:45.409 Core 1 2021.01.04 05:55:00   buy profit: -0.03

CS 0 18:28:45.409 Core 1 2021.01.04 05:55:00   total lock profit: -0.21

CS 0 18:28:45.409 Core 1 2021.01.04 05:55:00   delta profit: 0.18

....

CS 0 18:28:45.409 Core 1 2021.01.04 16:50:00   buy profit: -0.4

CS 0 18:28:45.409 Core 1 2021.01.04 16:50:00   total lock profit: -1.09

CS 0 18:28:45.409 Core 1 2021.01.04 16:50:00   delta profit: 0.6900000000000001

....

CS 0 18:28:45.409 Core 1 2021.01.05 04:40:00   buy profit: 12.1

CS 0 18:28:45.409 Core 1 2021.01.05 04:40:00   total lock profit: -11.34

CS 0 18:28:45.409 Core 1 2021.01.05 04:40:00   delta profit: 23.439999999999998

the equation somewhat wrong, just like the first example 0.26 - 0.5 should be -0.24 and not 0.76 and the other example have a wrong answer also. Can anyone tell me why this happen?

Your code sample is meaningless to us because it lacks code context that you have not shown.

You also have not explained in much detail, such as for example what "locked" profit is.

Fernando Carreiro #:

You also have not explained in much detail, such as for example what "locked" profit is.

"locked" is a sell position that I forgot to change.

```bool CountBuyPosition(int SymbolLoop, int &cntBuy,
{
string CurrentSymbol = SymbolArray[SymbolLoop];
int cntSell    = 0;
double totalSellProfit  = 0;

for(int i=0; i<=PositionsTotal(); i++)
{
ulong ticket = PositionGetTicket(i);
if(position.SelectByTicket(ticket))
if(position.Symbol() == CurrentSymbol)
{
{
{

//check last lot

//check if position is profitable
totalBuyProfit += position.Profit() + position.Swap() + position.Commission();
}
if(position.PositionType() == POSITION_TYPE_SELL)
{
cntSell++;

//check if position profitable
totalSellProfit += position.Profit() + position.Swap() + position.Commission();
}

if(cntSell > 0)
//check if Sell position is profitable
{
Print("total sell profit: ", totalSellProfit);
}

if(cntSell == 0)
{
}
}
}
}
return true;
}```

The sample code is a function to count buy position but in reality is to count buy position strategy (inside of it there's a sell position). The function also return a bool BuyPositionProfitable variable. If this variable return true then I want to close all buy strategy at the next signal. The problem that I face is when total buy profit is negative and total sell profit also negative why the result becomes positive like result below,

CS 0 18:28:45.409 Core 1 2021.01.04 16:50:00   buy profit: -0.4

CS 0 18:28:45.409 Core 1 2021.01.04 16:50:00   total lock profit: -1.09

CS 0 18:28:45.409 Core 1 2021.01.04 16:50:00   delta profit: 0.6900000000000001

and when total buy profit is positive and total sell profit is negative the result becomes positive like result below,

CS 0 18:28:45.409 Core 1 2021.01.04 05:50:00   buy profit: 0.26

CS 0 18:28:45.409 Core 1 2021.01.04 05:50:00   total lock profit: -0.5

CS 0 18:28:45.409 Core 1 2021.01.04 05:50:00   delta profit: 0.76

when -A - (-B) the result should be negative but the print result shows positive. If this a multiplication equation minus multiply by minus the result is positive, but this is addition and subtraction calculation. This is the issue that I faced.

Luandre Ezra #:

"locked" is a sell position that I forgot to change.

The sample code is a function to count buy position but in reality is to count buy position strategy (inside of it there's a sell position). The function also return a bool BuyPositionProfitable variable. If this variable return true then I want to close all buy strategy at the next signal. The problem that I face is when total buy profit is negative and total sell profit also negative why the result becomes positive like result below,

and when total buy profit is positive and total sell profit is negative the result becomes positive like result below,

when -A - (-B) the result should be negative but the print result shows positive. If this a multiplication equation minus multiply by minus the result is positive, but this is addition and subtraction calculation. This is the issue that I faced.
Wrong assertion. The issue is in your mind, the results are correct.
Reason: