MQL4 Modify Stop-Loss only works on one pair out of six.

 

I have an EA that is supposed to add a stop loss to breakeven (without previously having a stop-loss at all) once price gets to a certain distance between the take-profit and the entry. It comments where its supposed to change the stop-loss, but just changes it for one pair everyday. Here's my code:

openTrade is a boolean I use to identify if there are open orders on the current pair. It sets to true once the method to open trades runs, and to false when it closes the trades.

void checkTrade()
{   
   if((Hour() >= 10 && Hour() < 23) && openTrade)
   {
      double tp;
      double entry;
      double changeSL;
      double currentSL;
      string direction;
      
      for (int i = OrdersTotal(); i >= 0; i--)
      {
         if (OrderSelect(i, SELECT_BY_POS) == true)
         {
            if (OrderSymbol() == Symbol())
            {
               tp = OrderTakeProfit();
               entry = OrderOpenPrice();
               currentSL = OrderStopLoss();
               double distance = tp - entry;
               
               if(distance < 0)
               {
                  distance = distance * -1;
                  distance = distance/4;
                  changeSL = entry - distance;
                  direction = "Short";
               } 
               else
               {
                  distance = distance/4; 
                  changeSL = entry + distance;
                  direction = "Long";
               }            
            }
         }
      }
      
      tp = NormalizeDouble(tp, 5);
      entry = NormalizeDouble(entry, 5);
      changeSL = NormalizeDouble(changeSL, 5);
      currentSL = NormalizeDouble(currentSL, 5);
      
      if(direction == "Long" && currentSL != entry)
      {
         if(MarketInfo(Symbol(), MODE_ASK) >= changeSL)
         { 
            if(OrderSelect(0, SELECT_BY_POS,MODE_TRADES))
                  if(OrderSymbol() == Symbol())
                  {
                     currentSL = entry;
                     OrderModify(OrderTicket(), entry, entry, tp, 0, CLR_NONE);
                  }
         } 
      }
      
      if(direction == "Short" && currentSL != entry)
      {
         if(MarketInfo(Symbol(), MODE_ASK) <= changeSL)
         {
            if(OrderSelect(0, SELECT_BY_POS,MODE_TRADES))
                  if(OrderSymbol() == Symbol())
                  {
                     currentSL = entry;
                     OrderModify(OrderTicket(), entry, entry, tp, 0, CLR_NONE);
                  }
         }
      }
   }
   Comment("Current SL: " + currentSL + 
           "\nEntry: " + entry,
           "\nOpen Trade: " + openTrade + 
           "\nTP: " + tp + 
           "\nChange SL: " + changeSL);
}
 
jota9913: once price gets to a certain distance between the take-profit and the entry.
               tp = OrderTakeProfit();
               entry = OrderOpenPrice();
               currentSL = OrderStopLoss();
               double distance = tp - entry;
Your code doesn't modify the order selected. It keeps going and would attempt to modify any random order in position index zero. Move your modify code inside the loop.