wrong sign in the difference between 2 double numbers

 
void setPipsGained(){
   
      double difference;
      int pipsGained;
         
      for(int i=PositionsTotal()-1;i>=0;i--){ // returns the number of current position
         if(positionInfo.SelectByIndex(i)){     // selects the position by index for further access to its properties
            if(positionInfo.Symbol()==Symbol()){                         
               if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY){               
                  difference = orderPriceClose - orderPriceOpen;
                  pipsGained = round(difference * MathPow(10, Digits()) );
                  printf("orderPriceOpen=" + orderPriceOpen + 
                     " orderPriceClose=" + orderPriceClose + 
                     " difference=" + NormalizeDouble(difference, Digits()) +   
                     " pipsGained=" + pipsGained );
                  break;
               }else if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL){               
                  difference = orderPriceOpen - orderPriceClose;
                  pipsGained = round(difference * MathPow(10, Digits()) );
                  printf("orderPriceOpen=" + orderPriceOpen + 
                        " orderPriceClose=" + orderPriceClose +
                        " difference=" + NormalizeDouble(difference, Digits()) );
                  break;
               }
            }
         }
      }
      
      realPipsGained = pipsGained;
      
   }

results:

orderPriceOpen=1.13271 orderPriceClose=1.1328 difference=9.000000000000001e-05 pipsGained=9

as you can see, the sign of the operation result is wrong. pipsGained should be -9.

this happens only for

difference = orderPriceOpen - orderPriceClose;

what's wrong?

Thanks

 
You select a position, then access the order.

The order that does the closing of a long position is a sell order.

The closing of a short position is a buy order.

As far as I know, you need to select the order as well, not only the position.

Since I do not use the standard lib, I don't know if positionInfo does that for you.
 
if(positioninfo.PositionType() == POSITION_TYPE_BUY)
 
Andrea Di # :
that part of the code works, the difference I pointed out to you is that the result is incorrect

Position! = Pending order

Read the comments above. You are looping through POSITIONS, but at the same time referring to the PENDED ORDER properties!

 
the solution works, thanks guys
 
               if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY){               
                  difference = orderPriceClose - orderPriceOpen;
Does your code actually compile? Where do you set your variables?
 
William Roeder #:
Does your code actually compile? Where do you set your variables?

yes it compile well.

I setted the variabls with other functions.

Ernst Van Der Merwe solved my problem