Average True Range

Overview



The true range is defined as the maximum of the follwoing:

  • High - Low
  • Absolute Value of (High - Previous Close)
  • Absolute Value of (Low - Previoius Close)


The average is a rolling average for a given number of periods (or window). For example, you could define the average true range of a period of 10 days, which would be a rolling average over the last 10 days.

The average true range is an indicator of how much an asset moves on a daily basis. In some ways, it is heuristic substitute for volatility,

The average true range is often used to set trading exits. That is, a stop losses or profit targets is often times set as given multiples of the average true range.

The true range is generally expected to move within a range. Viewed then as an osciallor, a low value is sometimes viewed as a signal to a large breakout.

Calculation



You can do a straighforward calculation of the true range without any additional libraries, as in the following.


let ans = [];
for(let i=1;i<data.length;i++){
  ans.push(Math.max(data[i].high - data[i].close, Math.abs(data[i].high - data[i-1].close), Math.abs(data[i].low - data[i-1].close)));
}

					

API



The davinci library hosts a technical analysis module that can be used to calculate the true range.


let tr = await import('/lib/finance/technical/v1.0.0/technical.mjs');
let price1 = {close:100, high:102, low:99};
let price2 = {close:100, high:102, Low:99};

let trueRange = tr.trueRange(price2, price1);

					


In addition, the library hosts a list module that adds a true range method the list api.


let tc = await import('/lib/finance/technical/v1.0.0/technical.mjs');
let prices = [{Close:100, High:102, Low:99},{Close:100, High:102, Low:99},{Close:100, High:102, Low:99}];

let data = $list(prices).mapAppend(p=>({high:p.High, low:p.Low, close:p.Close}),{
  averageTrueRange:data=>{
    let result = [null];
    for(let i=1;i<data.length;i++){
      result.push(tc.trueRange(data[i], data[i-1]))
    }
    return result;
  }
}).items;

					
try it

Contents