Get total pips of an open trade - page 2

 
paul_hughes:

It depends if the close price is stored or calculated on the fly. My guess is it's stored and updated whenever any of the other values are changed on the trade (or it is being closed)

Put this EA on a chart,  then open a Buy for the Symbol the EA is on then look at the Experts tab,  if you are correct OCP won't be updated, if I am correct OCP should be the same a Bid as a Buy is closed at Bid price.

Files:
ocp_test.mq4  3 kb
 
codersguru: Open trades' OrderClosePrice() is 0.

No it is not. OrderCloseTime() is always zero.

It is Bid or Ask depending on order type. It is out of date exactly like Bid and Ask. Call RefreshRates(). Ref

 
RaptorUK:

Put this EA on a chart,  then open a Buy for the Symbol the EA is on then look at the Experts tab,  if you are correct OCP won't be updated, if I am correct OCP should be the same a Bid as a Buy is closed at Bid price.

 


I stand corrected, it does indeed appear to be updating correctly. It was a few months back when I saw this issue and I wondered if it was in the tester where I had noticed it. However, I've just ran the the test EA in the tester and it is working as expected (it always shows an up to date close price using OrderClosePrice()).

Regards, Paul.

 
Alex1004: All I want to do is to get the total profit (or loss) pips on an open trade.
  1. double profitInAccountCurrency = OrderProfit() +OrderSwap() +OrderCommission();

  2. double OOP       = OrderOpenPrice(),
           OCP       = OrderClosePrice(),
           DIR       = Direction( OrderType() ),
           pips      = (OCP-OOP)*DIR / pips2dbl,
           TickValue = DeltaValuePerLot( OrderSymbol() )
           profitInAccountCurrency = (OCP-OOP)*DIR * OrderLots() * TickBValue;
    
    From my code
    double   Direction(int op_xxx){ return( 1. - 2. * (op_xxx%2) );               }
    double   DeltaValuePerLot(string pair=""){
       //{Value in account currency of a Point of Symbol.
       // In tester I had a sale: open=1.35883 close=1.35736 (0.0147)
       // gain$=97.32/6.62 lots/147 points=$0.10/point    or $1.00/pip.
       // IBFX demo/mini       EURUSD TICKVALUE=0.1 MAXLOT=50 LOTSIZE=10,000
       // IBFX demo/standard   EURUSD TICKVALUE=1.0 MAXLOT=50 LOTSIZE=100,000
       //                      $1.00/point    or $10.0/pip.
       //
       // https://www.mql5.com/en/forum/127584 CB: MODE_TICKSIZE will usually return the same
       // value as MODE_POINT (or Point for the current symbol), however, an example
       // of where to use MODE_TICKSIZE would be as part of a ratio with
       // MODE_TICKVALUE when performing money management calculations which need to
       // take account of the pair and the account currency. The reason I use this
       // ratio is that although TV and TS may constantly be returned as something
       // like 7.00 and 0.0001 respectively, I've seen this (intermittently) change
       // to 14.00 and 0.0002 respectively (just example tick values to illustrate).
       // https://www.mql5.com/en/forum/135345 zzuegg reports for non-currency DE30:
       // MarketInfo(chart.symbol,MODE_TICKSIZE) returns 0.5
       // MarketInfo(chart.symbol,MODE_DIGITS)   return 1
       // Point    = 0.1
       // Prices to open must be a multiple of ticksize
       //}
       if(pair == "") pair = Symbol();
       return( MarketInfo(pair, MODE_TICKVALUE)
             / MarketInfo(pair, MODE_TICKSIZE) ); // Not Point.
    }
    

 

 OrderClosePrice() works as intended and always gives the ClosePrice. 

 Thanks for all the comments and discussion, really helped me a lot!

 
codersguru:

Open trades' OrderClosePrice() is 0.
Perhaps you meant OrderCloseTime(),  that is  0  for an open Order.
 

Unfortunately I 've not seen any good answer. Here is mine:

int getProfit() {
        if(OrderType() == OP_BUY)
                return ((Bid - OrderOpenPrice()) / Point);
        else
                return ((OrderOpenPrice() - Ask) / Point);
}

Don't forget to call

OrderSelect

function before a call of

getProfit
 
sijio:

Unfortunately I 've not seen any good answer.

I guess you didn't read all of the thread . . .

RaptorUK:

So . . .

int TypeModifier, Profit.Point ;

OrderSelect( . . . . .);  // this will need to be done in any case

TypeModifier = ((OrderType() * 2) - 1) * -1;  //  gives 1 for a Buy, -1 for a Sell

Profit.Point = ( OrderClosePrice() - OrderOpenPrice() ) * TypeModifier;   // gives +ve for a gain, -ve for a loss
 
RaptorUK:
TypeModifier = ((OrderType() * 2) - 1) * -1;  //  gives 1 for a Buy, -1 for a Sell

double   Direction(int op_xxx){ return( 1. - 2. * (op_xxx%2) );               } // +1 for Buy, BuyLimit, BuyStop