forked from DeviaVir/zenbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ta_macd.js
61 lines (55 loc) · 1.93 KB
/
ta_macd.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
var talib = require('talib')
module.exports = function macd (s, slow_period, fast_period, signal_period) {
return new Promise(function(resolve, reject) {
// check parameters
// if (fast_period > slow_period) {
// console.log('incorrect parameters MACD. (fast_period < slow_period || signal_period > fast_period)')
// return;
// }
//create object for talib. only close is used for now but rest might come in handy
if (!s.marketData) {
s.marketData = { open: [], close: [], high: [], low: [], volume: [] }
}
if (s.lookback.length > s.marketData.close.length) {
for (var i = (s.lookback.length - s.marketData.close.length) - 1; i >= 0; i--) {
s.marketData.close.push(s.lookback[i].close)
}
}
let periods_necessary = slow_period + signal_period - 1
//dont calculate until we have enough data
if (s.marketData.close.length >= periods_necessary) {
// fillup marketData for talib.
let tmpMarket = s.marketData.close.slice()
// add current period
tmpMarket.push(s.period.close)
talib.execute({
name: 'MACD',
startIdx: 0,
endIdx: tmpMarket.length - 1,
inReal: tmpMarket,
optInFastPeriod: fast_period,
optInSlowPeriod: slow_period,
optInSignalPeriod: signal_period
}, function (err, result) {
if (err) {
reject(err)
console.log(err)
return
}
//Result format: (note: outReal can have multiple items in the array)
// {
// begIndex: 8,
// nbElement: 1,
// result: { outReal: [ 1820.8621111111108 ] }
// }
resolve({
'macd': result.result.outMACD[(result.nbElement - 1)],
'macd_histogram': result.result.outMACDHist[(result.nbElement - 1)],
'macd_signal': result.result.outMACDSignal[(result.nbElement - 1)],
})
})
} else {
resolve()
}
})
}