title: How to modify a StopLoss of an active trade?
body: <p>I have trouble with modifying the stoploss of a running trade using MQL5. Selecting the order works out for me. But if I try to access the variables ( for instance <strong><code>OrderTicket()</code></strong> &amp; <strong><code>OrderOpenPrice()</code></strong> ), it always returns 0.00000:</p>
  
  <pre><code>2017.06.01 00:06:32.114 2016.04.08 00:00:00   failed modify  buy 0.00  sl: 0.00000, tp: 0.00000 -&gt; sl: 1.41594, tp: 0.00000 [Invalid request]
  </code></pre>
  
  <p>Here's my stoploss modyfing void:</p>
  
  <pre><code>void modifyStops() {
  
     int total = OrdersTotal();          // total number of placed pending orders
     Print( total + " Orders on the line!!!" );
  
     //--- Over all placed pending orders
     for ( int i = 0; i &lt;  total; i++ )
     {     bool isOrderSelected = OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
           if (  isOrderSelected )
           {  // TODO: Check the Trades to contain the correct Order Number
  
                 Print( "Symbol &amp; Magicnumber matching" );
                 double newStopLoss;
  
              // Update the stop loss
                 if (  OrderType() == OP_BUY )
                 {
                       newStopLoss = addTolerance( SL );
                 } 
                 else if (  OrderType() == OP_SELL )
                 {
                            newStopLoss = minusTolerance( SL );
                 }       
                 newStopLoss = NormalizeDouble( newStopLoss, Digits );
                 Print( "NEW STOP LOSS::::=====&gt; ", Symbol(), " at ", newStopLoss );
  
                 if ( !OrderModify( OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, Green ) )
                 {
                       Print( "OrderModify returned the error of ", GetLastError() );
                 }  
       }
  }
  </code></pre>
  
  <hr>
  
  <p>The CTrade class isn't working properly for me. I tried to implement the code you've posted - however: It still doesn't seem to work out.</p>
  
  <p>Unfortunately I implemented that in my EA and the OrderGetTicket(i) returns zero when the trade is live. So my void looks like this:</p>
  
  <pre><code>void modifyStops() {
  for(int i=0; i&lt;PositionsTotal();i++) 
      {
      ulong ticket;
      if((ticket=PositionGetTicket(i))&gt;0) 
          { 
           //--- return order properties 
           double open_price    =PositionGetDouble(POSITION_PRICE_OPEN); 
           datetime time_open     =(datetime)PositionGetInteger(POSITION_TIME); 
           string symbol        =PositionGetString(POSITION_SYMBOL); 
           int order_magic   =PositionGetInteger(POSITION_MAGIC); 
           double volume        =PositionGetDouble(POSITION_VOLUME); 
           double stoploss      =PositionGetDouble(POSITION_SL); 
           double takeprofit    =PositionGetDouble(POSITION_TP); 
           ENUM_ORDER_TYPE type          =EnumToString(ENUM_ORDER_TYPE(PositionGetInteger(POSITION_TYPE))); 
           //--- prepare and show information about the order 
           printf("#ticket %d %s %G %s at %G, with sl: %G tp: %G was set up at %s", 
                  ticket,                 // order ticket 
                  type,                   // type 
                  volume,                 // placed volume 
                  symbol,                 // symbol 
                  open_price,             // specified open price
                  stoploss,               //
                  takeprofit,             // 
                  TimeToString(time_open) // time of order placing 
                  ); 
          } 
      }
  }
  </code></pre>
  
  <p>And the printf function returns nothing:</p>
  
  <pre><code>2017.06.02 01:42:26.910 2016.04.07 00:00:00   #ticket 1 (non-string passed) 0  at 0, with sl: 0 tp: 0 was set up at 1970.01.01 00:00
  </code></pre>
  
  <p>I can't believe it's that hard to simply modify an SL in MQL5. That's horribly. However I need to get through it to test my strategy over several pairs... </p>
  
  <p>Do you have another idea? I set up trades with the following code:</p>
  
  <pre><code>void createPendingOrder(ENUM_ORDER_TYPE orderType, double lots, double stopLoss) {
     MqlTradeRequest request={0};
     MqlTradeResult  result={0};
  
  //--- parameters to place a pending order
     request.action   =TRADE_ACTION_PENDING;                             // type of trade operation
     request.symbol   =Symbol();                                         // symbol
     request.volume   =lots;                                             // volume of 0.1 lot
     //request.deviation=2;                                              // allowed deviation from the price
     request.magic    =224466      ;                                     // MagicNumber of the order
     //int offset = 3;                                                   // offset from the current price to place the order, in points
     double price;                                                       // order triggering price
     double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);                // value of point
     int digits=SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);                // number of decimal places (precision)
     //--- checking the type of operation
     if(orderType==ORDER_TYPE_BUY_STOP)
       {
        request.type         =ORDER_TYPE_BUY_STOP;                            // order type
        price                =entryPrice;   
        request.price        =NormalizeDouble(price,digits);                      // normalized opening price 
        request.sl           =stopLoss;
       }
     else if(orderType==ORDER_TYPE_SELL_STOP)
       {
        request.type         =ORDER_TYPE_SELL_STOP;                           // order type
        price                =entryPrice;           
        request.price        =NormalizeDouble(price,digits);                  // normalized opening price 
        request.sl           =stopLoss;
       }
     else Alert("This example is only for placing pending orders");   // if not pending order is selected
  //--- send the request
     if(!OrderSend(request,result))
        PrintFormat("OrderSend error %d",GetLastError());                 // if unable to send the request, output the error code
  //--- information about the operation
     PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }
  </code></pre>
  
  <p>Would it for instance be possible to save the result object in a Array and then access the running trade through that object?</p>