avatarSofien Kaabar, CFA

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3053

Abstract

:800/1*DuVKWSJAI3uQ5rz9Atjurw.png"><figcaption></figcaption></figure><ul><li>Calculate a 13-period weighted moving average on the normalized values from the previous step.</li></ul><p id="c657">The following Figure shows the SNATR in action:</p><figure id="efb5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5xnrUOZH82fniqo4578t2g.png"><figcaption><b>EURUSD with the Smoothed Normalized ATR</b></figcaption></figure><h1 id="8210">Coding the Strategy</h1><p id="5753">Bear in mind that the presented strategy has default parameters that must be optimized for each market (while incorporating the risk of overfitting), therefore, it may not work well on all markets. You should take the idea of the strategy and try to make it better.</p><p id="848c">Trend following generally two <i>theoretical </i>ideas:</p><ul><li><b><i>The first idea is that bullish trends generally have low volatility, therefore, if we can predict lower values on the ATR, we may be entering a bullish trend.</i></b></li><li><b><i>The second idea is that bearish trends generally have high volatility, therefore, if we can predict higher values on the ATR, we may be entering a bearish trend.</i></b></li></ul><p id="43f5">The strategy tries to predict a reversal on the SNATR the moment it shows a return to normality while at the same time a moving average filter confirms this. Below are the trading conditions:</p><ul><li><b>A long signal is generated whenever the SNATR dips below 0.80 while the close price is above the 100-period moving average</b></li><li><b>A short signal is generated whenever the SNATR rises above 0.20 while the close price is below the 100-period moving average.</b></li></ul><figure id="89f9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*j3dIoAqLEbiicU3CsgsTPw.png"><figcaption><b>Signal chart</b></figcaption></figure><p id="4301">The code for the strategy is as follows:</p><div id="2b64"><pre>// This source <span class="hljs-selector-tag">code</span> is subject <span class="hljs-selector-tag">to</span> the terms of the Mozilla Public License <span class="hljs-number">2.0</span> at https://mozilla.org/MPL/<span class="hljs-number">2.0</span>/ // © Sofien-Kaabar

//@version=<span class="hljs-number">5</span> <span class="hljs-built_in">indicator</span>(<span class="hljs-string">"ATR Strategy"</span>, overlay = true)

atr = ta.<span class="hljs-built_in">atr</span>(<span class="hljs-number">60</span>) normalized_atr = (atr - ta.<span class="hljs-built_in">lowest</span>(atr, <span class="hljs-number">60</span>)) / ((ta.<span class="hljs-built_in">highest</span>(atr, <span class="hljs-number">60</span>)) - ta.<span class="hljs-built_in">lowest</span>(atr, <span class="hljs-number">60</span>)) smoothed_normalized_atr = ta.<span class="hljs-built_in">wma</span>(normalized_atr, <span class="hljs-number">13</span>)

buy = smoothed_normalized_atr < <span class="hljs-number">0.8</span> and smoothed_normalized_atr[<span class="hljs-number">1</span>] > <span class="hljs-number">0.8</span>

Options

and close > ta.<span class="hljs-built_in">sma</span>(close, <span class="hljs-number">100</span>) sell = smoothed_normalized_atr > <span class="hljs-number">0.2</span> and smoothed_normalized_atr[<span class="hljs-number">1</span>] < <span class="hljs-number">0.2</span> and close < ta.<span class="hljs-built_in">sma</span>(close, <span class="hljs-number">100</span>)

<span class="hljs-built_in">plotshape</span>(buy, style = shape.triangleup, color = color.green, location = location.belowbar, size = size.small) <span class="hljs-built_in">plotshape</span>(sell, style = shape.triangledown, color = color.red, location = location.abovebar, size = size.small)</pre></div><figure id="7e7d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*b2WhvYZsH9eb1Lh0Y7dVpg.png"><figcaption><b>Signal chart</b></figcaption></figure><p id="2dfa">The following Figure shows another signal chart on Tesla:</p><figure id="d851"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*neu_uFDPvNZUX9sCaroFDg.png"><figcaption><b>Signal chart</b></figcaption></figure><p id="3618">If you want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have <i>hands-on</i> detailed courses that I highly recommend.</p><div id="fead" class="link-block"> <a href="https://www.lumiwealth.com/algorithmic-trading-landing-page/?utm_source=influence&amp;utm_medium=medium&amp;utm_campaign=sofien"> <div> <div> <h2>Learn Algorithmic Trading with Python Lumiwealth</h2> <div><h3>undefined</h3></div> <div><p>undefined</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*FbCz_ARtbOdywMlC)"></div> </div> </div> </a> </div><h1 id="e420">Summary</h1><p id="05a4">To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.</p><p id="5d41">I recommend you always follow the the below steps whenever you come across a trading technique or strategy:</p><ul><li><i>Have a critical mindset and get rid of any emotions.</i></li><li><i>Back-test it using real life simulation and conditions.</i></li><li><i>If you find potential, try optimizing it and running a forward test.</i></li><li><i>Always include transaction costs and any slippage simulation in your tests.</i></li><li><i>Always include risk management and position sizing in your tests.</i></li></ul><p id="beb3">Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.</p></article></body>

