forked from anandanand84/technicalindicators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChandelierExit.ts
81 lines (73 loc) · 2.42 KB
/
ChandelierExit.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Indicator, IndicatorInput } from '../indicator/indicator';
import { ATR } from '../directionalmovement/ATR';
import LinkedList from '../Utils/FixedSizeLinkedList';
export class ChandelierExitInput extends IndicatorInput {
period : number = 22;
multiplier : number = 3;
high : number[];
low : number[];
close : number[];
}
export class ChandelierExitOutput extends IndicatorInput {
exitLong : number;
exitShort : number;
};
export class ChandelierExit extends Indicator {
generator:IterableIterator<ChandelierExitOutput | undefined>;
constructor (input:ChandelierExitInput) {
super(input);
var highs = input.high;
var lows = input.low;
var closes = input.close;
this.result = [];
var atrProducer = new ATR({period : input.period, high : [], low : [], close : [], format : (v) => {return v}});
var dataCollector = new LinkedList(input.period * 2, true, true, false);
this.generator = (function* (){
var result;
var tick = yield;
var atr;
while (true)
{
var { high, low } = tick;
dataCollector.push(high);
dataCollector.push(low);
atr = atrProducer.nextValue(tick)
if((dataCollector.totalPushed >= (2 * input.period)) && atr!= undefined) {
result = {
exitLong : dataCollector.periodHigh - atr * input.multiplier,
exitShort : dataCollector.periodLow + atr * input.multiplier
}
}
tick = yield result
}
})();
this.generator.next();
highs.forEach((tickHigh, index) => {
var tickInput = {
high : tickHigh,
low : lows[index],
close : closes[index],
}
var result = this.generator.next(tickInput);
if(result.value != undefined){
this.result.push(result.value);
}
});
};
static calculate = chandelierexit;
nextValue(price:ChandelierExitInput):ChandelierExitOutput | undefined {
var result = this.generator.next(price);
if(result.value != undefined){
return result.value;
}
};
}
export function chandelierexit(input:ChandelierExitInput):number[] {
Indicator.reverseInputs(input);
var result = new ChandelierExit(input).result;
if(input.reversedInput) {
result.reverse();
}
Indicator.reverseInputs(input);
return result;
};