PINE SCRIPT CODE UPDATED ATR TRAILING STOP-LOSS (LONG/SHORT)
Several years ago I came across a website as I was searching for coding a trailing stop-loss on MetaStock. A website called Trader Nexus helped me to better understand what a Chandelier Exit is with several great examples and some sort of a plug in DLL specifically designed for MetaStock formula language. Over the years I made some modifications to the ATR based trailing stop-loss.
Recently I spent couple of long hours on Trading View to understand and formulate the similar ATR based trailing stop by using their formula language, pine script. I think I was able to plot the indicator and I’m sharing it here with out members. I plan to integrate more and more Trading View functionalities to Tech Charts reports and website.
If you are interested in giving the platform a try here is your discounted link (Try Trading View with Black Friday discount of 80%)
I named it ATR Trailing Stop (Long/Short Selectable) because so much more can be done with it by adding different calculations, such as price projections based on the initial risk etc. I will continue to build on this in the following posts. Let’s look at the below box for parameters.
The ATR Trailing Stop (Long/Short Selectable) will start calculating on a set date that you specify. This is great because you want to trail the price from the breakout day or even after exceeding specific price level (can be your breakeven level or even to capture more of the upside after the price target is met).
Trade Direction: This is a new addition to the script. Depending on your long or short chart pattern setup you can select the trade direction from the drop down menu.
Entry price: If you act at the close of the day, you can leave this value as 0 and it will take the close of the day for the initial protective stop-loss calculation. You can choose to add a value such as the pattern boundary and in that case it will subtract the initial protective stop-loss from the pattern boundary and not the close of the day. If you use a scaling in tactic during the day (buying in tranches intraday as the breakout takes place) and your average purchase price is different than the close of the day, you can also plug that number in to calculate the initial protective stop-loss.
ATR period: You can select the ATR period. It can be 10 day, 14 day or 30 day or any ATR period of your choice.
ATR Multiplier for Stop-loss: This is the multiplier that you want to trail the price with. From the highest level price reached it will trail the price with a 3 x ATR () distance. The higher the number, the wider the trailing stop-loss. (Short video on ATR based trailing stop-loss). A multiplier of 1 will trail the price so close that and adverse movement can result in triggering the stop-loss.
Custom Value for First day Trailing Stop: This is my favorite part. For aggressive risk management, your initial protective stop can be smaller than what the ATR Trailing Stop will use in its calculation after entry day. In this case you can take 1xATR () or even with FX and Futures you can apply 0.5xATR() as the first day to calculate initial protective stop. The protective stop turns into a trailing stop after the first day.

The chart below is a recent breakdown I have discussed under the Special Cryptocurrency report. The broadening pattern completed with a breakdown below the lower boundary of the pattern. The ATR Trailing Stop-Loss (Long/Short selectable) ads 1x ATR(10) above the pattern boundary (lower boundary given that it is a short setup) to calculate the first value as the initial protective stop-loss. As the price resumes lower, it shifts to 2x ATR(10) and trails the price until the high of the day hits the trailing stop-loss of the previous day’s calculation. At that point the trailing stop will stop plotting on the chart.

![]()
Here is the link that will direct you to the published script on Trading View.