Use This Indicator to Follow the Trend

Creating and Coding the Smoothed Normalized ATR Indicator

Volatility indicators are helpful in detecting trends. This article shows how to transform the ATR into a directional indicator and create a strategy out of it.

For a detailed and thorough collection of trend following trading strategies, you can check out my book. The book features a huge number of classic and modern techniques as it dwelves into the realm of technical analysis with different trading strategies. The book comes with its own GitHub.

Creating the Smoothed Normalized ATR (SNATR)

The Average True Range (ATR) is a technical analysis indicator that measures volatility in financial markets. It was introduced by J. Welles Wilder Jr. in his 1978 book, “New Concepts in Technical Trading Systems.” The ATR calculates the average of a security’s true range over a specified period, usually 14 days. The true range is the greatest of the following:

  • The current period’s high minus the current period’s low.
  • The absolute value of the current period’s high minus the previous period’s close.
  • The absolute value of the current period’s low minus the previous period’s close.

The ATR is the smoothed moving average of the true range calculations. By using these three values, the ATR measures the volatility of a security over a certain time period, taking into account any gaps or price movements that occur between periods. Traders can use the ATR to determine the potential size of price movements and adjust their trading strategies accordingly. For example, a high ATR value may indicate that a security is more volatile and therefore riskier to trade.

The SNATR is a simple transformation of the ATR to make it bounded. The steps to calculate it are as follows:

  • Calculate the ATR of a financial instrument using whichever lookback period required.
  • Normalize the ATR between 0 and 1 using the basic normalization function defined below:
  • Calculate a 13-period weighted moving average on the normalized values from the previous step.

The following Figure shows the SNATR in action:

EURUSD with the Smoothed Normalized ATR

Coding the Strategy

Bear in mind that the presented strategy has default parameters that must be optimized for each market (while incorporating the risk of overfitting), therefore, it may not work well on all markets. You should take the idea of the strategy and try to make it better.

Trend following generally two theoretical ideas:

  • The first idea is that bullish trends generally have low volatility, therefore, if we can predict lower values on the ATR, we may be entering a bullish trend.
  • The second idea is that bearish trends generally have high volatility, therefore, if we can predict higher values on the ATR, we may be entering a bearish trend.

The strategy tries to predict a reversal on the SNATR the moment it shows a return to normality while at the same time a moving average filter confirms this. Below are the trading conditions:

  • A long signal is generated whenever the SNATR dips below 0.80 while the close price is above the 100-period moving average
  • A short signal is generated whenever the SNATR rises above 0.20 while the close price is below the 100-period moving average.
Signal chart

The code for the strategy is as follows:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sofien-Kaabar

//@version=5
indicator("ATR Strategy", overlay = true)

atr = ta.atr(60)
normalized_atr = (atr - ta.lowest(atr, 60)) / ((ta.highest(atr, 60)) - ta.lowest(atr, 60))
smoothed_normalized_atr = ta.wma(normalized_atr, 13)

buy  = smoothed_normalized_atr < 0.8 and smoothed_normalized_atr[1] > 0.8 and close > ta.sma(close, 100)
sell = smoothed_normalized_atr > 0.2 and smoothed_normalized_atr[1] < 0.2 and close < ta.sma(close, 100)

plotshape(buy,  style = shape.triangleup, color = color.green, location =  location.belowbar, size = size.small)
plotshape(sell,  style = shape.triangledown, color = color.red, location =  location.abovebar, size = size.small)
Signal chart

The following Figure shows another signal chart on Tesla:

Signal chart

If you want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.

Summary

To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.

I recommend you always follow the the below steps whenever you come across a trading technique or strategy:

  • Have a critical mindset and get rid of any emotions.
  • Back-test it using real life simulation and conditions.
  • If you find potential, try optimizing it and running a forward test.
  • Always include transaction costs and any slippage simulation in your tests.
  • Always include risk management and position sizing in your tests.

Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.

Data Science
Trading
Investing
Cryptocurrency
Machine Learning
Recommended from ReadMedium