trailing stop movement after risk free

 
Hello friends
My code works like this:
In line 56, when the risk becomes 2 to 1, I want the trailing stop to be activated and the trailing stop to go up with each point that the price goes up.
For this, I used to store the current price in a global variable. But when I opened several positions at the same time, the calculations were messed up.

Please suggest a solution to fix this problem.


#include <Trade\Trade.mqh>
CTrade trade; // Create an instance of the CTrade class

// Define a global array to store prices by ticket number
double priceCurrentByTicket[];



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);

   if(PositionsTotal() == 0)
     {
      trade.Buy(0.1, _Symbol, Ask, Ask - 100 * _Point);
     }
   traling();
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void traling()
  {
   for(int i = 3; i >= 0; i--)
     {
      ulong ticket = PositionGetTicket(i);

      if(PositionSelectByTicket(ticket))
        {
         double open = PositionGetDouble(POSITION_PRICE_OPEN);
         double Sl = PositionGetDouble(POSITION_SL);
         double priceCurrent = PositionGetDouble(POSITION_PRICE_CURRENT);

         // Calculate the distance between open and SL
         double distanceToSL = MathAbs(open - Sl);

         // Calculate 1.5 times the distance to SL
         double onePointFiveSL = 1.5 * distanceToSL;

         // If the current price is greater than open plus 1.5 times the SL distance
         if(priceCurrent > open + onePointFiveSL)
           {

           }

        }
     }
  }
//+------------------------------------------------------------------+
 
Your topic has been moved to the section: Expert Advisors and Automated Trading — Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893