Trailing step

 

Hey guys,

I am trying to make a trailing step. Essentially like a trailing stop, but instead of moving the stop loss every point in profit, it only moves the stop loss after X amount in profit. Below is my code (tries to modify as soon as order enters and I get a constant OrderModify error 1):

for(int z=OrdersTotal()-1; z>=0; z--)
  {
   if(!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue;
    if(OrderMagicNumber()==MagicNumber)
     if(OrderSymbol()==Symbol())
      if(OrderType()==OP_BUY)
       {
        for(int tstepb=0; tstepb>=0; tstepb++)
         {
          if(Bid-OrderOpenPrice()>=(WhenToTrail*Point)+(TrailingStep*tstepb*Point))
           if(OrderStopLoss()<Bid-(TrailingStep*Point))
            {
             bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailingStep*Point),OrderTakeProfit(),0,clrBlue);
            }
         }
       }
       
      if(OrderType()==OP_SELL)
       {
        for(int tsteps=0; tsteps>=0; tsteps++)
         {
          if(OrderOpenPrice()-Ask>=(WhenToTrail*Point)+(TrailingStep*tsteps*Point))
           if(OrderStopLoss()>Ask+(TrailingStep*Point) || OrderStopLoss()==0)
            {
             bool sellstep=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailingStep*Point),OrderTakeProfit(),0,clrRed);
            }
         }
       }
  }

 Am I going about this incorrectly..? Is my logic incorrect..?

Any help is appreciated! 

 
        for(int tstepb=0; tstepb>=0; tstepb++) //Why use a loop, especially a never-ending loop
         {
          if(Bid-OrderOpenPrice()>=(WhenToTrail*Point)+(TrailingStep*tstepb*Point))
           if(OrderStopLoss()<Bid-(TrailingStep*Point))
            {
             bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailingStep*Point),OrderTakeProfit(),0,clrBlue);
            }
         }

You are stuck in a never-ending loop and there is no need for the loop at all

 

You could consider something like

   double new_stop_loss;
   if(Bid-OrderOpenPrice()>=WhenToTrail*Point)
     {
      if(OrderOpenPrice()-OrderStopLoss()>Point)
         new_stop_loss=OrderOpenPrice();
      else
         new_stop_loss=Bid-WhenToTrail*Point;
      if(new_stop_loss-OrderStopLoss()>=(TrailingStep*Point))
        {
         bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),new_stop_loss,OrderTakeProfit(),0,clrBlue);
        }
     }

 That is for your buy orders, not compiled or tested.

Don't forget that for sell orders you will need to take into account orders that may not have an initial SL. 

 
GumRai:

You are stuck in a never-ending loop and there is no need for the loop at all

 

You could consider something like

 That is for your buy orders, not compiled or tested.

Don't forget that for sell orders you will need to take into account orders that may not have an initial SL. 

Thanks GumRai, will give this a try and get back to you. :) 
 
GumRai:

You are stuck in a never-ending loop and there is no need for the loop at all

 

You could consider something like

 That is for your buy orders, not compiled or tested.

Don't forget that for sell orders you will need to take into account orders that may not have an initial SL. 

Fantastic works like a charm! Thanks!
 
sorry but what about the sell version 
 
i tried rewriting for a sell trailing stop i wasn't successful  

  double new_stop_loss;
 int WhenToTrail=100;
   int  TrailingStep=20;

   if(Bid-OrderOpenPrice()>=WhenToTrail*Point)
     {
      if(OrderOpenPrice()-OrderStopLoss()>Point)
         new_stop_loss=OrderOpenPrice();
      else
         new_stop_loss=Bid-WhenToTrail*Point;
      if(new_stop_loss-OrderStopLoss()>=(TrailingStep*Point))
        {
         bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),new_stop_loss,OrderTakeProfit(),0,clrBlue);
        }
     }
 
Emmanuel Thompson #: i tried rewriting for a sell trailing stop i wasn't successful  
  1. Please don't add text inside quoted text or CODE blocks, put it outside.
              MQL4 forum editor problem - MQL4 programming forum (2015)

  2.    if(Bid-OrderOpenPrice()>=WhenToTrail*Point)

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 pip spreads) in EURCHF? - General - MQL5 programming forum (2022)

  3. Simplify your code
     int WhenToTrail=100;
       int  TrailingStep=20;
       double newSL=OrderClosePrice() + WhenToTrail*Point;                     // Ask + d
       double currSL=MathMax(OrderOpenPrice(), OrderStopLoss() -TrailingStep); // BE or better.
       if(currSL - newSL > _Point)
         {
             bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),newSL,OrderTakeProfit(),0,clrBlue);
         }
Reason: