# Average True Range

## Technical Analysis Indicator: atr ## Function Prototype

/* Average True Range */
/* Type: indicator */
/* Input arrays: 3    Options: 1    Output arrays: 1 */
/* Inputs: high, low, close */
/* Options: period */
/* Outputs: atr */
int ti_atr_start(TI_REAL const *options);
int ti_atr(int size,
TI_REAL const *const *inputs,
TI_REAL const *options,
TI_REAL *const *outputs);

## Description

This documentation is still a work in progress. It has omissions, and it probably has errors too. If you see any issues, or have any general feedback, please get in touch.

Average True Range is a measure of volatility. It represents roughly how much you can expect a security to change in price on any given day. It is often used in position sizing formulas.

Average true range is calculated by applying Wilders Smoothing to True Range.

True range for each day is the greatest of:

• Day's high minus day's low
• The absolute value of the day's high minus the previous day's close
• The absolute value of the day's low minus the previous day's close
$$tr_{t} = maximum(high_{t} - low_{t}, |high_{t} - close_{t-1}|, |low_{t} - close_{t-1}|)$$

True range is then smoothed using Wilders Smoothing which yields the average true range.

$$atr_{t} = tr_{t} \cdot \frac{1}{n} + atr_{t-1} \cdot \frac{n-1}{n}$$

## Example Usage

### Calling From C

/* Example usage of Average True Range */
/* Assuming that 'high', 'low', and 'close' are pre-loaded arrays of size 'in_size'. */
TI_REAL *inputs[] = {high, low, close};
TI_REAL options[] = {5}; /* period */
TI_REAL *outputs; /* atr */

/* Determine how large the output size is for our options. */
const int out_size = in_size - ti_atr_start(options);

/* Allocate memory for output. */
outputs = malloc(sizeof(TI_REAL) * out_size); assert(outputs != 0); /* atr */

/* Run the actual calculation. */
const int ret = ti_atr(in_size, inputs, options, outputs);
assert(ret == TI_OKAY);


### Calling From Lua (with Tulip Chart bindings)

-- Example usage of Average True Range
atr = ti.atr(high, low, close, 5)


## Example Calculation

period = 5

datehighlowcloseatr
2005-11-0182.1581.2981.59
2005-11-0281.8980.6481.06
2005-11-0383.0381.3182.87
2005-11-0483.3082.6583.00
2005-11-0783.8583.0783.611.12
2005-11-0883.9083.1183.151.05
2005-11-0983.3382.4982.841.01
2005-11-1084.3082.3083.991.21
2005-11-1184.8484.1584.551.14
2005-11-1485.0084.1184.361.09
2005-11-1585.9084.0385.531.24
2005-11-1686.5885.3986.541.23
2005-11-1786.9885.7686.891.23
2005-11-1888.0087.1787.771.21
2005-11-2187.8787.0187.291.14

## Other Indicators

Previous indicator: Vector Arctangent

Next indicator: Average Price

Random indicator: Typical Price