-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrueRange.ts
More file actions
64 lines (52 loc) · 1.44 KB
/
Copy pathTrueRange.ts
File metadata and controls
64 lines (52 loc) · 1.44 KB
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
61
62
63
64
import { Indicator, IndicatorInput } from '@/indicator';
export interface TrueRangeInput extends IndicatorInput {
low: number[];
high: number[];
close: number[];
}
export type TrueRangeOutput = number | undefined;
export interface TrueRangeTick {
low: number;
high: number;
close: number;
}
/**
* True Range
*
* A method to expand a classical range (high minus low) to include
* possible gap scenarios.
*
* Sources:
* https://www.macroption.com/true-range/
*/
export class TrueRange extends Indicator<TrueRangeOutput, TrueRangeTick> {
protected override result: TrueRangeOutput[] = [];
protected override generator;
constructor(input: TrueRangeInput) {
super(input);
this.generator = this.trGenerator();
this.generator.next();
input.high.forEach((high, index) => {
this.nextValue({
high: high,
low: input.low[index]!,
close: input.close[index]!,
});
});
}
private *trGenerator(): IterableIterator<TrueRangeOutput, never, TrueRangeTick> {
let tick = yield;
let lastClose, output;
while (true) {
if (lastClose === undefined) {
lastClose = tick.close;
tick = yield output;
}
const high = Math.abs(tick.high - lastClose);
const low = Math.abs(tick.low - lastClose);
output = Math.max(tick.high - tick.low, isNaN(high) ? 0 : high, isNaN(low) ? 0 : low);
lastClose = tick.close;
tick = yield output;
}
}
}