How To Modify A Pending Order

 

In the following code why is it I cant get it to place stop loss and take profit onto the pending order after it has been placed?

int gBuyTicket;

void OnTick()
{
   //-------------- 
   // Timer
   //--------------
   int hour = Hour();
   bool timer = (hour >= 0 && hour <= 23);

   // Buy Order       
   if(timer && gBuyTicket == 0)
   {
      // Buy Objects  
      double pendingBuyPriceAsk = extremhigh+(Bid - Ask);  
      double pendingBuyPriceBid = extremhigh;
      
      RefreshRates();      
      double stopLevel = MarketInfo(_Symbol,MODE_STOPLEVEL) * _Point;      
      double upperStopLevel = Ask + stopLevel;       
          
      double stopLoss = 0.0;
      if(StopLoss > 0) stopLoss = pendingBuyPriceBid - (StopLoss * _Point);         
      
      // Buy Order                                                                                    
      if(upperStopLevel < stopLoss) gBuyTicket = OrderSend(_Symbol,OP_BUYSTOP,lotSize,pendingBuyPriceAsk,Slippage,0,0,"BuyOrder",MagicNumber,0,clrBlue);        
      buySwitch = true;                                                    
   }

   // Add stop loss & take profit to Buy order  
   if(OrderType() == OP_BUYSTOP && (StopLoss > 0 || TakeProfit > 0))
   {
      bool select = OrderSelect(gBuyTicket,SELECT_BY_TICKET); 

      double pendingBuyPriceBid = OrderOpenPrice();
            
      // Calculate stop loss & take profit
      double stopLoss = 0.0;
      double takeProfit = 0.0; 
      if(StopLoss > 0) stopLoss = pendingBuyPriceBid - (StopLoss * _Point);         
      if(TakeProfit > 0) takeProfit = pendingBuyPriceBid + (TakeProfit * _Point);
              
      // Modify order
      bool modify = OrderModify(gBuyTicket,0,stopLoss,takeProfit,0);

  }

      // Closed Outcome 2 - Cancel order if spread is above max spread
      if(hour == 23 && OrdersTotal() > 0) 
      {
         for(int order = 0; order <= OrdersTotal() - 1; order++)
         {
            bool select = OrderSelect(order,SELECT_BY_POS);
            
            // Check buy order for stop placement
            if(OrderType() == OP_BUYSTOP && OrderMagicNumber() == MagicNumber && select)
            { 
               bool buyclose = OrderDelete(gBuyTicket,clrBlue); 
               if(buyclose) order--;
               gBuyTicket = 0;
            }
         }
      }

}

 
Stephen Reynolds: In the following code why is it I cant get it to place stop loss and take profit onto the pending order after it has been placed?
bool modify = OrderModify(gBuyTicket,0,stopLoss,takeProfit,0);
  1. Because zero is not a valid (pending) price.
  2. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Reason: