{"id":52,"date":"2007-03-24T11:06:34","date_gmt":"2007-03-24T16:06:34","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/2007\/03\/24\/how-to-plot-a-trailing-stop-in-the-price-chart\/"},"modified":"2014-12-04T08:23:03","modified_gmt":"2014-12-04T13:23:03","slug":"how-to-plot-a-trailing-stop-in-the-price-chart","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2007\/03\/24\/how-to-plot-a-trailing-stop-in-the-price-chart\/","title":{"rendered":"How to plot a trailing stop in the Price chart"},"content":{"rendered":"

In this short article we will show how to calculate and plot trailing stop using two different methods. <\/p>

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.<\/p>
StopLevel <\/span>= <\/span>1 <\/span>- <\/span>Param<\/span>(<\/span>"trailing stop %"<\/span>, <\/span>3<\/span>, <\/span>0.1<\/span>, <\/span>10<\/span>, <\/span>0.1<\/span>)\/<\/span>100<\/span>;

<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>Sell <\/span>= <\/span>0<\/span>;
<\/span>trailARRAY <\/span>= <\/span>Null<\/span>;
<\/span>trailstop <\/span>= <\/span>0<\/span>;

for( <\/span>i <\/span>= <\/span>1<\/span>; <\/span>i <\/span>< <\/span>BarCount<\/span>; <\/span>i<\/span>++ )
{

   if( <\/span>trailstop <\/span>== <\/span>0 <\/span>AND <\/span>Buy<\/span>[ <\/span>i <\/span>] ) 
   { 
      <\/span>trailstop <\/span>= <\/span>High<\/span>[ <\/span>i <\/span>] * <\/span>stoplevel<\/span>;
   }
   else <\/span>Buy<\/span>[ <\/span>i <\/span>] = <\/span>0<\/span>; <\/span>\/\/ remove excess buy signals

   <\/span>if( <\/span>trailstop <\/span>> <\/span>0 <\/span>AND <\/span>Low<\/span>[ <\/span>i <\/span>] < <\/span>trailstop <\/span>)
   {
      <\/span>Sell<\/span>[ <\/span>i <\/span>] = <\/span>1<\/span>;
      <\/span>SellPrice<\/span>[ <\/span>i <\/span>] = <\/span>trailstop<\/span>;
      <\/span>trailstop <\/span>= <\/span>0<\/span>;
   }

   if( <\/span>trailstop <\/span>> <\/span>0 <\/span>)
   {
      <\/span>trailstop <\/span>= <\/span>Max<\/span>( <\/span>High<\/span>[ <\/span>i <\/span>] * <\/span>stoplevel<\/span>, <\/span>trailstop <\/span>);
      <\/span>trailARRAY<\/span>[ <\/span>i <\/span>] = <\/span>trailstop<\/span>;
   }

}

<\/span>PlotShapes<\/span>(<\/span>Buy<\/span>*<\/span>shapeUpArrow<\/span>,<\/span>colorGreen<\/span>,<\/span>0<\/span>,<\/span>Low<\/span>);
<\/span>PlotShapes<\/span>(<\/span>Sell<\/span>*<\/span>shapeDownArrow<\/span>,<\/span>colorRed<\/span>,<\/span>0<\/span>,<\/span>High<\/span>);

<\/span>Plot<\/span>( <\/span>Close<\/span>,<\/span>"Price"<\/span>,<\/span>colorBlack<\/span>,<\/span>styleBar<\/span>);
<\/span>Plot<\/span>( <\/span>trailARRAY<\/span>,<\/span>"trailing stop level"<\/span>, <\/span>colorRed <\/span>)<\/code>\u00a0<\/p>

Trailing stop plot<\/a><\/p>

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). <\/p>
StopLevel <\/span>= <\/span>Param<\/span>(<\/span>"trailing stop %"<\/span>, <\/span>3<\/span>, <\/span>0.1<\/span>, <\/span>10<\/span>, <\/span>0.1 <\/span>);

<\/span>SetTradeDelays<\/span>(<\/span>0<\/span>,<\/span>0<\/span>,<\/span>0<\/span>,<\/span>0<\/span>);

<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>Sell <\/span>= <\/span>0<\/span>;
<\/span>ApplyStop<\/span>( <\/span>stopTypeTrailing<\/span>, <\/span>stopModePercent<\/span>, <\/span>StopLevel<\/span>, <\/span>True <\/span>);

<\/span>Equity<\/span>( <\/span>1<\/span>, <\/span>0 <\/span>); <\/span>\/\/ evaluate stops, all quotes

<\/span>InTrade <\/span>= <\/span>Flip<\/span>( <\/span>Buy<\/span>, <\/span>Sell <\/span>);

<\/span>SetOption<\/span>(<\/span>"EveryBarNullCheck"<\/span>, <\/span>True <\/span>);
<\/span>stopline <\/span>= <\/span>IIf<\/span>( <\/span>InTrade<\/span>, <\/span>HighestSince<\/span>( <\/span>Buy<\/span>, <\/span>High <\/span>) * ( <\/span>1 <\/span>- <\/span>0.01 <\/span>* <\/span>StopLevel <\/span>), <\/span>Null <\/span>);

<\/span>PlotShapes<\/span>(<\/span>Buy<\/span>*<\/span>shapeUpArrow<\/span>,<\/span>colorGreen<\/span>,<\/span>0<\/span>,<\/span>Low<\/span>);
<\/span>PlotShapes<\/span>(<\/span>Sell<\/span>*<\/span>shapeDownArrow<\/span>,<\/span>colorRed<\/span>,<\/span>0<\/span>,<\/span>High<\/span>);

<\/span>Plot<\/span>( <\/span>Close<\/span>,<\/span>"Price"<\/span>,<\/span>colorBlack<\/span>,<\/span>styleBar<\/span>);
<\/span>Plot<\/span>( <\/span>stopline<\/span>, <\/span>"trailing stop line"<\/span>, <\/span>colorRed <\/span>)<\/code>","protected":false},"excerpt":{"rendered":"

In this short article we will show how to calculate and plot trailing stop using two different methods.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[53,15,55],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":822,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/52\/revisions\/822"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}