# Trailing step

330

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(OrderMagicNumber()==MagicNumber)
if(OrderSymbol()==Symbol())
{
for(int tstepb=0; tstepb>=0; tstepb++)
{
if(Bid-OrderOpenPrice()>=(WhenToTrail*Point)+(TrailingStep*tstepb*Point))
if(OrderStopLoss()<Bid-(TrailingStep*Point))
{
}
}
}

if(OrderType()==OP_SELL)
{
for(int tsteps=0; tsteps>=0; tsteps++)
{
{
}
}
}
}```

Any help is appreciated!

Moderator
10614

```        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))
{
}
}```

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))
{
}
}```

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

330

GumRai:

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

You could consider something like

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. :)
330

GumRai:

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

You could consider something like