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 Param("trailing stop %"30.1100.1)/100;

Buy CrossMACD(), Signal() );
Sell 0;
trailARRAY Null;
trailstop 0;

for( 1BarCounti++ )
{

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

   if( trailstop AND Low] < trailstop )
   {
      
Sell] = 1;
      
SellPrice] = trailstop;
      
trailstop 0;
   }

   if( trailstop )
   {   
      
trailstop MaxHigh] * stopleveltrailstop );
      
trailARRAY] = trailstop;
   }

}

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

PlotClose,"Price",colorBlack,styleBar);
PlottrailARRAY,"trailing stop level"colorRed );


 

Trailing stop plot

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 %"30.1100.1 );

SetTradeDelays(0,0,0,0);

Buy CrossMACD(), Signal() );
Sell 0;
ApplyStopstopTypeTrailingstopModePercentStopLevelTrue );
 
Equity1); // evaluate stops, all quotes

InTrade FlipBuySell );

SetOption("EveryBarNullCheck"True );
stopline IIfInTradeHighestSinceBuyHigh ) * ( 0.01 StopLevel ), Null );

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

PlotClose,"Price",colorBlack,styleBar);
Plotstopline"trailing stop line"colorRed );

8 Responses to “How to plot a trailing stop in the Price chart”

  1. Bert Steele
    June 17th, 2007 | 3:48 pm

    Thanks for the looping example. Examples that address issues via looping and also array approach are appreciated.

  2. Grant Noble
    January 9th, 2008 | 2:14 am

    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.

  3. Janhaus
    January 9th, 2008 | 8:21 pm

    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?

  4. January 10th, 2008 | 5:38 am

    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.

  5. Mikey
    March 23rd, 2008 | 7:48 am

    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 );

  6. March 23rd, 2008 | 8:43 am

    Yes it works with short trades as well. Remember to enable SHORT trades in the settings!

  7. Flávio
    July 29th, 2008 | 11:20 pm

    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.

  8. July 30th, 2008 | 6:30 am

    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

Leave a reply