how to get current total pips in all open trades?

 
how to get current total pips in all open trades? mql4 and mt4 for a 5 digit broker

let's say 1st order has opened at 1.00005 and now it's on 1.00025, and second order has opened at 1.00015 and now at 1.00035, then I want a code to get the current total pips of both trades which is +40, how to do that?

I'm trying to close all open orders when total pips reaches let's say +30 pips. And alert me when it reaches -30 pips. All I have is the following code to close the orders.


int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                          
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}

 
shermilden5000: let's say 1st order has opened at 1.00005 and now it's on 1.00025, and second order has opened at 1.00015 and now at 1.00035, then I want a code to get the current total pips of both trades which is +40, how to do that?
  1. Please use
SRC
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. The first can't be on 1.00025 and the second on 1.00035. There is only one market price.

  3. Compute Lots weighted average price OrderOpenPrice question . - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. Compute what a pip is. www.mql5.com/en/forum/169828#comment_4075614

  5. Total pips is (market - LWAP) / PIP
 
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it.

  2. The first can't be on 1.00025 and the second on 1.00035. There is only one market price.

  3. Compute Lots weighted average price OrderOpenPrice question . - MQL4 and MetaTrader 4 - MQL4 programming forum

  4. Compute what a pip is. www.mql5.com/en/forum/169828#comment_4075614

  5. Total pips is (market - LWAP) / PIP

1. Thank you for that "SRC" button 

2. of course it can be, we're talking about two different trades ;) not the same one at the exact millisecond :P and market prices change with time ;) 

3. Can you elaborate it a little bit?

4. Can you elaborate it a little bit?

5. Can you elaborate it a little bit?

 

Try following the links @whroeder1 gave.

If your orders are all the same lot size and/or you don't mind about the lot size...

Create a function to count up the pips of all the open orders:

double CountPips()
  {
   double pip_count = 0;
   double pnt2pip   = _Digits%2==0 ? 1 : 10; 
   for(int i=OrdersTotal()-1; i>=0; i--)           // good habit to count down
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;  // select the order
      if(OrderSymbol() != _Symbol)      continue;  // optional check for same symbol
      if(OrderMagicNumber()!= magic_no) continue;  // optional check for magic number
      int order_type = OrderType();
      if(order_type > 1)                continue;  // 0 == OP_BUY and 1 == OP_SELL
      double pip = SymbolInfoDouble(OrderSymbol(),SYMBOL_POINT) * pnt2pip;
      if(order_type==OP_BUY) pip_count+=(OrderClosePrice()-OrderOpenPrice())/pip;
      else                   pip_count+=(OrderOpenPrice()-OrderClosePrice())/pip; 
     }
   return(pip_count);
  }

Then run your check:

   double pips = CountPips();
   if(pips>=30) CloseAllOrders();
   else if(pips<=-30) SendAlert();

All untested.

Reason: