-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (39 loc) · 1.23 KB
/
index.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
'use strict'
const Decimal = require('decimal.js');
class ROC {
constructor(values, period) {
this.values = values ? values.reverse() : [];
this.period = period;
}
calculate(callback) {
this.calculateROC((err, results) => {
if (err) {
return callback(err);
}
callback(null, results);
});
}
calculateROC(callback) {
const data = [];
const values = this.values;
if (values && values.length > this.period) {
values.forEach((val, idx) => {
if (idx > (this.period - 1)) {
const closingPrice = values[(idx - (this.period ))];
const momentum = Decimal.sub(val, closingPrice).toNumber();
const roc = Decimal.mul(Decimal.div(momentum, closingPrice), 100).toNumber();
data[idx] = {
value: val,
closingPrice,
momentum,
roc
};
} else {
data[idx] = { value: val };
}
});
}
return callback(null, data);
}
}
module.exports = ROC;