Modify Pending Orders

 

Hi All,

I am trying to figure out an EA which updates its pending orders if current price pass the SL level of pending order both for buy and sell orders. As you can see below pic current price is below the SL point of pending orders. Therefore I want to modify new updated pending orders with openning price of from those SL point (+30 & -30 point) and not only for window currency but also for other currencies which are in the pending order list.

I tried below code but it didn't work out. Could you help me?

//------------------------------------------------------------------------------- 1 --
int start()                                    
  {
   string Symb=Symbol();                        // Symbol
//------------------------------------------------------------------------------- 2 --
   for(int i=1; i<=OrdersTotal(); i++)          
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) 
        {                                   
         //---------------------------------------------------------------------- 3 --
         if (OrderSymbol()== Symb) continue;    
         if (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) continue;           // Market order  
         //---------------------------------------------------------------------- 4 --
           {
            int    Tip   =OrderType();          
            int    Ticket=OrderTicket();        
            double Price =OrderOpenPrice();    
            double SL    =OrderStopLoss();     
            double TP    =OrderTakeProfit();    
           }                                   
        }                                       
     }                                          
//------------------------------------------------------------------------------- 5 --
   if (Tip==0)                                  
     {
      Alert("For ",Symb," no pending orders available");
      return;                                   
     }
//------------------------------------------------------------------------------- 6 --
   while(true)                                 
     {
      RefreshRates();                           // Update data
      //------------------------------------------------------------------------- 7 --
      double c_bid =MarketInfo(Symb,MODE_BID); // Request for the value of Bid
      double c_ask =MarketInfo(Symb,MODE_ASK); // Request for the value of Ask                         

      //------------------------------------------------------------------------- 8 --
      string Text="";                           // Not to be modified
      double New_SL=0;
      double New_TP=0;
      switch(Tip)                               // By order type
        {
         case 4:                                // BuyStopt
            if (NormalizeDouble(SL,Digits) > // If it is further than by
                NormalizeDouble(c_ask,Digits))//..the preset value
              {
               double New_Price=SL+30*Point;          // Its new price
               if (NormalizeDouble(SL,Digits)>0)
                  New_SL=New_Price-(Price-SL);  // New StopLoss
               if (NormalizeDouble(TP,Digits)>0)
                  New_TP=New_Price+(TP-Price);  // New TakeProfit
               Text= "BuyStopt ";               // Modify it.
              }
            break;                              // Exit 'switch'
         case 5:                                // SellStop
            if (NormalizeDouble(SL,Digits) < // If it is further than by
                NormalizeDouble(c_bid,Digits))//..the preset value
              {
               New_Price=SL-30*Point;          // Its new price
               if (NormalizeDouble(SL,Digits)>0)
                  New_SL=New_Price+(SL-Price);  // New StopLoss
               if (NormalizeDouble(TP,Digits)>0)
                  New_TP=New_Price-(Price-TP);  // New TakeProfit
               Text= "SellStop ";               // Modify it.
              }
        }
      if (NormalizeDouble(New_SL,Digits)<0)     // Checking SL
         New_SL=0;
      if (NormalizeDouble(New_TP,Digits)<0)     // Checking TP
         New_TP=0;
      if (Text=="")                             // If it is not modified
        {
         Alert("No conditions for modification.");
         break;                                 // Exit 'while'
        }
      //------------------------------------------------------------------------ 10 --
      Alert ("Modification ",Text,Ticket,". Awaiting response..");
      bool Ans=OrderModify(Ticket,New_Price,New_SL,New_TP,0);//Modify it!
      //------------------------------------------------------------------------ 11 --
      if (Ans==true)                            // Got it! :)
        {
         Alert ("Modified order ",Text," ",Ticket," :)");
         break;                                 // Exit the closing cycle
        }
      //------------------------------------------------------------------------ 12 --
      int Error=GetLastError();                 // Failed :(
      switch(Error)                             // Overcomable errors
        {
         case  4: Alert("Trade server is busy. Retrying..");
            Sleep(3000);                        // Simple solution
            continue;                           // At the next iteration
         case 137:Alert("Broker is busy. Retrying..");
            Sleep(3000);                        // Simple solution
            continue;                           // At the next iteration
         case 146:Alert("Trading subsystem is busy. Retrying..");
            Sleep(500);                         // Simple solution
            continue;                           // At the next iteration
        }
      switch(Error)                             // Critical errors
        {
         case 2 : Alert("Common error.");
            break;                              // Exit 'switch'
         case 64: Alert("Account is blocked.");
            break;                              // Exit 'switch'
         case 133:Alert("Trading is prohibited");
            break;                              // Exit 'switch'
         case 139:Alert("Order is blocked and is being processed");
            break;                              // Exit 'switch'
         case 145:Alert("Modification prohibited. ",
                              "Order is too close to the market");
            break;                              // Exit 'switch'
         default: Alert("Occurred error ",Error);//Other alternatives   
        }
      break;                                    // Exit the closing cycle
     }                                          // End of closing cycle   
//------------------------------------------------------------------------------ 13 --
   Alert ("The script has completed its operations -----------------------");
   return;                                      // Exit start()
  }
//------------------------------------------------------------------------------ 14 --