Get total pips of an open trade

 

Hi forum, 

 

 I guess my problem could be solved pretty easy but I just can't find a way or function..

All I want to do is to get the total profit (or loss) pips on an open trade. 

 

Thanks in advance 

 
Alex1004:

Hi forum, 

 

 I guess my problem could be solved pretty easy but I just can't find a way or function..

All I want to do is to get the total profit (or loss) pips on an open trade. 

Compare  OrderOpenPrice() and OrderClosePrice() and take into account OrderType() 
 
RaptorUK:
Compare  OrderOpenPrice() and OrderClosePrice() and take into account OrderType() 


Open trades' OrderClosePrice() is 0.
 

This function returns the profit of a selected trade:

double Profit(int type, double open, string symbol="", double lot=0.1, int option=2)
{
   if(symbol=="") symbol=Symbol();
   
   double tick = MarketInfo(symbol,MODE_TICKVALUE);
   if(MarketInfo(symbol,MODE_DIGITS)==2 || MarketInfo(symbol,MODE_DIGITS)==4) tick=tick*0.1;

   double point = GetPoint(symbol); 
   
   if(option==1) //Currency
   {
      if(type==OP_BUY) return(NormalizeDouble((((MarketInfo(symbol,MODE_BID) - open)/point)*tick*(lot/0.1)),2)); 
      if(type ==OP_SELL) return(NormalizeDouble((((open - MarketInfo(symbol,MODE_ASK))/point)*tick*(lot/0.1)),2));
   }
   
   if(option==2) //Points
   {
      if(type==OP_BUY) return(NormalizeDouble((((MarketInfo(symbol,MODE_BID) - open)/point)),2)); 
      if(type ==OP_SELL) return(NormalizeDouble((((open - MarketInfo(symbol,MODE_ASK))/point)),2));
   }
}
 
codersguru:

Open trades' OrderClosePrice() is 0.
Nope,  it isn't.
 
RaptorUK:
Nope,  it isn't.


I'd have to agree. AFAIK, the OrderClosePrice() is updated whenever the order is updated (in any way) so OrderClosePrice() would reflect the close price at the time of last order modification.

(love MT4 ;-)

Regards, Paul.

 
paul_hughes:


I'd have to agree. AFAIK, the OrderClosePrice() is updated whenever the order is updated (in any way) so OrderClosePrice() would reflect the close price at the time of last order modification.

(love MT4 ;-)

As far as I am aware OrderClosePrice() is the price at which a market order should be closed at if it is open or the price an order was closed at if it is closed.  Using it means that the same line of code can be used to close an Order regardless of type.
 
RaptorUK:
Compare  OrderOpenPrice() and OrderClosePrice() and take into account OrderType() 

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:
As far as I am aware OrderClosePrice() is the price at which a market order should be closed at if it is open or the price an order was closed at if it is closed.  Using it means that the same line of code can be used to close an Order regardless of type.


My findings are (up to maybe a couple of MT4 versions back) that if an order is closed, OrderClosePrice() is fine, but for an open trade, it reflects the close price at time of last modification. If you have not modifed the trade for a while OrderClosePrice() will become out of date. However, if it is regularly updated (e.g. it is being trailed) then you may not notice any difference. 

I first noticed this whilst searching for a bug in a bit of code which checked/updated trade status (using OrderClosePrice() on open trades) but when I switched to using Bid/Ask instead, it worked fine...

I agree, it would be useful if it worked out the latest value when called but I don't think it does so and is not reliable.

Regards, Paul.

 
paul_hughes:


My findings are (up to maybe a couple of MT4 versions back) that if an order is closed, OrderClosePrice() is fine, but for an open trade, it reflects the close price at time of last modification. If you have not modifed the trade for a while OrderClosePrice() will become out of date. 

It's a call to a function,  I'm not sure how it could be out of date.
 
RaptorUK:
It's a call to a function,  I'm not sure how it could be out of date.


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)
Reason: