forked from Bitvested/ta.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
256 lines (255 loc) · 6.69 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
async function rsi(data, len) {
var length = (!len) ? 14 : len;
var pl = [], arrsi = [];
for(var i = 1; i < data.length; i++) {
var diff = (data[i] - data[i - 1]) / data[i] * 100;
pl.push(diff);
if(pl.length >= length) {
var gain = 0, loss = 0;
for(q in pl) {
if(pl[q] < 0) loss += pl[q];
if(pl[q] >= 0) gain += pl[q];
}
gain /= length; loss = (Math.abs(loss)) / length;
rsi = Number(100 - 100 / (1 + (gain / loss)));
arrsi.push(rsi);
var diff = (data[i] - data[i - 1]) / data[i] * 100;
pl.push(diff);
pl.splice(0, 1);
}
}
return arrsi;
}
async function atr(data, len) {
var length = (!len) ? 14 : len;
var atr = [];
for(var i = 0; i < data.length; i++) {
if(atr.length < 1) {
atr.push(data[i][0] - data[i][2]);
} else {
var t0 = await Math.max((data[i][0] - data[i - 1][1]), (data[i][2] - data[i - 1][1]), (data[i][0] - data[i][2]));
var at =(atr[atr.length - 1] * (length - 1) + t0) / length;
atr.push(at);
}
}
return atr;
}
async function sma(data, len) {
var length = (!len) ? 14 : len;
var pl = [], sma = [];
for(var i = 0; i < data.length; i++) {
pl.push(data[i]);
if(pl.length >= length) {
var average = 0;
for(q in pl) {
average += pl[q];
}
sma.push(average / length);
pl.splice(0, 1);
}
}
return sma;
}
async function wma(data, len) {
var length = (!len) ? 14 : len;
var weight = 0;
for(var i = 1; i <= length; i++) weight += i;
var pl = [], wma = [];
for(var i = 0; i < data.length; i++) {
pl.push(data[i]);
if(pl.length >= length) {
var average = 0, index = 1;
for(q in pl) {
average += pl[q] * index / weight;
index++;
}
wma.push(average);
pl.splice(0, 1);
}
}
return wma;
}
async function ema(data, len) {
var length = (!len) ? 12 : len;
var pl = [], ema = [], prevema = 0,
weight = 2 / (length + 1);
for(var i = 0; i < data.length; i++) {
pl.push(data[i]);
var average = 0;
if(prevema == 0 && pl.length >= length) {
for(q in pl) {
average += pl[q];
}
average /= length;
ema.push(average);
prevema = average;
} else if(prevema != 0 && pl.length >= length) {
average = (data[i] - prevema) * weight + prevema;
ema.push(average);
prevema = average;
}
}
return ema;
}
async function macd(data, len1, len2) {
var length1 = (!len1) ? 12 : len1;
var length2 = (!len2) ? 26 : len2;
if(length1 > length2) [length1, length2] = [length2, length1];
var ema = await module.exports.ema(data, length1);
var emb = await module.exports.ema(data, length2);
ema.splice(0, length2 - length1);
var macd = [];
for(var i = 0; i < emb.length; i++) {
macd.push([ema[i] - emb[i]]);
}
return macd;
}
async function bands(data, len, dev) {
var length = (!len) ? 14 : len;
var deviations = (!dev) ? 1 : dev;
var pl = [], deviation = [], boll = [];
var sma = await module.exports.sma(data, length);
for(var i = 0; i < data.length; i++) {
pl.push(data[i]);
if(pl.length >= length) {
var devi = await module.exports.std(pl, length);
deviation.push(devi);
pl.splice(0, 1);
}
}
for(var i = 0; i < sma.length; i++) {
boll.push([sma[i] + deviation[i] * deviations, sma[i], sma[i] - deviation[i] * deviations]);
}
return boll;
}
async function bandwidth(data, len, dev) {
var length = (!len) ? 14 : len;
var deviations = (!dev) ? 1 : dev;
var pl = [], deviation = [], boll = [];
var sma = await module.exports.sma(data, length);
for(var i = 0; i < data.length; i++) {
pl.push(data[i]);
if(pl.length >= length) {
var devi = await module.exports.std(pl);
deviation.push(devi);
pl.push(data[i]);
pl.splice(0, 1);
}
}
for(var i = 0; i < deviation.length; i++) {
var bandwidth = (((sma[i] + deviation[i] * deviations) - (sma[i] - deviation[i] * deviations)) / sma[i]);
boll.push(bandwidth);
}
return boll;
}
async function std(data, len) {
var length = (!len) ? data.length : len;
var mean = data.reduce((a, b) => {
return Number(a) + Number(b);
}) / length;
var std = Math.sqrt(data.reduce((sq, n) => {
return sq + Math.pow(n - mean, 2);
}, 0) / (length));
return std;
}
async function aroon_up(data, len) {
var length = (!len) ? 10 : len;
var pl = [], aroon = [];
for(var i = 0; i < data.length; i++) {
pl.push(data[i]);
if(pl.length >= length) {
var hl = pl.slice(0);
hl.sort((a, b) => {
return a - b;
});
var h = hl[length - 1];
aroon.push((100 * (length - (pl.findIndex(x => x == h) + 1)) / length));
pl.splice(0, 1);
}
}
return aroon;
}
async function aroon_down(data, len) {
var length = (!len) ? 10 : len;
var pl = [], aroon = [];
for(var i = 0; i < data.length; i++) {
pl.push(data[i]);
if(pl.length >= length) {
var hl = pl.slice(0);
hl.sort((a, b) => {
return a - b;
});
var l = hl[0];
aroon.push((100 * (length - (pl.findIndex(x => x == l) + 1)) / length));
pl.splice(0, 1);
}
}
return aroon;
}
async function aroon_osc(data, len) {
var length = (!len) ? 25 : len;
var aroon = [];
var u = await module.exports.aroon.up(data, length);
var d = await module.exports.aroon.down(data, length);
for(var i = 0; i < u.length; i++) {
aroon.push(u[i] - d[i]);
}
return aroon;
}
async function mfi(data, len) {
var length = (!len) ? 14 : len;
var mfi = [], n = [], p = [];
for(var i = 0; i < data.length; i++) {
p.push(data[i][0]);
n.push(data[i][1]);
if(p.length >= length) {
var positive = 0, negative = 0;
for(q in p) positive += p[q];
for(q in n) negative += n[q];
mfi.push((100 - 100 / (1 + positive / negative)));
p.splice(0, 1);
n.splice(0, 1);
}
}
return mfi;
}
async function roc(data, len) {
var length = (!len) ? 14 : len;
var pl = [], roc = [];
for(var i = 0; i < data.length; i++) {
pl.push(data[i]);
if(pl.length >= length) {
roc.push((pl[length - 1] - pl[0]) / pl[0]);
pl.splice(0, 1);
}
}
return roc;
}
async function obv(data) {
var obv = [0];
for(var i = 1; i < data.length; i++) {
if(data[i][1] > data[i - 1][1]) obv.push(obv[obv.length - 1] + data[i][0])
if(data[i][1] < data[i - 1][1]) obv.push(obv[obv.length - 1] - data[i][0])
if(data[i][1] == data[i - 1][1]) obv.push(obv[obv.length - 1])
}
return obv;
}
module.exports = {
rsi: rsi,
atr: atr,
sma: sma,
wma: wma,
ema: ema,
macd: macd,
bands: bands,
bandwidth: bandwidth,
std: std,
aroon: {
up: aroon_up,
down: aroon_down,
osc: aroon_osc,
},
mfi: mfi,
roc: roc,
obv: obv
}