OrderProfit() result value abnormality.

 

This is a phenomenon that occurs occasionally.

The profit in the results window is -12.02, but when printed from the source, it comes out as -8.84.

chart & results


logs


[Code]

     for (int i = OrdersHistoryTotal() - 1; i >= 0; i--) {

         if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false) continue;

         if (OrderSymbol() == Symbol() && (OrderMagicNumber() == MAGIC_NUMBER || OrderMagicNumber() == MAGIC_NUMBER + 1)) {

            trade.totalLoss += OrderProfit();

            Print(OrdersHistoryTotal(), "-", i, " ", OrderProfit(), " ", trade.totalLoss);

         }

      }
 
trade.totalLoss += OrderProfit() + OrderCommission() + OrderSwap();

Why do you call this a loss ("totalLoss")? It can also be a profit😄

 

Let's call it a little differently:

double orderProfit = OrderProfit() + OrderCommission() + OrderSwap();

Then you can add it to whatever you want😄:

double orderProfit = OrderProfit() + OrderCommission() + OrderSwap();
trade.totalLoss += orderProfit;

Although, you probably mean something like this:

double orderProfit = OrderProfit() + OrderCommission() + OrderSwap();
if(orderProfit < 0.0)
   trade.totalLoss += orderProfit;
else trade.totalWin += orderProfit;

Well, you get the idea: you do not take into account commission and swap

 
Vladislav Boyko #:

Why do you call this a loss ("totalLoss")? It can also be a profit😄

thank you

It comes out right after adding the fee and swap.

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Forum rules and recommendations
Forum rules and recommendations
  • 2023.03.12
  • www.mql5.com
Each language has its own forum, and it is therefore forbidden to communicate in any language other than that of the forum in question...
 
Vladislav Boyko #:

Let's call it a little differently:

Then you can add it to whatever you want😄:

Although, you probably mean something like this:

Well, you get the idea: you do not take into account commission and swap

Thank you for your detailed answer.


The actual usage source is as follows:

      for (int i = OrdersHistoryTotal() - 1; i >= 0; i--) {
         if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false) continue;
         if (OrderSymbol() == Symbol() && (OrderMagicNumber() == MAGIC_NUMBER || OrderMagicNumber() == MAGIC_NUMBER + 1)) {
            trade.totalLoss += OrderProfit();

            Print(OrdersHistoryTotal(), "-", i, " ", OrderProfit(), " ", trade.totalLoss);

            if (OrderMagicNumber() == MAGIC_NUMBER) break;
         }
      }

I removed the code to print the log.

This is to calculate the previous loss when applying martingale.