Average True Range (ATR)
The Average True Range (ATR) is a technical analysis indicator, developed by J. Welles Wilder Jr., used to measure market volatility. It quantifies the degree of price movement by taking the average of the ‘true range’ over a specified number of periods. A higher ATR value indicates higher market volatility, while a lower ATR signifies a period of lower volatility or consolidation. Importantly, ATR does not provide an indication of price direction, only the magnitude of its movement.
ATR
=ATR(data, period) Example Usage
=ATR(A2:F500, 14)
Parameters
| Parameter | Type | Description | Status |
|---|---|---|---|
data | Range | Range of columns containing the date, Open, high, Low, close, volume data. | Required |
period | Number | Number of (periods) days over which to calculate the ATR. | Required |
Returns
A two-column array of dates and their corresponding ATR values.
Source Code
Copy the following code into your Apps Script editor (Extensions > Apps Script) to use the ATR function in your spreadsheet.
atr.js
/**
* Calculates the Average True Range (ATR) for a given data set and period.
*
* @param {array} data - An array of historical stock data. Expected to have at least 5 columns (Date, Open, High, Low, Close) for TR calculation.
* @param {number} period - The number of days over which the ATR is calculated.
* @returns {array} - A 2D array with headers: Date and ATR.
* @customfunction
*/
function ATR(data, period) {
// Argument validation
if (arguments.length !== 2) {
throw new Error(`Wrong number of arguments. Expected 2, but got ${arguments.length}.`);
}
if (typeof period !== 'number' || period <= 0 || !Number.isInteger(period)) {
throw new Error(`Invalid period. The period must be a positive integer. Got: ${period}`);
}
// Get True Range values using the TR function
const trueRanges = TR(data); // TR handles its own data validation
// Calculate the ATR using the SMA function
// SMA handles its own data validation (on trueRanges) and period validation
const atrValues = SMA(trueRanges, period);
// Update the ATR header for clarity
atrValues[0][1] = `ATR(${period})`;
return atrValues;
}