forked from DeviaVir/zenbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srsi.js
63 lines (51 loc) · 1.87 KB
/
srsi.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
54
55
56
57
58
59
60
61
62
var mathjs = require('mathjs')
var rsi = require('./rsi')
module.exports = function srsi(s, key, rsi_periods, k_periods, d_periods) {
let samplesRequiredForStochRSI = rsi_periods + k_periods + 1
if (s.lookback.length >= samplesRequiredForStochRSI - 1) {
let RSI = []
if (typeof s.period.rsi !== 'undefined') {
RSI.push(s.period.rsi)
} else {
rsi(s, 'rsi', rsi_periods)
RSI.push(s.period.rsi)
}
s.lookback.slice(0, samplesRequiredForStochRSI - 1).forEach(function (period) {
if (period.rsi) {
RSI.push(period.rsi)
}
})
RSI.reverse()
if(RSI.length >= samplesRequiredForStochRSI) {
let stochRSI = []
for(let i = 0; i < (k_periods + d_periods - 1); i++) {
let rsiForPeriod = RSI.slice(i, rsi_periods + i)
let highestRSI = Math.max(...rsiForPeriod)
let lowestRSI = Math.min(...rsiForPeriod)
if(highestRSI == lowestRSI) {
stochRSI.push(0)
} else {
stochRSI.push(((RSI[(rsi_periods - 1) + i] - lowestRSI) / (highestRSI - lowestRSI)) * 100)
}
}
stochRSI.reverse()
let percentK = []
for(let i = 0; i < k_periods; i++) {
let kData = stochRSI.slice(i, k_periods + i)
if(kData.length == k_periods) {
percentK.push(mathjs.mean(kData))
}
}
let percentD = []
for(let i = 0; i < d_periods; i++) {
let dData = percentK.slice(i, d_periods + i)
if(dData.length == d_periods) {
percentD.push(mathjs.mean(dData))
}
}
s.period[key + '_K'] = percentK[0] == 0 ? 0 : mathjs.round(percentK[0], 2)
s.period[key + '_D'] = percentD[0] == 0 ? 0 : mathjs.round(percentD[0], 2)
//console.log('lib.srsi: For RSI', RSI[RSI.length - 1], '-', '%K is', s.period[key + '_K'], ', %D is', s.period[key + '_D'], ', period info', s.period);
}
}
}