ModifyOrder Problem...

 

Hey! Im having problems with the ModifyOrder();

I have my strategy that sends Shorts and Longs of 2 kinds each, and when its come to a certain point, I want to modify the SL to the Open price. I think the problems comes when I call the trade open in the next way. 

(OrderSelect(orderID1,SELECT_BY_TICKET) == true)

The EA is taking the parameters of the last trade instead of the current one (I checked it calling the SL and Open trade price) and I don’t t know why, and obviously gave me error and is not modifying the order.

Here is my code... 

      if (CheckOpenOrders == false)
      {      
                                                          //Short

         if (PriceBid <= limitBreakDown)
         {
            if (Case 1)
            {  

                   orderID1 = OrderSend(NULL, 1, loteSize, entryPrice, 10, stopLoss, takeProfit,"Short open, mFrac simple");
                   Alert("Order ID1: " + orderID1);
                   
              
                  if (orderID1 < 0)
                  {
                      Alert("Fail to open trade, Error: " + GetLastError());                     
                  } else 
                  {   
                      Alert("Orden opened successfully ");                     
                  }
               
               
            } else if (Case 2)
            { 
                   orderID2 = OrderSend(NULL, 1, loteSize, entryPrice, 10, stopLoss, takeProfit,"Short open, mFrac complex");
                   Alert("Order ID2: " + orderID2);
                   
                  if (orderID1 < 0)
                  {
                      Alert("Fail to open trade, Error: " + GetLastError());                     
                  } else 
                  {   
                      Alert("Orden opened successfully ");                     
                  }

            } 
         } else
         
                                 //Long
         

         if (PriceAsk >= limitBreakUp) 
         { 
            if (Case 1) 
            {  

                   orderID1 = OrderSend(NULL, 0, loteSize, entryPrice, 10, stopLoss, takeProfit,"Long open, mFrac simple");
                   Alert("Order ID1: " + orderID1);
                   
                  
                  if (orderID1 < 0)
                  {
                      Alert("Fail to open trade, Error: " + GetLastError());                     
                  } else 
                  {   
                      Alert("Orden opened successfully ");                     
                  }
      
               
            } else if (Case 2)
            {  
                    
                   orderID2 = OrderSend(NULL, 0, loteSize, entryPrice, 10, stopLoss, takeProfit, "Long open, mFrac complex");
                   Alert("Order ID2: " + orderID2);

               
                  if (orderID1 < 0)
                  {
                      Alert("Fail to open trade, Error: " + GetLastError());                     
                  } else 
                  {   
                      Alert("Orden opened successfully ");                     
                  }
             } 
         }
     } 
       else if (OrderSelect(orderID1,SELECT_BY_TICKET) == true)     //Trade already opened.         Case 1
       {
       int orderType = OrderType();// Short = 1, Long = 0
            
            if(orderType == 0)//long position
            {  
               if(PriceAsk == HalfWayTradeUpSimple)
               {      
                  optimalStopLoss = NormalizeDouble((OrderOpenPrice() + (GetPipValue() * 2)), GetDigits());
                  
                  double SLdistance = MathAbs(optimalStopLoss - OrderStopLoss());

                  if(SLdistance > GetPipValue())
                  {
                  bool Ans = OrderModify(OrderTicket(),OrderOpenPrice(),optimalStopLoss,OrderTakeProfit(),0,Blue);
                  
                  if(!Ans){
                     Alert("Cant modify order, Error: ",GetLastError());
                     Alert("Order Entry Price: " + OrderOpenPrice());
                     Alert("Stop Loss: " + OrderStopLoss());
                     }
                  else {
                        Alert("Order modified successfully: " + orderID1);
                     }
                   }
               }
            } else if (orderType == 1)//if short
            {  
               if(PriceBid == HalfWayTradeDownSimple)
               {
                  
                  optimalStopLoss = NormalizeDouble((OrderOpenPrice() - (GetPipValue() * 2)),GetDigits());
                  
                  double SLdistance = MathAbs(OrderStopLoss() - optimalStopLoss);

                  if(SLdistance > GetPipValue())
                  {
                  bool Ans = OrderModify(OrderTicket(),OrderOpenPrice(),optimalStopLoss,OrderTakeProfit(),0,Blue);
                  
                  if(!Ans){
                     Alert("Cant modify order, Error: ",GetLastError());
                     Alert("Order Entry Price: " + OrderOpenPrice());
                     Alert("Stop Loss: " + OrderStopLoss());
                     }
                  else {
                        Alert("Order modified successfully: " + orderID1);
                     }
                  }
                }
            }  
            

            } else if (OrderSelect(orderID2,SELECT_BY_TICKET) == true)           //Case 2
           {
           
            int orderType = OrderType();// Short = 1, Long = 0
            
            double optimalStopLoss;
            
            if(orderType == 0)//long position
            {
               if(PriceAsk == HalfWayTradeUpComplex)
               {
                  optimalStopLoss = NormalizeDouble((OrderOpenPrice() + (GetPipValue() * 2)),GetDigits());
                  
                  double SLdistance = MathAbs(optimalStopLoss - OrderStopLoss());

                  if(SLdistance > GetPipValue())
                  {
                  bool Ans = OrderModify(OrderTicket(),OrderOpenPrice(),optimalStopLoss,OrderTakeProfit(),0,Blue);
                  
                  if(!Ans){
                     Alert("Cant modify order, Error: ",GetLastError());
                     Alert("Order Entry Price: " + OrderOpenPrice());
                     Alert("Stop Loss: " + OrderStopLoss());
                     }
                  else {
                        Alert("Order modified successfully: " + orderID1);
                     }
                  }
                }
               
            } else if (orderType == 1)//if short
            {
               if(PriceBid == HalfWayTradeDownComplex)
               {
                  optimalStopLoss = NormalizeDouble((OrderOpenPrice() - (GetPipValue() * 2)),GetDigits());
                  
                  double SLdistance = MathAbs(OrderStopLoss() - optimalStopLoss);

                  if(SLdistance > GetPipValue())
                  {
               
                  bool Ans = OrderModify(OrderTicket(),OrderOpenPrice(),optimalStopLoss,OrderTakeProfit(),0,Blue);
                  
                  if(!Ans){
                     Alert("Cant modify order, Error: ",GetLastError());
                     Alert("Order Entry Price: " + OrderOpenPrice());
                     Alert("Stop Loss: " + OrderStopLoss());
                     }
                  else {
                        Alert("Order modified successfully: " + orderID1);
                     }
                  }
                } 
            } 
       };
 
  1. (OrderSelect(orderID1,SELECT_BY_TICKET) == true)

    If you select by ticket, you must verify that the order is still open and not still pending.

  2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

  3. orderID2 = OrderSend(NULL, 0, loteSize, entryPrice, 10, stopLoss, takeProfit, "Long open, mFrac complex");

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

 
William Roeder #:
  1. If you select by ticket, you must verify that the order is still open and not still pending.

  2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

    Use a OrderSelect / Position select loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

  3. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

Hey William! after few days, I could fix it! Also took ur advice about hardcoding numbers.

So I wanna to tell u thanks! 
Reason: