Adding up pips on open trades

 

I need to loop through the open orders and add up the pips to get a total. I have discovered 2 problems with my current code below, 1 - I failed to consider that the EA may be on a symbol that is 4 digit or 2 digit and it may be on a broker with the extra digit precision; 2 - it adds the values of various symbols that are of different digit values.

The simple calculation is below, how can I modify this to accurately count the total of pips earned/lost on each open trade? In the code snippet, I am using Digits, but that will normalize the digits according to this symbols digits, depending on the symbol the EA is on. When adding the pips of the following as an example is the problem:

Extra digit:

Sell 132.150 131.352
Buy 131.332 131.319
Sell 108.648 108.126
Sell 108.863 108.125
Sell 1.80530 1.80598
Sell 1.59680 1.59740

Not Extra Digit:

Sell 132.15 131.35
Buy 131.33 131.31
Sell 108.64 108.12
Sell 108.86 108.12
Sell 1.8053 1.8059
Sell 1.5968 1.5974

In the code, we determine sell or buy and subtract accordingly, but then we simply add the value of a 2 digit to a 4 digit (or in the case of extra digit, 3 digit to a 5 digit), which in the end is not going to be a correct value. I think I need to get the subtraction, then convert the pip value into a whole number and if there is a extra digit situation we just drop the first digit and then add them up. How would I do this?

   double ChartPipTotal = 0.0;
   double TicketPips = 0.0;

   
   for(int i = 0; i <= OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber() == MagicNumber) {
         if(OrderType() == OP_BUY)
         {
            TicketPips = NormalizeDouble(OrderClosePrice() - OrderOpenPrice(), Digits);
         }
         else
         {
            TicketPips = NormalizeDouble(OrderOpenPrice() - OrderClosePrice(), Digits);
         }

         ChartPipTotal += TicketPips;
      }
   }
 

Here is a tool I built on it to help see the values. the only problem is that I am having a hard time getting the text to show dynamically. I tried to append the test together and then spit it out, but not working too well.

//---- input parameters
extern int       GlobalCashCollectInPips=100;
extern int       MagicNumber = 143;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   ObjectCreate("Obj_GCCText", OBJ_LABEL, 0, 0, 0);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("Obj_GCCText");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      GlobalCashCollector();   
//----
   return(0);
  }
//+------------------------------------------------------------------+

void GlobalCashCollector()
{
   
   double ChartPipTotal = 0.0;
   double TicketPips = 0.0;
   int PairDigits = 0;
   int Multiplier = 0;
   string strGCC = "";
   
   for(int i = 0; i <= OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber() == MagicNumber) {
         
         if(OrderType() == OP_BUY)
         {
            PairDigits = MarketInfo(OrderSymbol(), MODE_DIGITS);
            if (PairDigits == 2)
            {
               Multiplier = 100;
            }
            else if (PairDigits == 3)
            {
               Multiplier = 1000;
            }
            else if (PairDigits == 4)
            {
               Multiplier = 10000;
            }
            else if (PairDigits == 5)
            {
               Multiplier = 100000;
            }
            TicketPips = NormalizeDouble(OrderClosePrice() - OrderOpenPrice(), PairDigits) * Multiplier;
            
         }
         else
         {
            PairDigits = MarketInfo(OrderSymbol(), MODE_DIGITS);
            if (PairDigits == 2)
            {
               Multiplier = 100;
            }
            else if (PairDigits == 3)
            {
               Multiplier = 1000;
            }
            else if (PairDigits == 4)
            {
               Multiplier = 10000;
            }
            else if (PairDigits == 5)
            {
               Multiplier = 100000;
            }
            TicketPips = NormalizeDouble(OrderOpenPrice() - OrderClosePrice(), PairDigits) * Multiplier;
         }

         ChartPipTotal += TicketPips;
         strGCC = StringConcatenate(strGCC, "TicketPips: ", TicketPips, ", Symbol: ", OrderSymbol(), "\n");
      }
   }
   
   strGCC = StringConcatenate(strGCC, "ChartPipTotal: ", ChartPipTotal, " GlobalCashCollectInPips: ", GlobalCashCollectInPips);
   ObjectSet("Obj_GCCText", OBJPROP_CORNER, 0);
   ObjectSet("Obj_GCCText", OBJPROP_XDISTANCE, 200);
   ObjectSet("Obj_GCCText", OBJPROP_YDISTANCE, 0);
   ObjectSetText("Obj_GCCText", strGCC, 14, "Arial", Lime);

   if (ChartPipTotal > GlobalCashCollectInPips)
   {
      strGCC = StringConcatenate("WINNER: ChartPipTotal: ", ChartPipTotal, " GlobalCashCollectInPips: ", GlobalCashCollectInPips);
      ObjectSet("Obj_GCCText", OBJPROP_CORNER, 0);
      ObjectSet("Obj_GCCText", OBJPROP_XDISTANCE, 200);
      ObjectSet("Obj_GCCText", OBJPROP_YDISTANCE, 0);
      ObjectSetText("Obj_GCCText", strGCC, 14, "Arial", Lime);
   
   }
   
}
Attached as a file
 

