-
Notifications
You must be signed in to change notification settings - Fork 0
/
STOCHRSI.js
53 lines (42 loc) · 1.18 KB
/
STOCHRSI.js
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
const RSI = require('./RSI.js');
class Indicator {
constructor(config) {
this.input = 'candle';
this.interval = config.interval;
this.config = config;
this.rsi = new RSI({
interval: config.rsi
});
this.rsiHistory = [];
this.stochHistory = [];
this.kHistory = [];
}
update(candle) {
this.rsi.update(candle);
this.rsiHistory.push(this.rsi.result);
const max = Math.max.apply(null, this.rsiHistory);
const min = Math.min.apply(null, this.rsiHistory);
const stoch = (this.rsi.result - min) / (max - min);
this.stochHistory.push(stoch);
const k = (this.sumArray(this.stochHistory) / this.stochHistory.length) * 100;
this.kHistory.push(k);
const d = this.sumArray(this.kHistory) / this.kHistory.length;
this.result = {
k,
d
};
if (this.rsiHistory.length === this.config.stoch) {
this.rsiHistory.shift();
}
if (this.stochHistory.length === this.config.k) {
this.stochHistory.shift();
}
if (this.kHistory.length === this.config.d) {
this.kHistory.shift();
}
}
sumArray(arr) {
return arr.reduce((acc, item) => acc + item, 0);
}
}
module.exports = Indicator;