ATR Trail Stop Indicator


Trading View has an indicator called ATR Trail Stop, how can I add it to Amibroker?

This indicator indicates the trend, if it is bullish it is of one color and is below the price, if it is bearish it is of another color and is above the price, as in the attached image.

Thank you very much,

AmiBroker knowledge base and AmiBroker help are first resources to look at for any newbie.

if you want different color for line then in first code modify to

Plot( trailARRAY,"trailing stop level", iif(C >trailARRAY,colorGreen, colorRed ));

And if using second code then modify

Plot( stopline, "trailing stop line", iif(C >stopline,colorGreen,colorRed) );

Of course you have to change to ATR points since KB examples show percent trail stop.

If using ApplyStop

myATR = ATR( Param( "ATR period", 10, 1, 30, 1 ) );
stoplevel = Param( "ATR multiplier", 3, 1, 10, 0.1 ) * myATR;

Sell = Cover =  0; 
ApplyStop( stopTypeTrailing, stopModePoint, stopLevel, 1);

And stopline

stopline = IIf( InTrade, HighestSince( Buy, High ) - ValueWhen( Buy, stoplevel ), Null );

Now just add Short direction.

No image attached.


ATR trail stop
Sorry, I forgot to atach

Thank you very much fxshrat, you are very nice!

How can I add other line when the price is below, like the image attached?


SetOption("ReverseSignalForcesExit", False);
SetOption("ActivateStopsImmediately", 0);

period = 20;

myATR = ATR( period );
stoplevel = 3 * myATR;

BuyPrice = ShortPrice = Close;

Buy = C > ( LLV( L, period ) + stoplevel);
Short = C < ( HHV( H, period ) - stoplevel);

Sell = Cover = 0; 
ApplyStop( stopTypeTrailing, stopModePoint, stopLevel, 0, 1 );

eq = Equity( 1, 0 ); // evaluate stops, all quotes

InLongTrade = Flip( Buy, Sell );
InShortTrade = Flip( Short, Cover );

SetOption("EveryBarNullCheck", True );
stoplineL = IIf( InLongTrade, HighestSince( Buy, High ) - ValueWhen( Buy, stoplevel ), Null);
stoplineS = IIf( InShortTrade, LowestSince( Short, Low ) + ValueWhen( Short, stoplevel ), Null );



Plot( Close,"Price",colorDefault,styleBar);
Plot( stoplineL, "trailing stop line", colorGreen, styleStaircase );
Plot( stoplineS, "trailing stop line", colorRed, styleStaircase );



Than you, no draw the shortline (above the price)


1 Like

You have to enable Long and Short in Analysis settings - General - Positions and restart AB!

Otherwise, please do not post pictures of code. Pointless.

it already appears.

Sorry I didn't know, I'm a newbie!

Thank you fxshrat

1 Like

I'm not seeing my plot requests
When I backtest, I can right-click on a trade, 'Show Arrows for Actual Trades', and see Buy/Sell arrows
But I don't see any of the plots below — not even the simple (Low*0.95)
I know this is terribly basic but, what chart(s) do these plots go to?

FastMA = EMA( C, 21 );
SlowMA = MA( C, 50 );

Buy  = Cross( FastMA, SlowMA ); 
Sell = Cross( SlowMA, FastMA );

Plot(Low*0.95, "95%Low", colorRed, styleStaircase);

The code you posted above also has to be in the chart's code for it to display there, because when you right-click on a trade, nothing is sent to the chart except the buy/sell arrows.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.