would this work ?

   for(int i = 0; i <= OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber() == MagicNumber) {
         
         if(OrderType() == OP_BUY)
         {
         profit = OrderProfit();
         pips = NormalizeDouble(profit/lots/MarketInfo(OrderSymbol(),MODE_TICKVALUE),0);
 

I used your code to see what would happen and all of the 3 digit values had a leading 1 this is on a 5 digit broker. I think you may have something to help, but I think there is still a wrinkle or two here.

Example:

I put the code into the program I attached earlier. remarking out all of the if conditions for digit settings. Then compiled and put it on a 5 digit Symbol and a 3 digit Symbol in a 5 digit broker. The output is as follows:

Note: the values may be slightly off as I just grabbed a couple of outputs may not be directly related and this is acceptable. like the difference between the two AUDJPY outputs.

AUDJPY_fx-1628
EURNZD_fx-887
GBPJPY_fx-2208
GBPNZD_fx-1568
NZDJPY_fx-2435
NZDJPY_fx-1821
ChartPipTotal: -10547 GlobalCashCollectInPips: 100

AUDJPY_fx-1631
EURNZD_fx-887
GBPJPY_fx-2208
GBPNZD_fx-1578
NZDJPY_fx-2435
NZDJPY_fx-1821
ChartPipTotal: -10560 GlobalCashCollectInPips: 100

In this example, the true value of the pips is actually around 628 or 631. Now if my assumption is right, because this on a 5 digit broker, this is actually 62 or 63 pips. So your calculation is adding a 1 at the beginning and the 5 digit broker is adding the last digit which I still need to adjust the calculation for.

 

if thats the case i need to fix this in mine too i havent tested it on a 3 or 5 digit account, what does MarketInfo(Symbol(),MODE_TICKVALUE); return on your 3 and 5 digit pairs ?

 

pips should be (OrderOpenPrice() - OrderClosePrice())/Point

 
DxdCn:

pips should be (OrderOpenPrice() - OrderClosePrice())/Point


i thought it was neccessary to use tickvalue in the calculation because on another thread about something similar they said MODE_TICKVALUE = 1 pip and the value of a pip is different for USDJPY for instance if I do MarketInfo(Symbol(),MODE_TICKVALUE); in a loop on USDJPY right now it returns

11.3934 11.3921 11.3973 11.3960 changing almost every tick

while on EURUSD it is static at 10.0

Edit: Its because I was doing the calculation from OrderProfit() where pips have already been converted to base currency to convert them back to pips I used TICK_VALUE your way of using openprice-closeprice is probably better except in my case I didnt have a close price because I needed to calculate it on open orders

 
  1. while on EURUSD it is static at 10.0

    It is only static if your account currency is USD


  2. DxdCn:

    pips should be (OrderOpenPrice() - OrderClosePrice())/Point

    On a 5 digit broker a Point is not a pip.
  3. As for adding up pips of different pairs, what is the sum of 5 oranges and 3 apples. It's not 8 somethings. It's meaningless.

    int    points = ( OrderClosePrice()-OrderOpenPrice() )/Point;
    double profit = points * MarketInfo(Symbol(),MODE_TICKVALUE);
    Adding up profit's has meaning.
 

I'll give it a try

Wait a minute, if I wanted to deal with the trades based on profit then all I have to do is just use OrderProfit() to total it up. Nope it needs to be in pips.

Reason: