Average True Range Google Sheets Formula

Average True Range Google Sheets Formula

Last Updated on 24/05/2024 by Ndanileka

What is the Average True Range

The Average True Range is an indicator used to determine market volatility; it measures how much price moves on average over a given period. Welles Wilder introduced it in his book “New Concepts in Technical Trading Systems.” 

To calculate the ATR, we first need to find True Ranges (TR) for a given period. Where TR is the greatest of the following:

  • The distance from today’s high to today’s low.
  • The distance from yesterday’s close to today’s high.
  • The distance from yesterday’s close to today’s low

The Average True Range is a moving average of the True Ranges.
Learn more about the TR calculation in this post.

ATR Google Sheets Formula (Tutorial)

In the below example, we are going to calculate 14-period ATR

Cell B1 = “TSLA” ( Tesla, Inc)

Step 1 – Requesting data and creating a dataset

Request 3 price points: Today’s High, Today’s Low, Yesterday’s Close.

Step 1.1.1 – Historical High & Low Request
Request “all” prices from the previous 25 days. We request more days than required to account for weekends and holidays; more about this here.

=GOOGLEFINANCE(B1,"all",TODAY()-25, TODAY())
Historical Price Request

Step 1.1.2 – Historical High & Low Request

Create a new query, then select the High and Low columns from step 1.1.1, sort the values in date descending order, and limit the values returned to 14.

=QUERY(GOOGLEFINANCE(B1,"all",TODAY()-25, TODAY()),"SELECT Col3, Col4 ORDER BY Col1 DESC LIMIT 14")
Historical High and Low formula

Step 1.2.1 – Yesterday’s Close Request

Request close prices from the previous 25 days,

=QUERY(GOOGLEFINANCE(B1,"Close",today()-25, today()))
Historical close price formula

Step 1.2.2 – Yesterday’s Close Request

Create a new query, then select the close column from step 1.2.1, sort the values in date descending order, limit the values return to 14, offset by 1, and label the column as “Closeyest”.

=QUERY(GOOGLEFINANCE(B1,"Close",today()-25, today())," SELECT Col2 ORDER BY Col1 DESC LIMIT 14 OFFSET 1 LABEL Col2 'Closeyest'")
Yesterday’s close price formula

1.3 – Create a single dataset

We then combine the queries from 1.1.2 and 1.2.2 into a single dataset by wrapping them in brackets{} and using “,” as a separator.
1.3 = QUERY({1.1.2, 1.2.2})

=QUERY({QUERY(GOOGLEFINANCE(B1,"all",TODAY()-25, TODAY()),"SELECT Col3, Col4 ORDER BY Col1 DESC LIMIT 14"),QUERY(GOOGLEFINANCE(B1,"Close",today()-25, today())," SELECT Col2 ORDER by Col1 DESC LIMIT 14 OFFSET 1 LABEL Col2 'Closeyest'")})
Historical High, Low, Yesterday’s Close formula

Step 2 Average True Range Calculation

Step 2.1.1 True Ranges (Price Difference)
We use the dataset from 1.3 to calculate the price differences between:
(High – Low) , (High – Yesterday’s Close), (Yesterday’s Close – Low).
Learn more about the true range in this post, where it is explained in detail.

=QUERY(QUERY({QUERY(GOOGLEFINANCE(B1,"all",TODAY()-25, TODAY()),"SELECT Col3, Col4 ORDER BY Col1 DESC LIMIT 14"),QUERY(GOOGLEFINANCE(B1,"Close",today()-25, today()),"SELECT Col2 ORDER BY Col1 DESC LIMIT 14 OFFSET 1 LABEL Col2 'Closeyest'")}),"SELECT Col1-Col2, Col1-Col3, Col3-Col2 ")
Historical Price differences

Step 2.1.2True Ranges (Absolute Values)

We then wrap the values returned in 2.1.1 with the ABS() function to return positive values.
Since the data is an array, we need to wrap the formula with the ARRAYFORMULA() function for it to work.

=ARRAYFORMULA(ABS(QUERY(QUERY({QUERY(GOOGLEFINANCE(B1,"all",TODAY()-25, TODAY()),"SELECT Col3, Col4 ORDER BY Col1 DESC LIMIT 14"),QUERY(GOOGLEFINANCE(B1,"Close",today()-25, today()),"SELECT Col2 ORDER BY Col1 DESC LIMIT 14 OFFSET 1 LABEL Col2 'Closeyest'")}),"SELECT Col1-Col2, Col1-Col3, Col3-Col2 ")))
Absolute value Historical Price Differences

Step 2.1.3 True Ranges (Transpose dataset)

