Help with Take Profit Function

 

Good afternoon, community members.

I am developing an EA that exclusively trades the EURUSD pair. The strategy involves opening a new position to average the price whenever the price moves against the current position. This part is working perfectly.

However, I am facing difficulties with the function that sets the take profits at a single level. To illustrate better, let me describe a scenario:

Suppose I have open buy positions at the following prices:

  • 1.08500
  • 1.08000
  • 1.07500

These positions have the following lot sizes:

  • 0.01
  • 0.01
  • 0.02

The function I am developing (below) should adjust the take profit of these positions so that, when the target is reached, the profit is X times greater than the sum of the losses of the negative positions. The way the function is currently, it returns the error [invalid stops].

Unfortunately, my function is not achieving this goal. Therefore, I am reaching out to you for help in adjusting the logic to ensure this goal is met.


Thank you all in advance for your assistance.

Regards!


//---
void CalculateTakeProfit(long type, double multipler)
  {
//---
   int total                 = PositionsTotal();
//---
   double  buyTarget         = 0,
           desiredProfit     = 0,
           totalLoss         = 0,
           totalLots         = 0,
           totalSwapCost     = 0,
           weightedPriceSum  = 0;
//---
   int     buys              = CountPositions(BUY);

//---
   for(int i = total - 1; i >= 0; i--)
     {
      //---
      if(!PositionSelectByTicket(PositionGetTicket(i)))
         continue;

      //---
      if(PositionGetInteger(POSITION_MAGIC) != MagicNumber || (_Symbol != "" && PositionGetSymbol(i) != _Symbol))
         continue;

      //---
      if(type == BUY && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
        {
         weightedPriceSum += PositionGetDouble(POSITION_PRICE_OPEN) * PositionGetDouble(POSITION_VOLUME);
         totalLots        += PositionGetDouble(POSITION_VOLUME);
         totalLoss        += (Ask - PositionGetDouble(POSITION_PRICE_OPEN)) * PositionGetDouble(POSITION_VOLUME);
        }
     }

//---
   if(buys > 0 && weightedPriceSum != 0)
     {
      AveragePrice  = weightedPriceSum / totalLots;
      desiredProfit = totalLoss * multipler;
      buyTarget     = AveragePrice + (desiredProfit / totalLots);
     }

//---
   for(int i = total - 1; i >= 0; i--)
     {
      ulong ticket = PositionGetTicket(i);

      if(!PositionSelectByTicket(ticket))
         continue;

      if(PositionGetInteger(POSITION_MAGIC) != MagicNumber || (_Symbol != "" && PositionGetSymbol(i) != _Symbol))
         continue;

      //--- TP
      if(type == BUY && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && buyTarget > 0 && buys > 1 && NormalizeDoubles(buyTarget) != PositionGetDouble(POSITION_TP))
        {
         if(PositionModify(ticket, NormalizeDoubles(PositionGetDouble(POSITION_SL)), NormalizeDoubles(buyTarget))) {}
        }
     }
  }