TradingView
ZenAndTheArtOfTrading
Sep 10, 2019 5:54 AM

ATR Trailing Stop Loss Indicator 

AUD/JPYOANDA

Description

I rely on this script for both my live trading and my backtesting process. I couldn’t live without it. It’s extremely simple – all it does is calculate your trailing stop price.

How It Works
The first number in blue is the current ATR (pips). The second number in green is your trailing stop loss price for Long trades, and the third number in red is your trailing stop loss price for Short trades.

For short trades, the stop loss price is calculated by adding the current ATR value to the highest-high of the given lookback period.

For long trades, the stop loss price is calculated by subtracting the current ATR value from the lowest-low of the given lookback period.

Settings
ATR Length:
ATR period (how many candles to include in the calculation).

Use Structure?
If set to true, the script will use swing lows and highs in its calculation. If set to false, the script will ignore swing lows and highs and give you the distance of the ATR from the current candle close instead.

How Far To Look Back For High/Lows:
Candle lookback period for swing high/lows.

ATR X ?:
This controls your ATR multiplier. For example, if you want to use a 2x ATR stop, set this to 2.

Chart Companion:
Here is the chart companion script for this indicator:

tradingview.com/script/szZ5jO2w-ATR-Trailing-Stop-Indicator-Chart/

Source Code:
Go to https://zenandtheartoftrading.com/indicators/atr-trailing-stop-indicator/ for the source code – it’s free!

Release Notes

-
- Updated script to remove deprecated "transp" parameters
- Tidied up settings interface a little more
- To use the chart version of this script, click here: tradingview.com/script/szZ5jO2w-ATR-Trailing-Stop-Indicator-Chart/

Release Notes

Updated to Pine Script v5
Comments
JordanBelfort93
Great work. How do you determine where to Take profits or Stop loss?
ZenAndTheArtOfTrading
@JordanBelfort93, That's entirely up to you man, there's no right or wrong way to do that. Well, technically there is haha - whatever you do, don't cut your profits so short that they don't make up for your losses (on average). It depends entirely on what timeframe you're trading and the volatility of the instrument, but a general rule of thumb is that anything over 1:1 is good. So if you're using a 1 ATR stop loss, try not to take your profit until price has exceeded 1 ATR profit (and ideally much more).

I personally try to shoot for 2 ATR worth of profit before I consider exiting my profitable trades. If you risk 1% per trade and you're making +2% on your average winner, then you only need to win about 35% of your trades to be profitable over the long-term (key word being "average winner" - not all your winners will be that big, but over a large sample size, the average ought to be well over 1% on your average winner unless your strategy has a 60%+ win rate).

This video might help you understand the math behind risk:reward and risk management: youtube.com/watch?v=PGkOH-oFOSI&list=PLSP_1DBafH-Eh33zVCtmy3FuqULNn5nt1&index=4
gianverzi
I probably ask a silly question: if the prices never crossunder the line (for a short position for instance) how can this be used as a stop loss for automatic trading?
Many thanks
ZenAndTheArtOfTrading
@gianverzi, hi gianverzi! This script is not intended for automatic trading, it's a backtesting and trading tool that I use for managing positions manually :)

In order to create a trailing stop loss that gets hit by price the script needs to know where you entered your position and although that's possible to code it's much more complicated and requires coding in your entry conditions/patterns etc
gianverzi
@ZenAndTheArtOfTrading, understood, many thanks for the answer: now it is clear.
ZenAndTheArtOfTrading
@gianverzi, You're welcome gianverzi! If you're interested in coding and you want to see how to achieve what you're talking about, I recorded a lesson on how to create a trailing stop that behaves how you describe. Instead of specifying a entry pattern, you specify a time & date, but it does exactly what you are looking for: youtube.com/watch?v=WM8qOjKimUk
gianverzi
@ZenAndTheArtOfTrading, excellent I went through it and it is very good stuff. I got stuck when I tried to get rid of the Long/Short selection that the user must perform because I wanted to link it to the automatic opening of a long/short position from the strategy. I guess this is what the majority of people wants to do. Any help there? Many thanks.
pform77
wonderful script thanks
SparkyFlary
Is this the same trailing stop that Wilder uses that involves the use of a sic?
OutsourcE
I have a lot 'transp' warnings in my scripts.What needs to be done specifically?Can you please post an example?
More