How to get the accurate profit of a closed Order?

Back to topics list To post a new topic, please log in or register
avatar
228
jollydragon 2015.09.21 08:12
 

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. 

 

 

avatar
Moderator
3418
GumRai 2015.09.21 09:33 #
 
Show the code and include the ticket number in the Print
avatar
228
jollydragon 2015.09.22 06:40 #
 
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));                 
                 }

 

avatar
Moderator
3418
GumRai 2015.09.22 13:49 #
 

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().

 

avatar
228
jollydragon 2015.09.23 02:55 #
 
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. 
Back to topics list  

To add comments, please log in or register