How To Make An Average True Range (ATR) Trading Strategy In Python (Backtest, Performance, Setup, and Code)
Traders use a wide range of indicators when trading. Among the most popular are the momentum and mean-reversion indicators, but there also exist indicators that measure volatility, such as the average true range. Let’s make an average true range (ATR) trading strategy in Python.
The Average True Range (ATR) is a volatility indicator that, unlike other indicators, is not commonly used to generate trading signals. But we thought: Can we develop a profitable trading strategy using this indicator in Python?
In this article, we are going to look at what the average true range indicator is, how to calculate it in Python, and backtest a trading strategy using it on S&P 500 (SPY).
Related reading:
- We have many different trading systems for sale, and
- We have even more different free short-term trading systems.
What is the Average True Range?
The Average True Range (ATR) indicator is a technical analysis tool used in finance and trading to measure an asset’s volatility. It was developed by J. Welles Wilder Jr. in his book New Concepts in Technical Trading Systems. It is often used to help make decisions about placing stop-loss orders, determining trade positions, and understanding market conditions.
The most common way to use this indicator is to help determine where to place stop-loss orders. A larger ATR might suggest setting wider stop-loss levels to account for potential larger price swings, while a smaller ATR might call for tighter stop-loss levels. Another way to interpret it is for position sizing, given that traders might want to take smaller positions in high-volatility regimes and larger positions when volatility is low.
However, identifying potential breakout points can also help trade the asset. When the ATR value is significantly higher than usual, it could signal the potential for a strong price movement.
Python-related resources
We have written many articles about Python, and you might find these interesting:
- Python For Trading – How To Do It (Plenty of examples with code and images)
- Get Started With Python Making Trading Strategies (Step By Step)
- How To Download Data For Your Trading Strategy From Yahoo!Finance With Python
- Best Python Libraries For Algorithmic Trading – Examples
- How To Measure Skewness Of A Trading Strategy Using Python
- Python Bollinger Band Trading Strategy: Backtest, Rules, Code, Setup, Performance
- Python and RSI Trading Strategy
- Python and Momentum Trading Strategy
- How To Build A Trading Strategy From FRED Data In Python
- Python and MACD Trading Strategy: Backtest, Rules, Code, Setup, Performance
- How To Measure Skewness Of A Trading Strategy Using Python
- How To Do A Monte Carlo Simulation Using Python
How to calculate the Average True Range (ATR) in Python?
The ATR is the highest number of the following three:
- Today’s high minus today’s close(H – C)
- Today’s high minus yesterday’s close(H – C(-1))
- Today’s low minus yesterday’s close(L – C(-1))
The ATR is then a moving average, generally using 14 days, of the true ranges.
The ATR can also be easily calculated using Python and the pandas_ta library. Here is how to do it within just two lines of code:
We first downloaded all the historical SPY data from Yahoo Finance using the finance library and then used the pta.atr function from pandas_ta to calculate the ATR.
Here is the output of data:
And this is how the indicator evolved over the last years:
Now, it’s time to backtest a trading strategy using this indicator.
Average True Range (ATR) trading rules in Python
The trading rules of the strategy we are going to backtest are pretty simple(note that we are going to use the 14-day ATR):
Trading Rules
THIS SECTION IS FOR MEMBERS ONLY. _________________ BECOME A MEBER TO GET ACCESS TO TRADING RULES IN ALL ARTICLES CLICK HERE TO SEE ALL 350 ARTICLES WITH TRADING RULESThe idea is that we hold the asset when the volatility in the stock price is high and sell it when the volatility falls and is low.
Average True Range (ATR) backtest in Python
We backtested the strategy using the ETF version of the S&P 500, SPY. The data is not adjusted for dividends and splits. Here is the equity curve:
Here are some metrics and trading statistics about the performance:
- CAGR is 5.93% (buy and hold 7.81%)
- Time spent in the market is 53.40%
- Risk-adjusted return is 11.10% (CAGR divided by time spent in the market)
- Maximum drawdown is -51.59% (-56.47%)
The strategy performs better when adjusted for risk, although it has almost the same drawdown as buy and hold.
Average True Range (ATR) strategy in Python – conclusion
Today, we showed you how to calculate and develop an ATR trading strategy using Python. Although the results are not that promising, if we pair up the ATR with other indicators, we might obtain much better results.