How to Re-Calculate OrderProfit ? - page 2

 
DavidS777:

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.

P: 292.650 = OrderProfit()

myP: 390.900

TickValue: 1.00

TickSize: 0.01

Point: 0.01

Any idea why P != myP ?


It works for me. It's better to use :

MathAbs(OrderClosePrice() - OrderOpenPrice ())...

for an open trade OrderClosePrice() gives you the current price.

As WHRoeder said, there is something weird, as your broker.

 

thanks for info. yes this is a hardcoded test. but this is the same symbol of course.

I'm writing to them

 

@angevoyageur thanks for OrderClosePrice() use I though it was only available when closed (in history pool).

So I've udpated the whole code below. Then asked my broker why there is so much difference, I doubt on a bug from them but well will wait their reply!

Thank you to all of you guys

   string objname = "TEST_PROFIT_FORMULA";
   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))
   {
      double p= MarketInfo (OrderSymbol(), MODE_TICKVALUE) / MarketInfo(OrderSymbol(), MODE_TICKSIZE)*MarketInfo (OrderSymbol(), MODE_POINT);
      txt = "P: "+DoubleToStr(OrderProfit(), 3);

      double myP = MathAbs(OrderOpenPrice()-OrderClosePrice());
      double delta = MarketInfo (OrderSymbol (), MODE_TICKVALUE) / MarketInfo(OrderSymbol(), MODE_TICKSIZE);
      myP = myP*delta*OrderLots();
      
      txt = txt +" myP: "+ DoubleToStr(myP, 3);
      txt = txt +" TV: "+DoubleToStr(MarketInfo(OrderSymbol(), MODE_TICKVALUE), 3);
      txt = txt +" TS: "+DoubleToStr(MarketInfo(OrderSymbol(), MODE_TICKSIZE), 3);
      txt = txt +" P:"+DoubleToStr(MarketInfo(OrderSymbol(), MODE_POINT), 3);

   }
   ObjectSetText(objname, txt, 8, "Arial Black", Black);
 

I just found something interesting !

On MT4 => click right on the Profit column => in the popup => Profit then we can change the display in "as point", "as term currency or "as deposit currency".

Well If I displayed in "as term currency, the value is exacly the same as my formula.

But in OrderProfit() this is the value of "as deposit currency".

I don't understand very well those to display mode. Do you think the difference is the spread currency take by the broker ?

 

Interesting - I thought OrderProfit() was always in account currency.

The spread has nothing to do with your problem.

If I open an account in USD and trade the EURUSD for example. A pip change is always $10.00 USD/lot (exactly.) USD is the account currency (your currency invested)

If I open an account in EUR and trade the EURUSD, a pip change is still always $10.00 That is the "currency to term." But the change to account is $10 USD * 1.334 EUR/USD (exchange rate) or 13.34 EUR.

The value of a pip depends on exchange rates when the quote currency is not the account currency.

 

Got it. Yes exactly I found this also this morning . It dependent of the position currency and your account currency but now I can update the formula by taking care of and give you a nice function in few moments.....

This function will calculate your exact money invested in your account currency + your protected balance.... come back soon ;)

 

Now we are set, this topic is a conclusion (for me) and some people asking me: How much total I'm risking ? What are my potential earnings, etc....

Amount are displayed in your account currency.

(PS: We can also minor the Equity information by removing total swap & commission fees).

Edit: * fixed improved version @ 2013-09-17 14:49 GMT+1 !

//------------------------------------------------------------------------------------------------------------------------------
double getInvestment(int what)
// what = 0 (Protected investment) [When SL is used to get a profit, like Trailing Stop or manually set SL]
// what = 1 (Risked investment)
//------------------------------------------------------------------------------------------------------------------------------
{
   double protected = 0;
   for(int i=0;i<OrdersTotal();i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
            double pip = MathAbs(OrderOpenPrice()-OrderStopLoss());
            double pipC = MathAbs(OrderOpenPrice()-OrderClosePrice());
            double delta = MarketInfo (OrderSymbol (), MODE_TICKVALUE) / MarketInfo(OrderSymbol(), MODE_TICKSIZE);
            double p = pip*delta*OrderLots();
            double pC = pipC*delta*OrderLots();
            if(pC == 0) pC = 1.0;
            double exchange = OrderProfit()/pC;
            p *= exchange;
            p += OrderCommission();
            p += OrderSwap();
            if(what == 0 && OrderProfit() > 0)
               protected += p;
            else if(what == 1 && (( (OrderType() == OP_SELL && OrderStopLoss() > OrderOpenPrice()) ||
                                (OrderType() == OP_BUY && OrderStopLoss() < OrderOpenPrice()) )
                                ||
                                OrderStopLoss() == 0)
                     )
            protected += p;
      }
   }
   return (protected);
}


// Display sample

   int posy = 0, posx = 12;   
   string objname = "INFO_1";
   ObjectDelete(objname);
   ObjectCreate(objname, OBJ_LABEL, 0, 0, 0);
   ObjectSet(objname, OBJPROP_XDISTANCE, posx);
   ObjectSet(objname, OBJPROP_YDISTANCE, posy);
   string txt = "Current Balance: "+DoubleToStr(AccountBalance(), 2)+" "+AccountCurrency();
   ObjectSetText(objname, txt, 8, "Arial Black", Black);
   posy += 12;
   
   objname = "INFO_2";
   ObjectDelete(objname);
   ObjectCreate(objname, OBJ_LABEL, 0, 0, 0);
   ObjectSet(objname, OBJPROP_XDISTANCE, posx);
   ObjectSet(objname, OBJPROP_YDISTANCE, posy);
   txt = "Risk Invested: "+DoubleToStr(getInvestment(1), 2)+" "+AccountCurrency();
   ObjectSetText(objname, txt, 8, "Arial Black", Black);
   posy += 12;

   objname = "INFO_3";
   ObjectDelete(objname);
   ObjectCreate(objname, OBJ_LABEL, 0, 0, 0);
   ObjectSet(objname, OBJPROP_XDISTANCE, posx);
   ObjectSet(objname, OBJPROP_YDISTANCE, posy);
   txt = "Protected Amount: "+DoubleToStr(getInvestment(0), 2)+" "+AccountCurrency()+" | Protected Balance: "+DoubleToStr(AccountBalance()+getInvestment(0), 2)+" "+AccountCurrency();
   ObjectSetText(objname, txt, 8, "Arial Black", Black);
   posy += 12;
   
   objname = "INFO_4";
   ObjectDelete(objname);
   ObjectCreate(objname, OBJ_LABEL, 0, 0, 0);
   ObjectSet(objname, OBJPROP_XDISTANCE, posx);
   ObjectSet(objname, OBJPROP_YDISTANCE, posy);
   txt = "Current Profit: "+DoubleToStr(AccountProfit(), 2)+" "+AccountCurrency()+" | Equity: "+DoubleToStr(AccountEquity(), 2)+" "+AccountCurrency();
   ObjectSetText(objname, txt, 8, "Arial Black", Black);
   posy += 12;
Reason: