[trailing stop] Problem by trailing stop

 

Hi,

In my EA I use the trailing stop as follow:

...
extern double SL_prozent = 1.0;

double dStopLoss, dUpdatedPrice;

...
void OnTick(){
   // some businessLogic here

   if(OrdersTotal() > 0){
      updateTrailingStop();
   }   

...

   bEntryLong = ...
   bEntryShort = ...

   int iTotal=OrdersTotal();    
   // Handle Long Signal
   if(bEntryLong){
      if(iTotal>0)
         closeAllOpenedShortOrders();
      else{         
         int iLongTicket=OrderSend(Symbol(),OP_BUY,Lots,MarketInfo(Symbol(),MODE_ASK),Slippage,0,0,"T05_MURI",MAGIC_NUMBER,0,clrGreen);
         if(iLongTicket>0)
           {
            Print("Current Time: ", TimeCurrent()," Ask: ",Ask," Bid: ",Bid," Spread: ",(Ask-Bid));           
            if(OrderSelect(iLongTicket,SELECT_BY_TICKET,MODE_TRADES)){
               dUpdatedPrice = OrderOpenPrice();
               iOrderTicket = iLongTicket;
               Print("LONG order opened : ",OrderOpenPrice());
               if(OrderCloseTime() == 0 && OrderStopLoss() == 0)
               {
                  //double StoppLoss = NormalizeDouble(OrderOpenPrice() / ( 1 + SL_prozent/100), Digits);
                  dStopLoss = NormalizePrice(OrderOpenPrice() / ( 1 + SL_prozent/100));
                  bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),dStopLoss,OrderTakeProfit(),0,Yellow);
                  if(!OrderAngepasst)
                     Print("Error by OrderModify : ",GetLastError());
                  else
                     Print("Order modified successfully.");            
               }
            }   
            else
               Print("Error by OrderSelect : ",GetLastError());
           }
         else
            Print("Error opening LONG order : ",GetLastError());
      }      
   }

   // Handle Short Signal
   if(bEntryShort){
      if(iTotal>0)
         closeAllOpenedLongOrders();
      else{         
         int iShortTicket=OrderSend(Symbol(),OP_SELL,Lots,MarketInfo(Symbol(),MODE_BID),Slippage,0,0,"T05_MURI",MAGIC_NUMBER,0,clrYellow);
         if(iShortTicket>0)
           {
            Print("Current Time: ", TimeCurrent()," Ask: ",Ask," Bid: ",Bid," Spread: ",(Ask-Bid));           
            if(OrderSelect(iShortTicket,SELECT_BY_TICKET,MODE_TRADES)){
               dUpdatedPrice = OrderOpenPrice();
               iOrderTicket = iShortTicket;
               Print("SHORT order opened : ",OrderOpenPrice());
               if(OrderCloseTime() == 0 && OrderStopLoss() == 0)
               {
                  //double StoppLoss = NormalizeDouble(OrderOpenPrice() * ( 1 + SL_prozent/100), Digits);
                  dStopLoss = NormalizePrice(OrderOpenPrice() * ( 1 + SL_prozent/100));
                  bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),dStopLoss,OrderTakeProfit(),0,Yellow);
                  if(!OrderAngepasst)
                     Print("Error by OrderModify : ",GetLastError());
                  else
                     Print("Order modified successfully.");            
               }
            }               
            else
               Print("Error by OrderSelect : ",GetLastError());
           }
         else
            Print("Error opening SHORT order : ",GetLastError());
      }         
   }
}
...

bool updateTrailingStop(){
   Print("Update Trailing Stop.");
   
   int iOrder_type = 0; 
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i, SELECT_BY_POS))
         if(OrderMagicNumber() == MAGIC_NUMBER && OrderSymbol() == Symbol()) { 
            iOrder_type=OrderType(); 
         } 
         else{
            Print("MagicNumber or OrderSybol is wrong.");
            return false;
         }
      else 
         Print("OrderSelect() returned error - ",GetLastError());
         
      switch(iOrder_type)
        {
         case OP_BUY:
            dStopLoss = NormalizePrice(dUpdatedPrice / ( 1 + SL_prozent/100));
            Print("Bid: ", Bid," OrderOpenPrice: ",OrderOpenPrice()," dStopLoss: ",dStopLoss, " OrderStopLoss: ",OrderStopLoss());           
            if(Bid/OrderOpenPrice()> ( 1 + SL_prozent/100)){
               bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),dStopLoss,OrderTakeProfit(),0,Yellow);
               if(!OrderAngepasst)
                  Print("Error by OrderModify : ",GetLastError());
               else
                  Print("Trailing-Stop Order modified successfully.");            
            }                           
            else
              {
               Print("No need to modify trailing stop 1.");
              }            
            break;
         case OP_SELL:
            dStopLoss = NormalizePrice(dUpdatedPrice * ( 1 + SL_prozent/100));
            Print("Ask: ", Ask," OrderOpenPrice: ",OrderOpenPrice()," dStopLoss: ",dStopLoss, " OrderStopLoss: ",OrderStopLoss());           
            if(Ask/OrderOpenPrice() > (1 + SL_prozent/100)){
               bool OrderAngepasst = OrderModify(OrderTicket(),OrderOpenPrice(),dStopLoss,OrderTakeProfit(),0,Yellow);
               if(!OrderAngepasst)
                  Print("Error by OrderModify : ",GetLastError());
               else
                  Print("Trailing-Stop Order modified successfully.");            
            }                 
            else
              {
               Print("No need to modify trailing stop 1.");
              }            
            break;
        }      
     }
          
   return true;
}


But as I run the EA I got error message: CFDDAX,M5: Error by OrderModify : 1

 
  1.    for(int i=0;i<OrdersTotal();i++)
         {
          if(OrderSelect(i, SELECT_BY_POS))
             if(OrderMagicNumber() == MAGIC_NUMBER && OrderSymbol() == Symbol()) { 
                iOrder_type=OrderType(); 
             } 
             else{
                Print("MagicNumber or OrderSybol is wrong.");
                return false;
             }
    This code means the EA in incompatible with with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.

  3. thomas2004: Error by OrderModify : 1
    ERR_NO_RESULT
    You Server
    Change the SL to XIt is at X!
    Change the SL to X It is at X!
    Change the SL to XYou are insane
 

1 & 2.

Assumed I just use one EA for one chart and no any manual set order. In other words, there is only one order, long or short. Other case will not be consider now.

I do use the RefreshRates(). But I haven't posted in my code.


3.

"Change the SL to X"?  I don't understand what your table tells. 

 
thomas2004:
 

"Change the SL to X"?  I don't understand what your table tells. 

whroeder1 likes his templates that he spreads on this forum. He wanted to say by his table: If unchanged values are passed as the OrderModify parameters, the error 1 (ERR_NO_RESULT) will be generated.

Reason: