Average True Range python trading strategy

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:

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:

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:

How to calculate ATR using Python

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:

How to calculate Average True Range in Python

And this is how the indicator evolved over the last years:

Average True Range chart in Python

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 RULES

The 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:

Average True Range trading strategy in Python

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.

Similar Posts