-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindicatorUtility.js
128 lines (99 loc) · 3.27 KB
/
indicatorUtility.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const db = require('./db');
const utility = require('./utility');
const tulind = require('tulind');
const chalk = require('chalk');
const moment = require('moment');
async function retrieve(symbol, indicatorNames, indicatorPeriods, currentTime, limitQty) {
let dbResponse;
if (limitQty) {
dbResponse = db('indicators')
.whereIn('indicatorPeriod', indicatorPeriods)
.where('indicatorName', indicatorNames)
.where('symbol', symbol)
.where('time', '<', currentTime)
.orderBy('time', 'desc')
.limit(limitQty)
} else {
dbResponse = await db('indicators')
.whereIn('indicatorPeriod', indicatorPeriods)
.where('indicatorName', indicatorNames)
.where('symbol', symbol)
.orderBy('time', 'desc')
}
return dbResponse
}
async function insert(tickerSymbol, name, value, period, currentTime) {
await db('indicators')
.insert({
symbol: tickerSymbol,
indicatorName: name,
indicatorValue: value,
indicatorPeriod: period,
time: currentTime,
})
}
module.exports.retrieve = retrieve;
module.exports.calculateIndicator = async (indicator, data, period) => {
let indicatorFunc;
let indicatorData;
if (indicator === 'sma') {
indicatorFunc = tulind.indicators.sma.indicator;
} else if (indicator === 'dema') {
indicatorFunc = tulind.indicators.dema.indicator;
} else if (indicator === 'cci') {
indicatorFunc = tulind.indicators.cci.indicator;
} else if (indicator === 'ao') {
indicatorFunc = tulind.indicators.ao.indicator
}
if (period) {
indicatorData = await indicatorFunc(data, [period]);
} else {
indicatorData = await indicatorFunc(data, []);
}
return utility.roundToOneDecimal(indicatorData.pop().pop());
}
module.exports.saveIndicators = async (tickerSymbol, indicatorName, lastTimestamp, data) => {
await Promise.all(data.map(async (indicator) => {
const value = indicator.value ? parseInt(indicator.value) : 0;
let prevIndicatorTime = 0;
let prevIndicator = await retrieve(
tickerSymbol, indicatorName, [indicator.period], lastTimestamp, 1
)
if (prevIndicator.length === 1) {
prevIndicator = prevIndicator.pop();
prevIndicatorTime = parseInt(prevIndicator.time);
} else if (prevIndicator.length === 0) {
prevIndicatorTime = 0;
}
const indicatorDiffDays = utility.getDiffDays(
lastTimestamp, prevIndicatorTime * 1000, 'hour'
);
if (indicatorDiffDays > 0) {
await insert(
tickerSymbol, indicatorName, value, indicator.period, moment().unix()
)
}
}))
}
module.exports.isIndicatorsCross = (indicatorLow, indicatorHigh) => {
let crossText;
if (indicatorLow > indicatorHigh) {
crossText = chalk.bold.bgGreen('YES');
} else {
crossText = chalk.bold.bgRed('NO');
}
return crossText;
}
module.exports.getIndicatorValue = (tickerData) => {
let value = null;
if (tickerData !== null) {
return utility.roundToOneDecimal(tickerData.indicatorValue)
}
return value;
}
module.exports.filter = (data, indicatorName, indicatorPeriod) => {
const filteredData = data.filter(tickerData =>
tickerData.indicatorName === indicatorName && tickerData.indicatorPeriod == indicatorPeriod
)
return (filteredData.length > 0 ? filteredData.pop() : null);
}