Skip to main content

How to Add an ATR Trailing Stoploss to Your Strategy - TradingView Script (+ Source Code)

How to Add an ATR Trailing Stoploss to Your Strategy in TradingView

Free TradingView Scripts - How to Add an ATR Trailing Stoploss to Your Strategy - TradingView Script (+ Source Code)

Risk Disclaimer ❗

The information provided on this post does not constitute investment advice, financial advice, trading advice, or any other sort of advice, and you should not treat any of the posts's content as such. Free Trading View Scripts does not recommend any cryptocurrency should be bought, sold or held by you. Do conduct your own due diligence and consult your financial advisor before making any investment decisions.

Introduction

As promised, in this post we are going to add a trailing stoploss strategy to avoid giving back the profits to the market. Out of all the strategies available for trailing stop, I am choosing the ATR. The main reason I chose this is that the ATR changes based on the volatility of the candles. It takes into account the huge movements of the candle to avoid being whipsawed more often. You’ll see what I’m talking about later.

Before we start, you might want to use my referral link on Binance below to give you a 10% discount of commission whenever you place a trade.



Since I'll be giving this all away for free, please take a moment to subscribe to the youtube channel and like the youtube video version to support the blog and the channel.




The Strategy

For the strategy, we’ll be using the 3 EMA strategy script in the previous post (read it first if you haven't, here's the link). Then, we’ll add a trailing stop loss strategy using the ATR indicator.

The ATR or average true range as the name implies, checks the market volatility by computing the range of each candle and averaging it out. This indicator is best used as a stop loss and take profit target. By basing our stop loss level In this video we’ll be using the ATR as a trailing stop loss so that we’ll never give our profits back to the market.

Open up your tradingview and search for the Average True Range. Once, it's loaded up on your chart you can click the source code icon and it will open up in the editor below. You will need to copy paste this part of the code

Free TradingView Scripts - ATR Trailing Stop Loss Copy and Paste
Copy paste the code from built-in Average True Range Indicator

Then it should look like these:

Length

We'll be using 14 as the length of the ATR.

Multiplier/Factor

For the multiplier, we'll be using 5 because we want the stoploss be placed 5 times the ATR so that the price has room for movements. We will make it so that it's changeable in the indicator settings.


Prototype

Below you'll see a picture our prototype of our trailing stoploss. As you can see, this is what the ATR that we’ll use look like. The ATR is following the price. If the price moves up it moves up and if it moves down it moves down. And the atr moves based on the price volatility, notice that the atr also adjusts based on the range of the prices. The bigger the range of the candle the more loose the atr is and this is to avoid getting whipsawed.

Free TradingView Scripts - Custom built ATR Indicator
My Custom Built ATR Indicator


Combining

Let’s combine the trailing stop loss with our previous ema crossover strategy. Copy paste it into our ema crossover indicator.

Free TradingView Scripts - Combining Indicators by Cope & Pasting



Let’s add an input parameter which is a boolean variable that we can use to toggle on/off our trailing stop loss.


Then let’s add the code to compute the trailing stop loss. What we’ll do is we multiply the current ATR value based on the factor that we input, which is in this case it’s 5. Then for long we’ll subtract it to the close of the current candle and if it’s short then we’ll add it to the closing price instead. Since, we want our trailstop to secure  our profit, we will change the value of our stop only if the value is higher than the previous value if it’s long and lower than the previous value if the trade is short. This way, the trailing stoploss will always secure our profit.


Let’s place the code to set the stoploss into action. We can do this by using strategy.exit and using the stop parameter.


Because we are cool 😎, we need to draw it cooler. We can create a cloud like drawing by first plotting the closing price and trailing stop. After we plot those two, we can use those 2 lines and fill them up with a color. We want to color it green if it’s bullish and red if it’s bearish.

Free TradingView Scripts - Filling The ATR Trailstop with Colors
Filling the Trailstop with Colors


Conclusion

Looks like we won't have too worry too much now if the market ever crashes because we have a trailing stoploss in place. 

Free TradingView Scripts - Avoiding Market Crashes Using ATR Trailing Stop Loss
Exiting before the market crash



It's easy to add a trailing stoploss to any of your existing strategy. If your trading strategy is focused on trend following then I suggest you hold on to the position till the trailing stoploss is hit. 

Free TradingView Scripts - Trend Following using ATR Trailing Stoploss
Riding the trend till it breaks


But not all strategies needs a trailing stoploss, some strategies works better without a trailing stoploss. It depends on how the market moves really, the most important thing is you test your strategy first and you should know when the strategy is breaking.

Source Code

You can view the full source code here: source code

A Little Favor From You 😅

Since I give everything for free in this blog, please have time to support the blog by following or subscribing to some of my social media accounts:


👉 Blog


 

Comments

Popular posts from this blog

Simple 3-EMA Crossover Trading Strategy - Free Profitable TradingView Strategy Script (+ Source Code)

SIMPLE 3-EMA CROSSOVER TRADING STRATEGY FOR BEGINNERS Risk Disclaimer ❗ The information provided on this post does not constitute investment advice, financial advice, trading advice, or any other sort of advice, and you should not treat any of the posts's content as such. Free Trading View Scripts does not recommend any cryptocurrency should be bought, sold or held by you. Do conduct your own due diligence and consult your financial advisor before making any investment decisions. Introduction In this post, I’m gonna show you a simple yet profitable trading strategy that anyone can use to trade crypto. We’ll only be using one type of indicator and that is the Exponential Moving Average. Later in this post i’ll help you code this strategy using tradingview pinescript so that we can easily backtest historical data and see if this strategy has a chance or not. Before we start, you might want to use my referral link on Binance

Source Code - Simple 3-EMA Crossover TradingView Strategy

Source Code of Simple 3-EMA Crossover TradingView Strategy Script Related links Link to the blog post Link to youtube video version Source Code A Little Favor From You 😅 Since I give everything for free in this blog, please have time to support the blog by following or subscribing to some of my social media accounts: 👉 Youtube Channel 👉 Blog