How to Re-Calculate OrderProfit ?

 

Hi,

I know the question has been asked few times but I cannot get the same amount as OrderProfit() by doing this (when this is a profitable position > 0)

profit = MathAbs(price - OrderOpenPrice ()) * OrderLots () * MarketInfo (Symbol (), MODE_TICKVALUE) / Point;

profit += OrderSwap () + OrderCommission ();



Any idea ?
I was thinking about the spread but even if I remove the pip of the spread I've always a bigger value than the OrderProfit()..

:(

This is problematic because I would like to use this formula to compute the potential profit at an arbitrary close price from the openprice. For exemple at the SL or TP price.

Thank for help!
 

price is the close price?

The formula look right to me (apart from potential problems with TICKVALUE)

Did you check the there's no problems with the variables going in?

 
DavidS777:

Hi,

I know the question has been asked few times but I cannot get the same amount as OrderProfit() by doing this (when this is a profitable position > 0)


Any idea ?
I was thinking about the spread but even if I remove the pip of the spread I've always a bigger value than the OrderProfit()..

:(

This is problematic because I would like to use this formula to compute the potential profit at an arbitrary close price from the openprice. For exemple at the SL or TP price.

Thank for help!
You don't have to add OrderSwap() and OrderCommission() if you want to compare your calculated profit with OrderProfit().
 

@alladir :thanks for your reply.

"price" can be the "close price" or current bid or ask price or any price we want.

But typically if I'm using Prices/Volume... of my positions in MT4 with this forumla, Then compare to the realtime "profit column" in MT4 = OrderProfit() : I've not the same value, especially for XAUUSD. (I always get much a bigger value than what the one displayed in MT4.) Don't you think the pip of the spread should be included in the formula ? Like substract them... In fact I tried but this is not enough.

Same problem with current TP/SL. In the last version of MT4 while moving the SL / TP manually you can read the number of pip and potential gain/loss from the position. This is a nice feature.
Again, I cannot compute with "my formula" the same value as the one displayed. I'm using the same number of pip like the one displayed...

Really strange. I'm sure the problem is from me... but well I don't see what I missed.

ps: (sorry for bad english I'm non-english user)


@angevoyageur :

in fact my swap and commission or currently = 0 but the price is definitively not the same.

I'll checking one more time my code. and will give a concrete sample as necessary.

 
DavidS777:

@alladir :thanks for your reply.

"price" can be the "close price" or current bid or ask price or any price we want.

But typically if I'm using Prices/Volume... of my positions in MT4 with this forumla, Then compare to the realtime "profit column" in MT4 = OrderProfit() : I've not the same value, especially for XAUUSD. (I always get much a bigger value than what the one displayed in MT4.) Don't you think the pip of the spread should be included in the formula ? Like substract them... In fact I tried but this is not enough.

Same problem with current TP/SL. In the last version of MT4 while moving the SL / TP manually you can read the number of pip and potential gain/loss from the position. This is a nice feature.
Again, I cannot compute with "my formula" the same value as the one displayed. I'm using the same number of pip like the one displayed...

Really strange. I'm sure the problem is from me... but well I don't see what I missed.

ps: (sorry for bad english I'm non-english user)


@angevoyageur :

in fact my swap and commission or currently = 0 but the price is definitively not the same.

I'll checking one more time my code. and will give a concrete sample as necessary.


TICK_VALUE gives you the value of a tick, not the value of a point. Specially for XAUDUSD Point is not the same as TICK_SIZE.
 
you mean I have to use TICK_SIZE ? I don't understand this value. It is equal to the value of a point ?
 
DavidS777:
you mean I have to use TICK_SIZE ? I don't understand this value. It is equal to the value of a point ?
See this topic https://www.mql5.com/en/forum/145362
 

Basically.. do tickvalue / ticksize * point to get the value of one point. This takes care of all possible situations, i.e. even for currency pairs where the price doesn't move one point at a time.

My guess is the price inputs are wrong..? Try it with pen and paper and see what happens. Then use Alerts to see exactly what variables are going into the formula.

I don't see how you can use the current price and with the OrderOpenPrice...? It' should be the order's open and close prices.. whether real or hypothetical

 

Ok I read the difference between TICK_VALUE & TICK_SIZE, I updated the code but see my problem, try on your account for example:

In this sample, my Ticket is #8787249 for XAUUSD and this is OP_SELL order.

   string objname = "TEST_INFO";
   ObjectDelete(objname);
   ObjectCreate(objname, OBJ_LABEL, 0, 0, 0);
   ObjectSet(objname, OBJPROP_XDISTANCE, 0);
   ObjectSet(objname, OBJPROP_YDISTANCE, 0);
   string txt = "";
   if(OrderSelect(8787249, SELECT_BY_TICKET, MODE_TRADES))
   {
      txt = "P: "+DoubleToStr(OrderProfit(), 3);
      double myP = (MathAbs(OrderOpenPrice()-MarketInfo(Symbol(), MODE_ASK))) * OrderLots () * MarketInfo (Symbol (), MODE_TICKVALUE) / MarketInfo(Symbol(), MODE_TICKSIZE);
      txt = txt +" myP: "+ DoubleToStr(myP, 3);
      txt = txt +" TV: "+DoubleToStr(MarketInfo(Symbol (), MODE_TICKVALUE), 3);
      txt = txt +" TS: "+DoubleToStr(MarketInfo(Symbol (), MODE_TICKSIZE), 3);
      txt = txt +" P:"+DoubleToStr(MarketInfo(Symbol(), MODE_POINT), 3);
   }
   ObjectSetText(objname, txt, 8, "Arial Black", Black);

P: 292.650 = OrderProfit()

myP: 390.900

TickValue: 1.00

TickSize: 0.01

Point: 0.01

Any idea why P != myP ?

 

edit: The most simple should be to find the formula of OrderProfit() ? Is there a source code available somewhere or if anybody may ask to the development team of MT4 :)

 

You are computing change=(orderOpenPrice - ask) Delta=tickValue/tickSize profit(in account currency)=change * delta * lots.

Ask is correct for a sell on symbol(). You could also have used OrderClosePrice.

Print OOP and Ask/OCP values and ask your broker. Sounds like they have misconfigured something.

You've also hard coded symbol() in your calculation. Wrong if OrderSymbol() is different

Reason: