Trailing Stop Loss for each Ticket

 

I have the below trailing stop loss coded, but it seems to be affecting all the open orders the same way, making all of them converge to the parameters of only one (the most recent) and it always prevents older orders from reaching take profit.

How can I best change this code so that Position Modify affects ticket by ticket?

 void Trailing()
 {
 
 double bid= 0,ask=0,point=0;
 int digits=0;
 for(int i=0;i<OrdersTotal();i++)
{
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
       {
               bid= SymbolInfoDouble(OrderSymbol(),SYMBOL_BID);
               ask= SymbolInfoDouble(OrderSymbol(),SYMBOL_ASK);
               point= SymbolInfoDouble(OrderSymbol(),SYMBOL_POINT);
               digits = SymbolInfoInteger(OrderSymbol(),SYMBOL_DIGITS);
               
        if((OrderSymbol() == _Symbol) && OrderMagicNumber() == Magic_Number) {
            if(OrderType()==OP_BUY)
            {
               
               double profit_points=bid-OrderOpenPrice();
               
               if((NormalizeDouble(profit_points,digits)/point)>=(start_points*10) /*&& OrderStopLoss()!=OrderOpenPrice()*/)
               {
                  //Print("Profit-points "+profit_points);
                  double sl_updated_point=0;
                  if(OrderStopLoss() < OrderOpenPrice())
                  {
                     sl_updated_point = (first_move*10);
                  }
                  if((NormalizeDouble(profit_points,digits)/point)>(start_points*10))
                  {
                     sl_updated_point=(NormalizeDouble(profit_points,digits)/point)-(start_points*10);
                     sl_updated_point = int(sl_updated_point / step_points);
                     sl_updated_point = sl_updated_point * step_move;
                     sl_updated_point = sl_updated_point + (first_move*10);
                  }
                  double s=NormalizeDouble(OrderOpenPrice()+(sl_updated_point)*point,digits);
                  //Print("OrderStoploss "+OrderStopLoss());
                  if(OrderStopLoss()<s)
                  {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),s,OrderTakeProfit(),0,clrNONE))
                     {
                        Print("Order Modify on ticket #",OrderTicket()," Failed. ERROR CODE",GetLastError());  
                     }
                     else
                     {
                        Print("Stop loss modified");
                        }
                  }   
               }
            }
            if(OrderType()==OP_SELL)
            {
              double profit_points=OrderOpenPrice()-ask;

               if((NormalizeDouble(profit_points,digits)/point)>=(start_points*10) /*&& OrderStopLoss()!=OrderOpenPrice()*/)
               {
                  //Print("Profit-points "+profit_points);
                  double sl_updated_point=0;
                  if(OrderStopLoss() > OrderOpenPrice())
                  {
                     sl_updated_point = (first_move*10);
                  }
                  if((NormalizeDouble(profit_points,digits)/point)>(start_points*10))
                  {
                     sl_updated_point=(NormalizeDouble(profit_points,digits)/point)-(start_points*10);
                     sl_updated_point = int(sl_updated_point / step_points);
                     sl_updated_point = sl_updated_point * step_move;
                     sl_updated_point = sl_updated_point + (first_move*10);
                  }
                  double s=NormalizeDouble(OrderOpenPrice()-sl_updated_point*point,digits);
                   //Print("OrderStoploss "+OrderStopLoss());
                  if(OrderStopLoss()>s || OrderStopLoss()==0)
                  {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),s,OrderTakeProfit(),0,clrNONE))
                        Print("Order Modify on ticket #",OrderTicket()," Failed. ERROR CODE",GetLastError());  
                     else
                     {
                        Print("Stop loss modified");  
                     }
                   } 
               }
            } 
         }
        }
 }
 }
 
Please don't create topics randomly in any section. It has been moved to the section: MQL4 e MetaTrader 4