-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrossUtility.js
72 lines (54 loc) · 1.98 KB
/
crossUtility.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
const indicatorUtility = require('./indicatorUtility');
const utility = require('./utility');
const chalk = require('chalk');
const _ = require('lodash');
function calculateCrossChange(currentLow, currentHigh, prevLow, prevHigh) {
const currCrossValue = currentLow - currentHigh;
let prevCrossValue = currCrossValue;
if (prevLow && prevHigh) {
prevCrossValue = prevLow - prevHigh;
}
let change = utility.roundToOneDecimal(currCrossValue - prevCrossValue);
if (change > 0) {
change = chalk.bold.bgGreen(change);
} else if (change < 0) {
change = chalk.bold.bgRed(change);
}
return change
}
module.exports.calculateCrossChange = calculateCrossChange;
module.exports.getCrossChange = async (tickerSymbol, indicatorName, lastTickerTimestamp, data) => {
// Render prev indicator cross change
const lowData = data[0];
const highData = data[1];
const periods = [lowData.period, highData.period]
const dbIndicators = await indicatorUtility.retrieve(
tickerSymbol, indicatorName, periods, lastTickerTimestamp
);
const filteredDBIndicators = [];
periods.forEach(period => {
const periodIndicators = dbIndicators.filter(indicator => (
parseInt(indicator.indicatorPeriod) === period
))
for(let i = 0; i < periodIndicators.length; i += 1){
const dbIndicatorPeriod = parseInt(periodIndicators[i].time) * 1000;
const hour = utility.getDiffDays(lastTickerTimestamp, dbIndicatorPeriod, 'hour')
if (hour > 0) {
filteredDBIndicators.push(_.cloneDeep(periodIndicators[i]))
break;
}
}
})
const prevLow = indicatorUtility.filter(
filteredDBIndicators, indicatorName, lowData.period
);
const prevHigh = indicatorUtility.filter(
filteredDBIndicators, indicatorName, highData.period
);
const crossChangeValue = calculateCrossChange(
lowData.value, highData.value,
indicatorUtility.getIndicatorValue(prevLow),
indicatorUtility.getIndicatorValue(prevHigh)
)
return crossChangeValue
}