How to get the accurate profit of a closed Order?

 

Please refer to the screenshot below.

The profit is printed with OrderProfit()  once detecting OrderClose(...) returns "true". There's no problem with back testing.

During the live test, you can see OrderProfit() of a closed order was printed in the experts log as "1.40".  However, the accurate profit is "3.44" as shown as in "Account History".

Therefore, it seems OrderProfit() can't get accurate profit of a closed order.  Then what code can be used to get the accurate profit of a closed order? Thanks in advance. 

 

 

 
Show the code and include the ticket number in the Print
 
GumRai:
Show the code and include the ticket number in the Print

 

Dear GumRai,

 

There is only 1 trade on 1 symbol that day. The ticket number is 100% same as the one in the screenshot. 

. According to the expert log, the order was closed with Bid/Ask, 9.31973/9.33045. According to OrderPrint(), it's closed at price 9.33045. Based on this price, the OrderProfit is 1.40.

. According to the account history, the order was closed at 9.32915. Based on this price, the OrderProfit is 3.44. 

. Certainly account history  should be correct with the closed price 9.32915 after slippage happened. Then why OrderProfit() and OrderPrint() calculated the profit with the Ask price when closing order? Is it a bug of MQL4 functions?

. It's highly appreciated if you could help suggest some code to work around to get the correct profit result.

         if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true && OrderMagicNumber()==MAGIC && OrderSymbol()==Symbol())
           {
            if(OrderOpenPrice()-Ask>=curProfitL*pipPoints)
              {
               r=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,clrDodgerBlue);
               if(r)
                 {
                  profit=OrderProfit();
                  commission=OrderCommission();
                  swap=OrderSwap();
                  orderProfit=profit+commission+swap;
                  PrintFormat("CP.S1_CloseSell with Profit, %.2f+%.2f+%.2f, Slippage Target, %i, and Bid/Ask, %s/%s.",profit,commission,swap,slippage,DoubleToStr(Bid,Digits),DoubleToStr(Ask,Digits));                 
                 }
 

Although that is quite a difference it is still possible that it is because you are using out of date data.

You must re-select the order after it has closed 

               r=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,clrDodgerBlue);
               if(r && OrderSelect(OrderTicket(),SELECT_BY_TICKET))
                 {
                  close_price=OrderClosePrice();
                  profit=OrderProfit();
                  commission=OrderCommission();
                  swap=OrderSwap();
                  orderProfit=profit+commission+swap;
                  PrintFormat("CP.S1_CloseSell with Profit, %.2f+%.2f+%.2f, Slippage Target, %i, and Bid/Ask, %s/%s.",profit,commission,swap,slippage,DoubleToStr(Bid,Digits),DoubleToStr(Ask,Digits));                 
                 }

 and don't use Bid/Ask that may also be out of date. Use OrderClosePrice().

 
GumRai:

Although that is quite a difference it is still possible that it is because you are using out of date data.

You must re-select the order after it has closed 

 and don't use Bid/Ask that may also be out of date. Use OrderClosePrice().  

Dear Sir, thanks a lot. Let me try to test further. Will let you know if no problem found for a period. 
 
moonsboy:


Dear GumRai,

 

There is only 1 trade on 1 symbol that day. The ticket number is 100% same as the one in the screenshot. 

. According to the expert log, the order was closed with Bid/Ask, 9.31973/9.33045. According to OrderPrint(), it's closed at price 9.33045. Based on this price, the OrderProfit is 1.40.

. According to the account history, the order was closed at 9.32915. Based on this price, the OrderProfit is 3.44. 

. Certainly account history  should be correct with the closed price 9.32915 after slippage happened. Then why OrderProfit() and OrderPrint() calculated the profit with the Ask price when closing order? Is it a bug of MQL4 functions?

. It's highly appreciated if you could help suggest some code to work around to get the correct profit result.


Bro whn ever i tried to close a buy or sell trade fr example bid-orderopenprice()>5 ..close at bid,bt it always closes in loss..can u plz help me in adding the code..like hw to close the trade exluding broker any broker comission ,spread n closing the trade in profit..thank u

 
legendofcibola: i tried to close a buy or sell trade fr example bid-orderopenprice()>5 ..close at bid,bt it always closes in loss..can u plz help me in adding the code..like hw to close the trade exluding broker any broker comission ,spread n closing the trade in profit..thank u
  1. Don't Hijack other threads for your off-topic post. Next time, make your own, new, thread.

  2. Write English. "Bro", "fr", "bt", "u", "plz", "hw", "n", "u" are not words. Please post only in English on this forum.

              Please don't write ur - it's "you are" or "your" - MQL4 programming forum 2014.03.04

  3. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. We can't see your broken code.

  4. We are not your "Bro"

Reason: