-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathaverageTrueRange.ts
60 lines (53 loc) · 1.27 KB
/
averageTrueRange.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { tr } from './trueRange';
import { sma } from '../trend/simpleMovingAverage';
/**
* Average true range result.
*/
export interface ATRResult {
trLine: number[];
atrLine: number[];
}
/**
* Optional configuration of ATR parameters.
*/
export interface ATRConfig {
period?: number;
}
/**
* The default configuration of ATR.
*/
export const ATRDefaultConfig: Required<ATRConfig> = {
period: 14,
};
/**
* Average True Range (ATR). It is a technical analysis indicator that
* measures market volatility by decomposing the entire range of stock
* prices for that period.
*
* TR = Max((High - Low), (High - Closing), (Closing - Low))
* ATR = SMA TR
*
* @param highs high values.
* @param lows low values.
* @param closings closing values.
* @param config configuration.
* @return atr tr line and atr line.
*/
export function atr(
highs: number[],
lows: number[],
closings: number[],
config: ATRConfig = {}
): ATRResult {
const { period } = { ...ATRDefaultConfig, ...config };
const trLine = tr(highs, lows, closings);
const atrLine = sma(trLine, { period });
return {
trLine,
atrLine,
};
}
// Export full name
export { atr as averageTrueRange };