order modify more than one open order

 

Hi I tried to modify all open orders using this code,

double initialPrice; //open price of the first open order
double stepInPoint; //distance pips from initial price (value in price). let's say the distance is 20 points
double StopLossPrice;
double TakeProfitPrice;
double trailStopStep = 40*_Point;
double stoplossStart; //stoploss of the first open order(value in price)
void UpdateAllOpenOrders()
  {
   if(Bid+spread>=initialPrice+stepInPoint) //if price moves up to a certain pips
     {
      for(int i = OrdersTotal() - 1; i >= 0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderType() == OP_BUY) 
              {
               StopLossPrice = NormalizeDouble(stoplossStart+=trailStopStep,Digits); //40 points would be added to the last stoploss and make it as stoploss value.
               TakeProfitPrice = OrderTakeProfit();
               if(OrderStopLoss()<StopLossPrice)
                 {                
                  bool res = OrderModify(OrderTicket(), OrderOpenPrice(), StopLossPrice, TakeProfitPrice, OrderExpiration());
                  Print("Order modify successfully for ticket #", OrderTicket());
                  if(!res)
                     Print("Order failed to update for ticket #", OrderTicket(), " with error: ", GetLastError());
                     
                  initialPrice += stepInPoint; //step distance would be added to initial price so the value would be updated
                 }
              }
           }
        }
     }
   if(Ask-spread<=initialPrice-stepInPoint) //if price moves down to a certain pips
     {
      for(int i = OrdersTotal() - 1; i >= 0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderType() == OP_SELL)
              {
               StopLossPrice = NormalizeDouble(stoplossStart-=trailStopStep,Digits); //40 points would be deduct from the last stoploss and make it as stoploss value.
               TakeProfitPrice = OrderTakeProfit();
               if(OrderStopLoss()<StopLossPrice)
                 {
                  
                  bool res = OrderModify(OrderTicket(), OrderOpenPrice(), StopLossPrice, OrderTakeProfit(), OrderExpiration());
                  Print("Order modify successfully for ticket #", OrderTicket());
                  if(!res)
                     Print("Order failed to update for ticket #", OrderTicket(), " with error: ", GetLastError());
                     
                  initialPrice -= stepInPoint; //step distance would be deducted from initial price so the value would be updated
                 }
              }
           }
        }
     }
  }

the idea of this stoploss is when price moves up or down more than a certain distance I want the stoploss to change to a certain distance. The initialPrice and stoplossStart would be updated as the price moving in a direction of the open orders. With code above only the first open order that is modified and the rest is not. Anyone can help me fix this problem?

 
  1. double initialPrice; //open price of the first open order
    double stepInPoint; //distance pips from initial price (value in price). let's say the distance is 20 points
    double StopLossPrice;
    double TakeProfitPrice;
    double stoplossStart; //stoploss of the first open order(value in price)

    These variables have random values, or you set them with code you do not show.

  2.  NormalizeDouble(stoplossStart+=trailStopStep,Digits); //40 points would be added to the last stoploss and make it as stoploss value.
    

    For every time you call UpdateAllOpenOrders, for every order on the current symbol, you increment stoplossStart. Your variable is meaningless.