Order profit positive in MT4 app, but OrderProfit() returns negative value

 

Hello guys,

I'm facing an issue with my code: I want to check profit for a closed order, using OrderProfit().

In MT4 app, this trade is like this in history:

In logs here are information coming from my code:

In my code I use OrderSelect() in history with ticket ID (OrderSelect(ID, SELECT_BY_TICKET, MODE_HISTORY)).

Then I check OrderProfit().


Why is it negative while it was closed ith positive profit?


Thanks.

 
sebastien444:

Hello guys,

I'm facing an issue with my code: I want to check profit for a closed order, using OrderProfit().

In MT4 app, this trade is like this in history:

In logs here are information coming from my code:

In my code I use OrderSelect() in history with ticket ID (OrderSelect(ID, SELECT_BY_TICKET, MODE_HISTORY)).

Then I check OrderProfit().


Why is it negative while it was closed ith positive profit?


Thanks.

double positive_value = OrderProfit();
double value = 0;

if(positive_value > 0) value = MathAbs(positive_value);

Try this

 
Are you sure you are selecting the exact same trade in history. To make sure log other properties like OrderCloseTime.
 
Adj007 #:

Try this

Thanks.

Unfortunately OrderProfit() in if will return a negative value.
Even if it's positive in MT4 (column Profit, $61.80).

How to get this value?

I don't know what is -5.4 and where it comes from.

 
Yashar Seyyedin #:
Are you sure you are selecting the exact same trade in history. To make sure log other properties like OrderCloseTime.
OrderSelect() succeeds with this ID so I don't what to do more.
I can't understand where -5.4 comes from, it's nowhere in history.

Thanks.

 

pool=MODE_TRADES

[in]  Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any of the following values:

MODE_TRADES (default)- order selected from trading pool(opened and pending orders),
MODE_HISTORY - order selected from history pool (closed and canceled order).

https://docs.mql4.com/trading/orderselect


So in case SELECT_BY_TICKET, it is not needed to use MODE_HISTORY. I guess you do not need the third input parameter at all.

OrderSelect - Trade Functions - MQL4 Reference
OrderSelect - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderSelect - Trade Functions - MQL4 Reference
 
Excellent! My bad.. #RTFM

Thanks guys.

 
sebastien444 #:
Excellent! My bad.. #RTFM

Thanks guys.

Just re-checked my code and its actually quite bad. OrderProfit() returns a trailing value if called upon every tick when a position is open or not.

Here is a much better code, compile is successful and tested, a much better version that makes more sense than the previous code I put out.

void PrintProfitIfTakeProfitReached()
{
    int totalOrders = OrdersHistoryTotal();
    for (int i = 0; i < totalOrders; i++) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
            double takeProfitLevel = OrderTakeProfit();
	    double profitValue = OrderProfit();


            
            if (MathAbs(takeProfitLevel <= Bid) || MathAbs(takeProfitLevel <= Ask)) {
                Print("Order Profit:", profitValue);
            }
        }
    }
}
 
Adj007 #:

Just re-checked my code and its actually quite bad. OrderProfit() returns a trailing value if called upon every tick when a position is open or not.

Here is a much better code, compile is successful and tested, a much better version that makes more sense than the previous code I put out.

Thanks bro!

I changed my code to check close time, but I pin your code which seems ok too.

++

 
Adj007 #:

    int totalOrders = OrdersHistoryTotal();
    for (int i = 0; i < totalOrders; i++) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {

MT4:

  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017)

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - «TICKET»
    Rollover - «TICKET»

    >R/O - 1,000 EUR/USD @0.52

    #«ticket»  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

 
William Roeder #:

MT4:

  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017)

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - «TICKET»
    Rollover - «TICKET»

    >R/O - 1,000 EUR/USD @0.52

    #«ticket»  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

3. The code is fine for someone who does not consider commission or swaps, what if someone who reads this are not paying for commission and have a standard account and close before the end of the day? No, just get ahead of yourself bud. 

2. This code is intended to get the last recent orders profit, works for me, works perfectly fine actually. 

1. History can potentially have opened, closed & probably deleted orders too, I don't know I haven't tested that. 


For someone who is looking for something basic like this is better than telling them to pay for a freelancer or copy & paste your 1990s link about how to rtfm. Thanks though. 

Reason: