forked from DeviaVir/zenbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathti_stochrsi.js
89 lines (72 loc) · 2.41 KB
/
ti_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
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
82
83
84
85
86
87
var tulind = require('tulind')
module.exports = function ti_stochrsi(s, key, rsi_period, k_periods, d_periods, optMarket)
{
return new Promise(function(resolve, reject) {
//dont calculate until we have enough data
let tmpMarket = optMarket
if (!tmpMarket)
{
tmpMarket = s.lookback.slice(0, 1000).map(x=>x.close)
tmpMarket.reverse()
//add current period
tmpMarket.push(s.period.close)
}
else
{
tmpMarket = tmpMarket.map(x=>x.close)
}
if ( tmpMarket.length >= rsi_period) {
//doublecheck length.
if (tmpMarket.length >= rsi_period) {
// extract int from string input for ma_type
tulind.indicators.rsi.indicator(
[tmpMarket],
[rsi_period]
, function (err, result) {
if (err) {
console.log(err)
reject(err, result)
return
}
let trsi = result[0]
// 0 oldest -- end newest
trsi.reverse()
let stochRSI = []
for(let i = 0; i < (k_periods + d_periods - 1); i++) {
let rsiForPeriod = trsi.slice(i, rsi_period + i)
let highestRSI = Math.max(...rsiForPeriod)
let lowestRSI = Math.min(...rsiForPeriod)
if(highestRSI == lowestRSI) {
stochRSI.push(0)
} else {
stochRSI.push(((trsi[ (rsi_period - 1) + i] - lowestRSI) / (highestRSI - lowestRSI)) )
}
}
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(kData.reduce((a,b) => a + b, 0) / kData.length )
}
}
let percentD = []
for(let i = 0; i < d_periods; i++) {
let dData = stochRSI.slice(i, d_periods + i)
if(dData.length == d_periods) {
percentD.push(dData.reduce((a,b) => a + b, 0) / dData.length)
}
}
resolve({
stochRSI: stochRSI,
stochk :percentK,
stochd :percentD
})
})
}
else {
reject('MarketLenth not populated enough')
}
} else {
reject('MarketLenth not populated enough')}
})
}