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();
Print("buy profit: ",totalBuyProfit);

//calculating total sell profit
totalSellProfit += position.Profit() + position.Swap() + position.Commission();
Print("total lock profit: ", totalBuyLockProfit);

//calculating total profit
totalProfit = totalBuyProfit - totalBuyLockProfit;
Print("total profit: ",totalBuyProfit - totalBuyLockProfit);

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.

So, I suggest you use the built-in debugger in MetaEditor, or add more "Prints" in your code to help you solve your issue.

 
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,
                       double &lastLotBuy,
                       int MagicNumberBuy,
                       double &totalBuyProfit,
                       double &totalProfitBuy,
                       bool &BuyPositionProfitable)
  {
   string CurrentSymbol = SymbolArray[SymbolLoop];
   cntBuy         = 0;
   int cntSell    = 0;
   totalBuyProfit = 0;
   double totalSellProfit  = 0;
   lastLotBuy     = 0;
   totalProfitBuy = 0;
   BuyPositionProfitable = false;

   for(int i=0; i<=PositionsTotal(); i++)
     {
      ulong ticket = PositionGetTicket(i);
      if(position.SelectByTicket(ticket))
         if(position.Symbol() == CurrentSymbol)
           {
            if(position.Magic() == MagicNumberBuy)
              {
               if(position.PositionType() == POSITION_TYPE_BUY)
                 {
                  //count buy position
                  cntBuy++;

                  //check last lot
                  if(lastLotBuy == 0 || lastLotBuy < position.Volume())
                     lastLotBuy = position.Volume();

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

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

               if(cntSell > 0)
                  //check if Sell position is profitable
                  if(totalBuyProfit - totalSellProfit > 0.0)
                    {
                     totalProfitBuy = totalBuyProfit - totalSellProfit;
                     Print("total buy profit: ",totalBuyProfit);
                     Print("total sell profit: ", totalSellProfit);
                     Print("delta profit: ",totalBuyProfit - totalSellProfit);
                     BuyPositionProfitable = true;
                    }

               if(cntSell == 0)
                  if(totalBuyProfit > 0.0)
                    {
                     totalProfitBuy = totalBuyProfit;
                     BuyPositionProfitable = true;
                    }
              }
           }
     }
   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.