EA does not calculate accurately

 

Hello Great People, I am trying to make an EA recalculate Take Profit once a new Position is Open using a formulae and also modify the TP of all open trades to the new TP. 

I want to identify and select the open trades in the order they were opened. i.e. Select 1st Buy Position first then Select the 2nd Buy Position and so on.

With my little mql5 experience, I have not been able to make the formulae work nor modify all open trades to the new TP. Below is my code. Kindly help look into this.

Thank you.

double TradeP2 ()
 
 {
   double A = 0;
   double B = 0;
   double C = 0;
   double TP2 = 0;
  
  { if (PositionSelect (_Symbol)==true)
 
 {
   
   if (PositionGetString (POSITION_COMMENT) == "1stTrade") A = PositionGetDouble(POSITION_PRICE_OPEN);
   if (PositionGetString (POSITION_COMMENT) == "2ndTrade") B = PositionGetDouble(POSITION_PRICE_OPEN);
   if (PositionGetString (POSITION_COMMENT) == "3rdTrade") C = PositionGetDouble(POSITION_PRICE_OPEN); 
   
   TP2 = A/4 + B/4 + C*2/4 + 0.00050;
   
     }
   
   return(TP2);
   }