Trailing stop after "x" points price rises


i have gone through KB article but still cannot understand how to apply trailing stoploss

for ex
if buyprice = 100
stoploss should be 6 points behind i.e 94

and after every "x" points rise (just say 3 points) in price...the stoploss should also rises by 3 points.



Hi @mradtke
sorry for disturbing but any way out you have?


@jass17 , if you open the formula code editor and use the "Insert Snippet" functionality, you'll get the following two examples of trailing stops.

The simplest one:

amount = 10; // 10% loss (trailing)
ApplyStop( stopTypeTrailing, stopModePercent, amount, True );

Simply adding the above code to your trading system code, you are applying a 10% trailing stop rule (automatically "trailing" the price as it rises).

The function declaration is:

_ApplyStop( type, mode, amount, exitatstop, volatile = False, ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1 ):_ 

Select the mode 2 to apply an amount expressed in points (stopModePoint) if you want to work with points instead of a percent amount.

If you have not already done it, review also the ApplyStop apply built-in stop documentation page, to better understand all the parameters.

There is also a second sample code using a loop, that shows graphically the trailing stop level:

Buy = 1; // replace with your buy rule
Sell = 0; // replace with your sell rule

StopLevel = 1 - Param("trailing stop %", 3, 0.1, 10, 0.1)/100;

trailARRAY = Null;
trailstop = 0;

for( i = 1; i < BarCount; i++ )

   if( trailstop == 0 AND Buy[ i ] ) 
       trailstop = High[ i ] * stoplevel;
    else Buy[ i ] = 0; // remove excess buy signals

   if( trailstop > 0 AND Low[ i ] < trailstop )
       Sell[ i ] = 1;
       SellPrice[ i ] = trailstop;
       trailstop = 0;

   if( trailstop > 0 )
       trailstop = Max( High[ i ] * stoplevel, trailstop );
       trailARRAY[ i ] = trailstop;



Plot( Close,"Price",colorBlack,styleBar);
Plot( trailARRAY,"trailing stop level", colorRed );

While waiting for more advanced users' answers/solutions, you may start to play/modify these AmiBroker provided code snippets and see if you can use them in your sample system.

Moreover, a search for "trailing stop" here in the forum will provide you additional material to study.


@beppe is correct - it should be easy to implement a normal trailing stop as he has described. But @jass17, are you asking to only increase the stop price when the stock price rises by a predetermined about (like 3 points)? So for example, if you enter at 100, your stop is at 94. If the price rises to 102, you still want your stop at 94. But when the price rises to 103, then you want to move the stop up to 97?


@mradtke yes exactly what i am trying to do here .please help me with that

Earlier i had applied a trailing stoploss by reading from KB like dis

StopLevel = iif( intrade , highestsince(buy,high)*(1- stopline),Null);
*stopline be number of points of stoploss for eg 6 points traling stoploss

but it wasn't good for a healthy trend.


If that's the case, then we would need to fully understand the rules you want to implement, i.e. the behavior you would like to model. For example, if your minimum increment is 3 percent (I don't think you really mean "points"):

  • What happens when the price goes up by 4% from the entry? Does the stop go up by 3% or 4% ?
  • If the price now rises by another 2% (for a total of 6%), is it time to move the stop again, or do you need another 3% move from the previous high?

We would also need to know what kind of system you are implementing, specifically whether or not it is safe to assume that the first entry signal encountered for a symbol will always result in a new trade being opened. If it is likely that you will have more entry signals than you have capacity for new trades, then you would have to implement a system like this inside a custom back test, which I suspect is beyond your comfort level at this point. If you're only testing against a single symbol or a small enough watch list that you will always be able to take all entry signals, then you can implement this with a loop in your phase 1 AFL.


@mradtke yes i am actully try to implement stoploss in points...
My Condition:

  1. if buy happens at 100. the stoplos point should be at 94
  2. Price moves to 106..the trail stoploss should goes to 100
  3. Price further goes to 112 the stoploss should now goes to 106
    and so on

So after "X" points increment only the stoploss changes.
In above case only after "6" points rise not before that

I usualy trade in 5 to 6 symbols at most in a day.And most of time its intraday position..But there are times position gets roll on.

But why does taking position makes any differences? because trailing stoploss will be included in my Buy/Sell code and will only work if there is an open position right?


Previously you said:

That only makes sense if stopline is expressed as a percentage, not as a number of points. For example, if your entry price is $200 and your stop loss is at $198, that's 2 points ($200 - $198) but it's 1% ($2 / $200).

Instead of the code above, you could probably use something like this UNTESTED example. Note that this assumes that you have already removed extra Buy signals using ExRem or some other method. That may NOT be a good assumption. But the point is to illustrate the step-wise stop functionality.

pctStop = 6;
pctStepSize = 6;
entryPrice = ValueWhen(Buy, BuyPrice);
maxPctChange = 100 * (HighestSince(Buy,High) / entryPrice -1);
numSteps = floor(maxPctChange/pctStepSize );
highestStep = entryPrice * (1 + numSteps * pctStepSize/100);
StopLevel = iif( intrade , highestStep *(1- pctStop/100),Null);


@mradtke thank you so much for your time.
the idea is good...there seems certain glitches, (like its exiting on very next candle on open only) but i get to learn many things on what all i can do.
i didnt got much time yet to play with the code ... got busy in things
Will surely acknowledge you when anything new comes up