Question on trailing stops and OrderModify

 

Hi, i have been trying to figure out why my trailing would not work.

When a trade is executed, i would like to change the stoploss accordingly like a trailing stop. I used an OrderModify to modify the stoploss as per seen below but always ended up in an error. After some testing, i suspect that my Stoploss cannot be more than the OrderOpenPrice() and was giving me an error 130. (when i am long)

 I have tried replacing the OrderOpenPrice() with different values like 0, < or > than stoploss but it doesnt seem to work.

 

 Is my suspicion correct that the issue is that i am trying to modify the opening and stoploss prices, such that the stoploss value is higher than the opening price?

Or am i not suppose to use OrderModify   in such situation? possibly an alternative way to change my stoploss?

is there other value i could possibly use to replace  OrderOpenPrice() that would solve this issue?


 Thank you

   if(count_trades == 1)
   {  
      i=OrdersTotal()-1;
      for (i == OrdersTotal()-1;i>=0; i--)       //Cycle for all orders..
      {  
         //---  Selecting of Open Order
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()== 2) 
         {
            //---  If a long position was triggered         
            if(OrderType()==OrderCommandBid)
            {
                  if(BidStopLoss > OrderStopLoss())
                  {
                     if(!OrderModify(OrderTicket(), OrderOpenPrice(),BidStopLoss,OrderTakeProfit(), 0, Pink))
                        {
                        Print("(OrderModify Error)");
                        }
                  }    
            }
 
 
I have tried replacing the OrderOpenPrice() with different values like 0, < or > than stoploss but it doesnt seem to work.

You can't modify OrderOpenPrice.

What is OrderCommandBid's value? 

Where is the value for BidStopLoss calculated? 

 
GumRai:
 

You can't modify OrderOpenPrice.

What is OrderCommandBid's value? 

Where is the value for BidStopLoss calculated? 

Hi, Thank you for your reply,

The OrderCommandBid's value is  OP_BUYLIMIT

The  BidStopLoss=NormalizeDouble(iHigh(NULL,0,iHighest(NULL,0,MODE,PastPeriod,1))-iATR(NULL,0,ATRperiod,0)*ATRMultiplier,4); 

 i am getting the error when i go long. 

The code are attached below:


Is this the issue? A limitation for market order modification is the minimum allowed distance between the stop order and the market price, set by the dealing center (see Order Characteristics andRequirements and Limitations in Making Trades). If the program tries to change the position of a stop order in such a way that it is placed closer to the market than the allowed minimum distance, such trade request will be rejected by the client terminal and the execution of the function OrderModify() will fail (error 130). This is why you should provide a special block in your program, which will consider this limitation.

It has the same error code but my issue is in the orderopen price and stoploss, not current price and stoploss. Am i understanding it wrongly?

//+------------------------------------------------------------------+//| Parameters                                |//+------------------------------------------------------------------+double BidOpen = 0;double BidStopLoss = 0;double BidTakeProfit = 0;double OfferOpen = 0;double OfferStopLoss = 0;double OfferTakeProfit = 0;double ATR=0;int r=0;int i=0;//--- MA modificationextern int MA1period=50;extern int MA1shift=0;extern int MA1method=1;extern int MA1price=0;extern int MA1shift2=0;extern int MA2period=100;extern int MA2shift=0;extern int MA2method=1;extern int MA2price=0;extern int MA2shift2=0;//--- High modificationextern int PastPeriod=50;extern int MODE=MODE_CLOSE;//--- Bid modificationextern int OrderCommandBid=OP_BUYLIMIT;extern int OrderCommandOffer=OP_SELLLIMIT;extern double LotSize = 0.1;//--- StopLoss modificationextern int ATRperiod=20;extern int ATRMultiplier=3;//+------------------------------------------------------------------+//| Expert initialization function                                   |//+------------------------------------------------------------------+int OnInit()  {//---//---   return(INIT_SUCCEEDED);  }//+------------------------------------------------------------------+//| Expert deinitialization function                                 |//+------------------------------------------------------------------+void OnDeinit(const int reason)  {//---  }//+------------------------------------------------------------------+//| Expert tick function                                             |//+------------------------------------------------------------------+void OnTick()  {//--- Calculating entry and exit   BidOpen=NormalizeDouble((Close[0]-Low[0])/2+Low[0],4);   BidStopLoss=NormalizeDouble(iHigh(NULL,0,iHighest(NULL,0,MODE,PastPeriod,1))-iATR(NULL,0,ATRperiod,0)*ATRMultiplier,4);   OfferOpen=NormalizeDouble((Close[0]-High[0])/2+High[0],4);   OfferStopLoss=NormalizeDouble(iLow(NULL,0,iLowest(NULL,0,MODE,PastPeriod,1))+iATR(NULL,0,ATRperiod,0)*ATRMultiplier,4);//--- Check Position   r=OrdersTotal()-1;   int count_trades=0;      for (r == OrdersTotal()-1;r>=0;r--)      {         if(OrderSelect(r,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==2)         {         count_trades++;         }      }//---//---  If No Order/Position   if(count_trades ==0)   {      //--- MA difference      if(iMA(NULL,0,MA1period,MA1shift,MA1method,MA1price,MA1shift2)>iMA(NULL,0,MA2period,MA2shift,MA2method,MA2price,MA2shift2))      {           //--- Close higher than past highest         if(Close[0]>iHigh(NULL,0,iHighest(NULL,0,MODE,PastPeriod,1)))         {           int buy=OrderSend(Symbol(),OrderCommandBid,LotSize,BidOpen,1,BidStopLoss,0,"BUY",2,0,clrGreen);         }      }      if(iMA(NULL,0,MA1period,MA1shift,MA1method,MA1price,MA1shift2)<iMA(NULL,0,MA2period,MA2shift,MA2method,MA2price,MA2shift2))      {           //--- Close higher than past highest         if(Close[0]<iLow(NULL,0,iLowest(NULL,0,MODE,PastPeriod,1)))         {           int sell=OrderSend(Symbol(),OrderCommandOffer,LotSize,OfferOpen,1,OfferStopLoss,0,"SELL",2,0,clrGreen);         }      }   }   if(count_trades == 1)   {      i=OrdersTotal()-1;      for (i == OrdersTotal()-1;i>=0; i--)       //Cycle for all orders..      {         //---  Selecting of Open Order         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()== 2)         {            //---  If a long position was triggered                  if(OrderType()==OrderCommandBid)            {                  if(BidStopLoss > OrderStopLoss())                  {                     if(!OrderModify(OrderTicket(), OrderOpenPrice(),BidStopLoss,OrderTakeProfit(), 0, Pink))                        {                        Print("(OrderModify Error)");                        }                  }              }            //---  If a short position was triggered            if(OrderType()==OrderCommandOffer)            {             if(BidStopLoss > OrderStopLoss())                  {                  Print(BidStopLoss,OrderStopLoss());                  OrderModify(OrderTicket(),OrderOpenPrice(),OfferStopLoss,0,0,clrGreen);                  }            }         }      }   }//+-----------end  }//+------------------------------------------------------------------+

 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  3. Where do you check that (Close[0]-Low[0])/2+Low[0] < Ask - MarketInfo(_Symbol, MODE_STOPLEVEL)*Point) ? Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  4. I never got pending orders to work when I once tried. I thought it was comparing stops to current market rather than pending price. Try opening first and then set stops.
  5. There is no need for pending orders in a EA. Humans need them because they can't watch the screen every second; EAs can. Wait for price to reach trigger and open.
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it.It's use is always wrong
  3. Where do you check that (Close[0]-Low[0])/2+Low[0] < Ask - MarketInfo(_Symbol, MODE_STOPLEVEL)*Point) ? Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
  4. I never got pending orders to work when I once tried. I thought it was comparing stops to current market rather than pending price. Try opening first and then set stops.
  5. There is no need for pending orders in a EA. Humans need them because they can't watch the screen every second; EAs can. Wait for price to reach trigger and open.

Thanks for the reply WHRoeder! I will test and go through them throughly when i get home from my military camp in the weekend. 

But in the mean while, what do you mean by point 3? what do i have to check for? The ticksize or something else?

And thank you for point 4 and 5, i just realised that i dont have to modify my order everytime but to simply close the trade when the conditions are met.
 
zerosen:
But in the mean while, what do you mean by point 3? what do i have to check for? The ticksize or something else?

 just realised that i dont have to modify my order everytime but to simply close the trade when the conditions are met.
  1. I posted exactly what you must check for and a link to why.
  2. You must have a SL in place in case of news, loss of connection, loss of power, etc.
Reason: