March 24, 2007
How to plot a trailing stop in the Price chart
In this short article we will show how to calculate and plot trailing stop using two different methods.
First method uses looping and it does not use ApplyStop() function as it does not plot stops – it only triggers them in backtest mode. The stop % level can be adjusted via PARAMETERS dalog.
StopLevel = 1 - Param("trailing stop %", 3, 0.1, 10, 0.1)/100;
Buy = Cross( MACD(), Signal() );
Sell = 0;
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;
}
}
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
Plot( Close,"Price",colorBlack,styleBar);
Plot( trailARRAY,"trailing stop level", colorRed );
Alternatively you can use code without looping, but then it requires Equity(1) to evaluate stops as shown in the example code below. Equity( 1 ) is the backtester-in-a-box that runs actual single-security backtest and when parameter 1 is passed it writes back signals (removing excessive ones and writing out all stops to Buy/Sell/Short/Cover arrays).
StopLevel = Param("trailing stop %", 3, 0.1, 10, 0.1 );
SetTradeDelays(0,0,0,0);
Buy = Cross( MACD(), Signal() );
Sell = 0;
ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );
Equity( 1, 0 ); // evaluate stops, all quotes
InTrade = Flip( Buy, Sell );
SetOption("EveryBarNullCheck", True );
stopline = IIf( InTrade, HighestSince( Buy, High ) * ( 1 - 0.01 * StopLevel ), Null );
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
Plot( Close,"Price",colorBlack,styleBar);
Plot( stopline, "trailing stop line", colorRed );
Filed by support at 11:06 am under Indicators, Systems
8 Comments


Thanks for the looping example. Examples that address issues via looping and also array approach are appreciated.
I must agree. I once spent months getting frustrated w/ ApplyStop before moving on to learn loop code. Seeing identical functionality presented both ways is fascinating and instructive. Nicely done Tomasz.
I pasted these two types of AFL code into 2 different formulas, and quickly find that these two approaches generate different trade results in Backtest. Any ideas?
Copy-paste artists usually forget about using SAME settings. Most probably you are using non-zero trading delays while the example assumes using zero delay.
Does applystop also work to cover a short trade?
I can get the both methods to work as a buy/sell combo, but a simple replacement of “Buy” with “Short” and “Sell” with “Cover” doesn’t seem to work. (also swapped the stop line around to be LowestSince, rather than HighestSince).
Code I’m trying is as follows
StopLevel = Param(”trailing stop %”, 3, 0.1, 10, 0.1 );
SetTradeDelays(0,0,0,0);
Short = Cross( MACD(), Signal() );
Cover= 0;
ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );
Equity( 1); // evaluate stops, all quotes
InTrade = Flip( Short, Cover );
SetOption(”EveryBarNullCheck”, True );
stopline = IIf( InTrade, LowestSince( Short, Low ) * ( 1 – 0.01 * StopLevel ), Null );
PlotShapes(Short*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Cover*shapeDownArrow,colorRed,0,High);
Plot( Cover,”Price”,colorBlack,styleBar);
Plot( stopline, “trailing stop line”, colorRed );
Yes it works with short trades as well. Remember to enable SHORT trades in the settings!
Can you please put an example using ApplyStop(stopTypeTrailing, stopModePoint…)? I.e. show how to calculate “stopline” so that the plot match the array used by ApplyStop() internally when in stopModePoint mode? Thanks.
For “point” mode replace
StopLevel = 1 – Param(”trailing stop %”, 3, 0.1, 10, 0.1)/100;
with
StopLevel = Param(”trailing stop pt”, 3, 0.1, 10, 0.1);
and every occurrence of:
High[ i ] * stoplevel
with
High[ i ] – stoplevel