-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtrueRange.ts
42 lines (36 loc) · 855 Bytes
/
trueRange.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import {
abs,
checkSameLength,
max,
shiftRightAndFillBy,
subtract,
} from '../../helper/numArray';
/**
* True Range (TR).
*
* TR = Max((High - Low), Abs(High - Closing[-1]), Abs(Low - Closing[-1]))
*
* @param period window period.
* @param highs high values.
* @param lows low values.
* @param closings closing values.
* @return tr values.
*/
export function tr(
highs: number[],
lows: number[],
closings: number[]
): number[] {
checkSameLength(highs, lows, closings);
const previous = shiftRightAndFillBy(1, closings[0], closings);
const result = max(
subtract(highs, lows),
abs(subtract(highs, previous)),
abs(subtract(lows, previous))
);
return result;
}
// Export full name
export { tr as trueRange };