The QUERY() function does not have a built-in method to “SELECT” values per row.
Therefore, we transpose the dataset to have the rows as columns; this will assist us when calculating the True Range for each day.

=ARRAYFORMULA(TRANSPOSE(ABS(QUERY(QUERY({QUERY(GOOGLEFINANCE(B1,"all",TODAY()-25, TODAY()),"SELECT Col3, Col4 ORDER BY Col1 DESC LIMIT 14"),QUERY(GOOGLEFINANCE(B1,"Close",today()-25, today()),"SELECT Col2 ORDER BY Col1 DESC LIMIT 14 OFFSET 1 LABEL Col2 'Closeyest'")}),"SELECT Col1-Col2, Col1-Col3, Col3-Col2 "))))

Step 2.1.4 – True Ranges ( Max(Col) String)

We could create a new query with the formula below to find the True Range for each day.
Each column’s “Max Value” equals the “True Range” for that day.


Query( (Step 2.1.3),"SELECT Max(Col1), Max(Col2), Max(Col3), Max(Col4), Max(Col5), Max(Col6), Max(Col7), Max(Col8), Max(Col9), Max(Col10), Max(Col1), Max(Col2), Max(Col13),Max(Col14), Max(Col15))"

However, this would be time-consuming and every time the ATR(Period) is changed, we would need to edit the number of “Max(Col)’s”.
Therefore, we need to automate the process of creating the Max(Col) “string“.
We will use the functions INDIRECT(), AMPERSAND “&”, ROW(), and JOIN() to accomplish this.

=ARRAYFORMULA((JOIN(",max(Col",row(indirect("A1:A15"))&")")))
Max(Col) string

We then combine our automated string with “SELECT Max(Col” to complete the query.
To change the number of “Max(Col)” we only need to edit “A15”.

View this post from infoinspired.com for a detailed explanation of how to find Max Values in each row.

Step 2.1.5 – True Ranges ( Per Day)

=ARRAYFORMULA(QUERY(TRANSPOSE(ABS(QUERY(QUERY({QUERY(GOOGLEFINANCE(B1,"all",TODAY()-25, TODAY()),"SELECT Col3, Col4 ORDER BY Col1 DESC LIMIT 14"),QUERY(GOOGLEFINANCE(B1,"Close",today()-25, today()),"SELECT Col2 ORDER BY Col1 DESC LIMIT 14 OFFSET 1 LABEL Col2 'Closeyest'")}),"SELECT Col1-Col2, Col1-Col3, Col3-Col2 "))),"SELECT max(Col"&JOIN(",max(Col",row(indirect("A1:A15"))&")")))
Historical True Range Formula

Step 2.1.6 – Calculate Average True Range

To complete the ATR formula, we wrap Step 2.15 with the AVERAGE() function.

=AVERAGE(ARRAYFORMULA(QUERY(TRANSPOSE(ABS(QUERY(QUERY({QUERY(GOOGLEFINANCE(B1,"all",TODAY()-25, TODAY()),"SELECT Col3, Col4 ORDER BY Col1 DESC LIMIT 14"),QUERY(GOOGLEFINANCE(B1,"Close",today()-25, today()),"SELECT Col2 ORDER BY Col1 DESC LIMIT 14 OFFSET 1 LABEL Col2 'Closeyest'")}),"SELECT Col1-Col2, Col1-Col3, Col3-Col2 "))),"SELECT max(Col"&JOIN(",max(Col",row(indirect("A1:A15"))&")"))))
ATR formula

The charts below show Tesla, Inc.’s ATR on June 17, 2022, which matches our calculation.

17/06/2022 @ ATR(14) 46.49

Spreadsheet

The ATR calculator generates values using the formula we completed above.
To use input the stock code and desired period to generate an ATR Value.

ATR Calculator Spreadsheet

Happy Trading!

4 thoughts on “Average True Range Google Sheets Formula

  1. Hi i see your sheet its great,
    i have 2 questions
    1. are any way i can calculate ATR Wilder and not ATR Simple???

    2. For example if i like to know the ATR from a stock from 01/10/2023 to 02/21/2023 are any way???

    Thanks

    1. Hi Leandro,
      If you create a sheet tab (like sheet 1) and load the historical data.
      Then on sheet 2, create the formulas and histogram for the ATR. It should be easy.
      The tricky part is when trying to create single cell formulas without loading any data.

      Send a sheet to [email protected] that we can collaborate on for the formulas.

Leave a Reply

Back To Top

Discover more from DEUS HARVEST

Subscribe now to keep reading and get access to the full archive.

Continue reading