@typedtrader/exchange 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/index.cjs +967 -0
- package/dist/index.d.cts +361 -0
- package/dist/index.d.mts +361 -0
- package/dist/index.mjs +919 -0
- package/package.json +63 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,919 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import ms, { default as ms$1 } from "ms";
|
|
4
|
+
import jsonabc from "jsonabc";
|
|
5
|
+
import { ApiError, Client, OrderStatus } from "@master-chief/alpaca-ts";
|
|
6
|
+
import { retry } from "ts-retry-promise";
|
|
7
|
+
|
|
8
|
+
//#region src/candle/BatchedCandle.ts
|
|
9
|
+
const BasicCandlePrice = z.union([
|
|
10
|
+
z.literal("open"),
|
|
11
|
+
z.literal("high"),
|
|
12
|
+
z.literal("low"),
|
|
13
|
+
z.literal("close")
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region ../../node_modules/big.js/big.mjs
|
|
18
|
+
/************************************** EDITABLE DEFAULTS *****************************************/
|
|
19
|
+
var DP = 20, RM = 1, MAX_DP = 1e6, MAX_POWER = 1e6, NE = -7, PE = 21, STRICT = false, NAME = "[big.js] ", INVALID = NAME + "Invalid ", INVALID_DP = INVALID + "decimal places", INVALID_RM = INVALID + "rounding mode", DIV_BY_ZERO = NAME + "Division by zero", P = {}, UNDEFINED = void 0, NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
|
|
20
|
+
function _Big_() {
|
|
21
|
+
function Big$1(n) {
|
|
22
|
+
var x = this;
|
|
23
|
+
if (!(x instanceof Big$1)) return n === UNDEFINED && arguments.length === 0 ? _Big_() : new Big$1(n);
|
|
24
|
+
if (n instanceof Big$1) {
|
|
25
|
+
x.s = n.s;
|
|
26
|
+
x.e = n.e;
|
|
27
|
+
x.c = n.c.slice();
|
|
28
|
+
} else {
|
|
29
|
+
if (typeof n !== "string") {
|
|
30
|
+
if (Big$1.strict === true && typeof n !== "bigint") throw TypeError(INVALID + "value");
|
|
31
|
+
n = n === 0 && 1 / n < 0 ? "-0" : String(n);
|
|
32
|
+
}
|
|
33
|
+
parse(x, n);
|
|
34
|
+
}
|
|
35
|
+
x.constructor = Big$1;
|
|
36
|
+
}
|
|
37
|
+
Big$1.prototype = P;
|
|
38
|
+
Big$1.DP = DP;
|
|
39
|
+
Big$1.RM = RM;
|
|
40
|
+
Big$1.NE = NE;
|
|
41
|
+
Big$1.PE = PE;
|
|
42
|
+
Big$1.strict = STRICT;
|
|
43
|
+
Big$1.roundDown = 0;
|
|
44
|
+
Big$1.roundHalfUp = 1;
|
|
45
|
+
Big$1.roundHalfEven = 2;
|
|
46
|
+
Big$1.roundUp = 3;
|
|
47
|
+
return Big$1;
|
|
48
|
+
}
|
|
49
|
+
function parse(x, n) {
|
|
50
|
+
var e, i, nl;
|
|
51
|
+
if (!NUMERIC.test(n)) throw Error(INVALID + "number");
|
|
52
|
+
x.s = n.charAt(0) == "-" ? (n = n.slice(1), -1) : 1;
|
|
53
|
+
if ((e = n.indexOf(".")) > -1) n = n.replace(".", "");
|
|
54
|
+
if ((i = n.search(/e/i)) > 0) {
|
|
55
|
+
if (e < 0) e = i;
|
|
56
|
+
e += +n.slice(i + 1);
|
|
57
|
+
n = n.substring(0, i);
|
|
58
|
+
} else if (e < 0) e = n.length;
|
|
59
|
+
nl = n.length;
|
|
60
|
+
for (i = 0; i < nl && n.charAt(i) == "0";) ++i;
|
|
61
|
+
if (i == nl) x.c = [x.e = 0];
|
|
62
|
+
else {
|
|
63
|
+
for (; nl > 0 && n.charAt(--nl) == "0";);
|
|
64
|
+
x.e = e - i - 1;
|
|
65
|
+
x.c = [];
|
|
66
|
+
for (e = 0; i <= nl;) x.c[e++] = +n.charAt(i++);
|
|
67
|
+
}
|
|
68
|
+
return x;
|
|
69
|
+
}
|
|
70
|
+
function round(x, sd, rm, more) {
|
|
71
|
+
var xc = x.c;
|
|
72
|
+
if (rm === UNDEFINED) rm = x.constructor.RM;
|
|
73
|
+
if (rm !== 0 && rm !== 1 && rm !== 2 && rm !== 3) throw Error(INVALID_RM);
|
|
74
|
+
if (sd < 1) {
|
|
75
|
+
more = rm === 3 && (more || !!xc[0]) || sd === 0 && (rm === 1 && xc[0] >= 5 || rm === 2 && (xc[0] > 5 || xc[0] === 5 && (more || xc[1] !== UNDEFINED)));
|
|
76
|
+
xc.length = 1;
|
|
77
|
+
if (more) {
|
|
78
|
+
x.e = x.e - sd + 1;
|
|
79
|
+
xc[0] = 1;
|
|
80
|
+
} else xc[0] = x.e = 0;
|
|
81
|
+
} else if (sd < xc.length) {
|
|
82
|
+
more = rm === 1 && xc[sd] >= 5 || rm === 2 && (xc[sd] > 5 || xc[sd] === 5 && (more || xc[sd + 1] !== UNDEFINED || xc[sd - 1] & 1)) || rm === 3 && (more || !!xc[0]);
|
|
83
|
+
xc.length = sd;
|
|
84
|
+
if (more) for (; ++xc[--sd] > 9;) {
|
|
85
|
+
xc[sd] = 0;
|
|
86
|
+
if (sd === 0) {
|
|
87
|
+
++x.e;
|
|
88
|
+
xc.unshift(1);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (sd = xc.length; !xc[--sd];) xc.pop();
|
|
93
|
+
}
|
|
94
|
+
return x;
|
|
95
|
+
}
|
|
96
|
+
function stringify(x, doExponential, isNonzero) {
|
|
97
|
+
var e = x.e, s = x.c.join(""), n = s.length;
|
|
98
|
+
if (doExponential) s = s.charAt(0) + (n > 1 ? "." + s.slice(1) : "") + (e < 0 ? "e" : "e+") + e;
|
|
99
|
+
else if (e < 0) {
|
|
100
|
+
for (; ++e;) s = "0" + s;
|
|
101
|
+
s = "0." + s;
|
|
102
|
+
} else if (e > 0) {
|
|
103
|
+
if (++e > n) for (e -= n; e--;) s += "0";
|
|
104
|
+
else if (e < n) s = s.slice(0, e) + "." + s.slice(e);
|
|
105
|
+
} else if (n > 1) s = s.charAt(0) + "." + s.slice(1);
|
|
106
|
+
return x.s < 0 && isNonzero ? "-" + s : s;
|
|
107
|
+
}
|
|
108
|
+
P.abs = function() {
|
|
109
|
+
var x = new this.constructor(this);
|
|
110
|
+
x.s = 1;
|
|
111
|
+
return x;
|
|
112
|
+
};
|
|
113
|
+
P.cmp = function(y) {
|
|
114
|
+
var isneg, x = this, xc = x.c, yc = (y = new x.constructor(y)).c, i = x.s, j = y.s, k = x.e, l = y.e;
|
|
115
|
+
if (!xc[0] || !yc[0]) return !xc[0] ? !yc[0] ? 0 : -j : i;
|
|
116
|
+
if (i != j) return i;
|
|
117
|
+
isneg = i < 0;
|
|
118
|
+
if (k != l) return k > l ^ isneg ? 1 : -1;
|
|
119
|
+
j = (k = xc.length) < (l = yc.length) ? k : l;
|
|
120
|
+
for (i = -1; ++i < j;) if (xc[i] != yc[i]) return xc[i] > yc[i] ^ isneg ? 1 : -1;
|
|
121
|
+
return k == l ? 0 : k > l ^ isneg ? 1 : -1;
|
|
122
|
+
};
|
|
123
|
+
P.div = function(y) {
|
|
124
|
+
var x = this, Big$1 = x.constructor, a = x.c, b = (y = new Big$1(y)).c, k = x.s == y.s ? 1 : -1, dp = Big$1.DP;
|
|
125
|
+
if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP);
|
|
126
|
+
if (!b[0]) throw Error(DIV_BY_ZERO);
|
|
127
|
+
if (!a[0]) {
|
|
128
|
+
y.s = k;
|
|
129
|
+
y.c = [y.e = 0];
|
|
130
|
+
return y;
|
|
131
|
+
}
|
|
132
|
+
var bl, bt, n, cmp, ri, bz = b.slice(), ai = bl = b.length, al = a.length, r = a.slice(0, bl), rl = r.length, q = y, qc = q.c = [], qi = 0, p = dp + (q.e = x.e - y.e) + 1;
|
|
133
|
+
q.s = k;
|
|
134
|
+
k = p < 0 ? 0 : p;
|
|
135
|
+
bz.unshift(0);
|
|
136
|
+
for (; rl++ < bl;) r.push(0);
|
|
137
|
+
do {
|
|
138
|
+
for (n = 0; n < 10; n++) {
|
|
139
|
+
if (bl != (rl = r.length)) cmp = bl > rl ? 1 : -1;
|
|
140
|
+
else for (ri = -1, cmp = 0; ++ri < bl;) if (b[ri] != r[ri]) {
|
|
141
|
+
cmp = b[ri] > r[ri] ? 1 : -1;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
if (cmp < 0) {
|
|
145
|
+
for (bt = rl == bl ? b : bz; rl;) {
|
|
146
|
+
if (r[--rl] < bt[rl]) {
|
|
147
|
+
ri = rl;
|
|
148
|
+
for (; ri && !r[--ri];) r[ri] = 9;
|
|
149
|
+
--r[ri];
|
|
150
|
+
r[rl] += 10;
|
|
151
|
+
}
|
|
152
|
+
r[rl] -= bt[rl];
|
|
153
|
+
}
|
|
154
|
+
for (; !r[0];) r.shift();
|
|
155
|
+
} else break;
|
|
156
|
+
}
|
|
157
|
+
qc[qi++] = cmp ? n : ++n;
|
|
158
|
+
if (r[0] && cmp) r[rl] = a[ai] || 0;
|
|
159
|
+
else r = [a[ai]];
|
|
160
|
+
} while ((ai++ < al || r[0] !== UNDEFINED) && k--);
|
|
161
|
+
if (!qc[0] && qi != 1) {
|
|
162
|
+
qc.shift();
|
|
163
|
+
q.e--;
|
|
164
|
+
p--;
|
|
165
|
+
}
|
|
166
|
+
if (qi > p) round(q, p, Big$1.RM, r[0] !== UNDEFINED);
|
|
167
|
+
return q;
|
|
168
|
+
};
|
|
169
|
+
P.eq = function(y) {
|
|
170
|
+
return this.cmp(y) === 0;
|
|
171
|
+
};
|
|
172
|
+
P.gt = function(y) {
|
|
173
|
+
return this.cmp(y) > 0;
|
|
174
|
+
};
|
|
175
|
+
P.gte = function(y) {
|
|
176
|
+
return this.cmp(y) > -1;
|
|
177
|
+
};
|
|
178
|
+
P.lt = function(y) {
|
|
179
|
+
return this.cmp(y) < 0;
|
|
180
|
+
};
|
|
181
|
+
P.lte = function(y) {
|
|
182
|
+
return this.cmp(y) < 1;
|
|
183
|
+
};
|
|
184
|
+
P.minus = P.sub = function(y) {
|
|
185
|
+
var i, j, t, xlty, x = this, Big$1 = x.constructor, a = x.s, b = (y = new Big$1(y)).s;
|
|
186
|
+
if (a != b) {
|
|
187
|
+
y.s = -b;
|
|
188
|
+
return x.plus(y);
|
|
189
|
+
}
|
|
190
|
+
var xc = x.c.slice(), xe = x.e, yc = y.c, ye = y.e;
|
|
191
|
+
if (!xc[0] || !yc[0]) {
|
|
192
|
+
if (yc[0]) y.s = -b;
|
|
193
|
+
else if (xc[0]) y = new Big$1(x);
|
|
194
|
+
else y.s = 1;
|
|
195
|
+
return y;
|
|
196
|
+
}
|
|
197
|
+
if (a = xe - ye) {
|
|
198
|
+
if (xlty = a < 0) {
|
|
199
|
+
a = -a;
|
|
200
|
+
t = xc;
|
|
201
|
+
} else {
|
|
202
|
+
ye = xe;
|
|
203
|
+
t = yc;
|
|
204
|
+
}
|
|
205
|
+
t.reverse();
|
|
206
|
+
for (b = a; b--;) t.push(0);
|
|
207
|
+
t.reverse();
|
|
208
|
+
} else {
|
|
209
|
+
j = ((xlty = xc.length < yc.length) ? xc : yc).length;
|
|
210
|
+
for (a = b = 0; b < j; b++) if (xc[b] != yc[b]) {
|
|
211
|
+
xlty = xc[b] < yc[b];
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (xlty) {
|
|
216
|
+
t = xc;
|
|
217
|
+
xc = yc;
|
|
218
|
+
yc = t;
|
|
219
|
+
y.s = -y.s;
|
|
220
|
+
}
|
|
221
|
+
if ((b = (j = yc.length) - (i = xc.length)) > 0) for (; b--;) xc[i++] = 0;
|
|
222
|
+
for (b = i; j > a;) {
|
|
223
|
+
if (xc[--j] < yc[j]) {
|
|
224
|
+
for (i = j; i && !xc[--i];) xc[i] = 9;
|
|
225
|
+
--xc[i];
|
|
226
|
+
xc[j] += 10;
|
|
227
|
+
}
|
|
228
|
+
xc[j] -= yc[j];
|
|
229
|
+
}
|
|
230
|
+
for (; xc[--b] === 0;) xc.pop();
|
|
231
|
+
for (; xc[0] === 0;) {
|
|
232
|
+
xc.shift();
|
|
233
|
+
--ye;
|
|
234
|
+
}
|
|
235
|
+
if (!xc[0]) {
|
|
236
|
+
y.s = 1;
|
|
237
|
+
xc = [ye = 0];
|
|
238
|
+
}
|
|
239
|
+
y.c = xc;
|
|
240
|
+
y.e = ye;
|
|
241
|
+
return y;
|
|
242
|
+
};
|
|
243
|
+
P.mod = function(y) {
|
|
244
|
+
var ygtx, x = this, Big$1 = x.constructor, a = x.s, b = (y = new Big$1(y)).s;
|
|
245
|
+
if (!y.c[0]) throw Error(DIV_BY_ZERO);
|
|
246
|
+
x.s = y.s = 1;
|
|
247
|
+
ygtx = y.cmp(x) == 1;
|
|
248
|
+
x.s = a;
|
|
249
|
+
y.s = b;
|
|
250
|
+
if (ygtx) return new Big$1(x);
|
|
251
|
+
a = Big$1.DP;
|
|
252
|
+
b = Big$1.RM;
|
|
253
|
+
Big$1.DP = Big$1.RM = 0;
|
|
254
|
+
x = x.div(y);
|
|
255
|
+
Big$1.DP = a;
|
|
256
|
+
Big$1.RM = b;
|
|
257
|
+
return this.minus(x.times(y));
|
|
258
|
+
};
|
|
259
|
+
P.neg = function() {
|
|
260
|
+
var x = new this.constructor(this);
|
|
261
|
+
x.s = -x.s;
|
|
262
|
+
return x;
|
|
263
|
+
};
|
|
264
|
+
P.plus = P.add = function(y) {
|
|
265
|
+
var e, k, t, x = this, Big$1 = x.constructor;
|
|
266
|
+
y = new Big$1(y);
|
|
267
|
+
if (x.s != y.s) {
|
|
268
|
+
y.s = -y.s;
|
|
269
|
+
return x.minus(y);
|
|
270
|
+
}
|
|
271
|
+
var xe = x.e, xc = x.c, ye = y.e, yc = y.c;
|
|
272
|
+
if (!xc[0] || !yc[0]) {
|
|
273
|
+
if (!yc[0]) if (xc[0]) y = new Big$1(x);
|
|
274
|
+
else y.s = x.s;
|
|
275
|
+
return y;
|
|
276
|
+
}
|
|
277
|
+
xc = xc.slice();
|
|
278
|
+
if (e = xe - ye) {
|
|
279
|
+
if (e > 0) {
|
|
280
|
+
ye = xe;
|
|
281
|
+
t = yc;
|
|
282
|
+
} else {
|
|
283
|
+
e = -e;
|
|
284
|
+
t = xc;
|
|
285
|
+
}
|
|
286
|
+
t.reverse();
|
|
287
|
+
for (; e--;) t.push(0);
|
|
288
|
+
t.reverse();
|
|
289
|
+
}
|
|
290
|
+
if (xc.length - yc.length < 0) {
|
|
291
|
+
t = yc;
|
|
292
|
+
yc = xc;
|
|
293
|
+
xc = t;
|
|
294
|
+
}
|
|
295
|
+
e = yc.length;
|
|
296
|
+
for (k = 0; e; xc[e] %= 10) k = (xc[--e] = xc[e] + yc[e] + k) / 10 | 0;
|
|
297
|
+
if (k) {
|
|
298
|
+
xc.unshift(k);
|
|
299
|
+
++ye;
|
|
300
|
+
}
|
|
301
|
+
for (e = xc.length; xc[--e] === 0;) xc.pop();
|
|
302
|
+
y.c = xc;
|
|
303
|
+
y.e = ye;
|
|
304
|
+
return y;
|
|
305
|
+
};
|
|
306
|
+
P.pow = function(n) {
|
|
307
|
+
var x = this, one = new x.constructor("1"), y = one, isneg = n < 0;
|
|
308
|
+
if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) throw Error(INVALID + "exponent");
|
|
309
|
+
if (isneg) n = -n;
|
|
310
|
+
for (;;) {
|
|
311
|
+
if (n & 1) y = y.times(x);
|
|
312
|
+
n >>= 1;
|
|
313
|
+
if (!n) break;
|
|
314
|
+
x = x.times(x);
|
|
315
|
+
}
|
|
316
|
+
return isneg ? one.div(y) : y;
|
|
317
|
+
};
|
|
318
|
+
P.prec = function(sd, rm) {
|
|
319
|
+
if (sd !== ~~sd || sd < 1 || sd > MAX_DP) throw Error(INVALID + "precision");
|
|
320
|
+
return round(new this.constructor(this), sd, rm);
|
|
321
|
+
};
|
|
322
|
+
P.round = function(dp, rm) {
|
|
323
|
+
if (dp === UNDEFINED) dp = 0;
|
|
324
|
+
else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP);
|
|
325
|
+
return round(new this.constructor(this), dp + this.e + 1, rm);
|
|
326
|
+
};
|
|
327
|
+
P.sqrt = function() {
|
|
328
|
+
var r, c, t, x = this, Big$1 = x.constructor, s = x.s, e = x.e, half = new Big$1("0.5");
|
|
329
|
+
if (!x.c[0]) return new Big$1(x);
|
|
330
|
+
if (s < 0) throw Error(NAME + "No square root");
|
|
331
|
+
s = Math.sqrt(+stringify(x, true, true));
|
|
332
|
+
if (s === 0 || s === Infinity) {
|
|
333
|
+
c = x.c.join("");
|
|
334
|
+
if (!(c.length + e & 1)) c += "0";
|
|
335
|
+
s = Math.sqrt(c);
|
|
336
|
+
e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
|
|
337
|
+
r = new Big$1((s == Infinity ? "5e" : (s = s.toExponential()).slice(0, s.indexOf("e") + 1)) + e);
|
|
338
|
+
} else r = new Big$1(s + "");
|
|
339
|
+
e = r.e + (Big$1.DP += 4);
|
|
340
|
+
do {
|
|
341
|
+
t = r;
|
|
342
|
+
r = half.times(t.plus(x.div(t)));
|
|
343
|
+
} while (t.c.slice(0, e).join("") !== r.c.slice(0, e).join(""));
|
|
344
|
+
return round(r, (Big$1.DP -= 4) + r.e + 1, Big$1.RM);
|
|
345
|
+
};
|
|
346
|
+
P.times = P.mul = function(y) {
|
|
347
|
+
var c, x = this, Big$1 = x.constructor, xc = x.c, yc = (y = new Big$1(y)).c, a = xc.length, b = yc.length, i = x.e, j = y.e;
|
|
348
|
+
y.s = x.s == y.s ? 1 : -1;
|
|
349
|
+
if (!xc[0] || !yc[0]) {
|
|
350
|
+
y.c = [y.e = 0];
|
|
351
|
+
return y;
|
|
352
|
+
}
|
|
353
|
+
y.e = i + j;
|
|
354
|
+
if (a < b) {
|
|
355
|
+
c = xc;
|
|
356
|
+
xc = yc;
|
|
357
|
+
yc = c;
|
|
358
|
+
j = a;
|
|
359
|
+
a = b;
|
|
360
|
+
b = j;
|
|
361
|
+
}
|
|
362
|
+
for (c = new Array(j = a + b); j--;) c[j] = 0;
|
|
363
|
+
for (i = b; i--;) {
|
|
364
|
+
b = 0;
|
|
365
|
+
for (j = a + i; j > i;) {
|
|
366
|
+
b = c[j] + yc[i] * xc[j - i - 1] + b;
|
|
367
|
+
c[j--] = b % 10;
|
|
368
|
+
b = b / 10 | 0;
|
|
369
|
+
}
|
|
370
|
+
c[j] = b;
|
|
371
|
+
}
|
|
372
|
+
if (b) ++y.e;
|
|
373
|
+
else c.shift();
|
|
374
|
+
for (i = c.length; !c[--i];) c.pop();
|
|
375
|
+
y.c = c;
|
|
376
|
+
return y;
|
|
377
|
+
};
|
|
378
|
+
P.toExponential = function(dp, rm) {
|
|
379
|
+
var x = this, n = x.c[0];
|
|
380
|
+
if (dp !== UNDEFINED) {
|
|
381
|
+
if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP);
|
|
382
|
+
x = round(new x.constructor(x), ++dp, rm);
|
|
383
|
+
for (; x.c.length < dp;) x.c.push(0);
|
|
384
|
+
}
|
|
385
|
+
return stringify(x, true, !!n);
|
|
386
|
+
};
|
|
387
|
+
P.toFixed = function(dp, rm) {
|
|
388
|
+
var x = this, n = x.c[0];
|
|
389
|
+
if (dp !== UNDEFINED) {
|
|
390
|
+
if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP);
|
|
391
|
+
x = round(new x.constructor(x), dp + x.e + 1, rm);
|
|
392
|
+
for (dp = dp + x.e + 1; x.c.length < dp;) x.c.push(0);
|
|
393
|
+
}
|
|
394
|
+
return stringify(x, false, !!n);
|
|
395
|
+
};
|
|
396
|
+
P.toJSON = P.toString = function() {
|
|
397
|
+
var x = this, Big$1 = x.constructor;
|
|
398
|
+
return stringify(x, x.e <= Big$1.NE || x.e >= Big$1.PE, !!x.c[0]);
|
|
399
|
+
};
|
|
400
|
+
if (typeof Symbol !== "undefined") P[Symbol.for("nodejs.util.inspect.custom")] = P.toJSON;
|
|
401
|
+
P.toNumber = function() {
|
|
402
|
+
var n = +stringify(this, true, true);
|
|
403
|
+
if (this.constructor.strict === true && !this.eq(n.toString())) throw Error(NAME + "Imprecise conversion");
|
|
404
|
+
return n;
|
|
405
|
+
};
|
|
406
|
+
P.toPrecision = function(sd, rm) {
|
|
407
|
+
var x = this, Big$1 = x.constructor, n = x.c[0];
|
|
408
|
+
if (sd !== UNDEFINED) {
|
|
409
|
+
if (sd !== ~~sd || sd < 1 || sd > MAX_DP) throw Error(INVALID + "precision");
|
|
410
|
+
x = round(new Big$1(x), sd, rm);
|
|
411
|
+
for (; x.c.length < sd;) x.c.push(0);
|
|
412
|
+
}
|
|
413
|
+
return stringify(x, sd <= x.e || x.e <= Big$1.NE || x.e >= Big$1.PE, !!n);
|
|
414
|
+
};
|
|
415
|
+
P.valueOf = function() {
|
|
416
|
+
var x = this, Big$1 = x.constructor;
|
|
417
|
+
if (Big$1.strict === true) throw Error(NAME + "valueOf disallowed");
|
|
418
|
+
return stringify(x, x.e <= Big$1.NE || x.e >= Big$1.PE, true);
|
|
419
|
+
};
|
|
420
|
+
var Big = _Big_();
|
|
421
|
+
|
|
422
|
+
//#endregion
|
|
423
|
+
//#region src/candle/CandleBatcher.ts
|
|
424
|
+
var CandleBatcher = class CandleBatcher extends EventEmitter {
|
|
425
|
+
batch = [];
|
|
426
|
+
desiredIntervalInMillis;
|
|
427
|
+
constructor(desiredIntervalInMillis) {
|
|
428
|
+
super();
|
|
429
|
+
this.desiredIntervalInMillis = desiredIntervalInMillis;
|
|
430
|
+
}
|
|
431
|
+
get present() {
|
|
432
|
+
return this.batch.length;
|
|
433
|
+
}
|
|
434
|
+
static amountOfCandles(intervalInMillis, timespanInMillis) {
|
|
435
|
+
const interval = typeof intervalInMillis === "number" ? intervalInMillis : ms$1(intervalInMillis);
|
|
436
|
+
return (typeof timespanInMillis === "number" ? timespanInMillis : ms$1(timespanInMillis)) / interval;
|
|
437
|
+
}
|
|
438
|
+
static isBatchedCandle(candle) {
|
|
439
|
+
return typeof candle.open !== "string";
|
|
440
|
+
}
|
|
441
|
+
static createBatchedCandle(batch, desiredIntervalInMillis) {
|
|
442
|
+
const openTimesArray = batch.map((candle) => candle.openTimeInMillis);
|
|
443
|
+
batch = batch.filter((candle, index) => index === openTimesArray.indexOf(candle.openTimeInMillis));
|
|
444
|
+
const firstCandle = batch[0];
|
|
445
|
+
const lastCandle = batch[batch.length - 1];
|
|
446
|
+
const isPositive = new Big(lastCandle.close).gt(firstCandle.open);
|
|
447
|
+
const accumulator = {
|
|
448
|
+
base: firstCandle.base,
|
|
449
|
+
change: new Big(1).minus(new Big(firstCandle.open).div(lastCandle.close)).times(100),
|
|
450
|
+
close: new Big(lastCandle.close),
|
|
451
|
+
closeAsk: new Big(lastCandle.closeAsk || lastCandle.close),
|
|
452
|
+
counter: firstCandle.counter,
|
|
453
|
+
high: new Big(firstCandle.high),
|
|
454
|
+
highAsk: new Big(firstCandle.highAsk || firstCandle.high),
|
|
455
|
+
isLatest: lastCandle.isLatest,
|
|
456
|
+
isNegative: !isPositive,
|
|
457
|
+
isPositive,
|
|
458
|
+
low: new Big(firstCandle.low),
|
|
459
|
+
lowAsk: new Big(firstCandle.lowAsk || firstCandle.low),
|
|
460
|
+
medianPrice: new Big(0),
|
|
461
|
+
open: new Big(firstCandle.open),
|
|
462
|
+
openAsk: new Big(firstCandle.openAsk || firstCandle.open),
|
|
463
|
+
openTimeInISO: firstCandle.openTimeInISO,
|
|
464
|
+
openTimeInMillis: firstCandle.openTimeInMillis,
|
|
465
|
+
sizeInMillis: desiredIntervalInMillis,
|
|
466
|
+
volume: new Big(0),
|
|
467
|
+
weightedMedianPrice: new Big(0)
|
|
468
|
+
};
|
|
469
|
+
const reducer = (accumulator$1, candle) => {
|
|
470
|
+
const { high, highAsk, low, lowAsk } = accumulator$1;
|
|
471
|
+
candle.lowAsk ??= candle.low;
|
|
472
|
+
candle.highAsk ??= candle.high;
|
|
473
|
+
return {
|
|
474
|
+
base: accumulator$1.base,
|
|
475
|
+
change: accumulator$1.change,
|
|
476
|
+
close: accumulator$1.close,
|
|
477
|
+
closeAsk: accumulator$1.closeAsk,
|
|
478
|
+
counter: accumulator$1.counter,
|
|
479
|
+
high: high.gte(candle.high) ? high : new Big(candle.high),
|
|
480
|
+
highAsk: highAsk.gte(candle.highAsk) ? highAsk : new Big(candle.highAsk),
|
|
481
|
+
isLatest: accumulator$1.isLatest,
|
|
482
|
+
isNegative: accumulator$1.isNegative,
|
|
483
|
+
isPositive: accumulator$1.isPositive,
|
|
484
|
+
low: low.lte(candle.low) ? low : new Big(candle.low),
|
|
485
|
+
lowAsk: lowAsk.lte(candle.lowAsk) ? lowAsk : new Big(candle.lowAsk),
|
|
486
|
+
medianPrice: accumulator$1.medianPrice,
|
|
487
|
+
open: accumulator$1.open,
|
|
488
|
+
openAsk: accumulator$1.openAsk,
|
|
489
|
+
openTimeInISO: accumulator$1.openTimeInISO,
|
|
490
|
+
openTimeInMillis: accumulator$1.openTimeInMillis,
|
|
491
|
+
sizeInMillis: accumulator$1.sizeInMillis,
|
|
492
|
+
volume: accumulator$1.volume.plus(candle.volume),
|
|
493
|
+
weightedMedianPrice: accumulator$1.weightedMedianPrice
|
|
494
|
+
};
|
|
495
|
+
};
|
|
496
|
+
const batchedCandle = batch.reduce(reducer, accumulator);
|
|
497
|
+
const weightedMedianPrice = batchedCandle.volume.eq(0) ? batchedCandle.close : batch.map((candle) => {
|
|
498
|
+
const medianPrice = new Big(candle.low).plus(candle.high).div(2);
|
|
499
|
+
return new Big(candle.volume).mul(medianPrice);
|
|
500
|
+
}).reduce((medianSum1, medianSum2) => medianSum1.plus(medianSum2)).div(batchedCandle.volume);
|
|
501
|
+
batchedCandle.medianPrice = new Big(batchedCandle.high).plus(batchedCandle.low).div(2);
|
|
502
|
+
batchedCandle.weightedMedianPrice = weightedMedianPrice;
|
|
503
|
+
return batchedCandle;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Add candles that should be batched. Once enough candles have been added, the function will return a batched
|
|
507
|
+
* candle.
|
|
508
|
+
*
|
|
509
|
+
* Example: If you aim for a 1-hour batch, and you include 15-minute candles, this function will produce a 1-hour
|
|
510
|
+
* candle once you've added four sets of 15-minute candles.
|
|
511
|
+
*/
|
|
512
|
+
addToBatch(candle) {
|
|
513
|
+
const exchangeCandle = CandleBatcher.isBatchedCandle(candle) ? CandleBatcher.toExchangeCandle(candle) : candle;
|
|
514
|
+
const { currentBatchArray, newBatch } = CandleBatcher.add(exchangeCandle, this.batch, this.desiredIntervalInMillis);
|
|
515
|
+
this.batch = currentBatchArray;
|
|
516
|
+
if (newBatch) this.emit("batchedCandle", newBatch);
|
|
517
|
+
return newBatch;
|
|
518
|
+
}
|
|
519
|
+
static batchMany(candles, desiredIntervalInMillis) {
|
|
520
|
+
if (candles.length === 0) return [];
|
|
521
|
+
const batchesArray = [];
|
|
522
|
+
let currentBatchArray = [];
|
|
523
|
+
for (const candle of candles) {
|
|
524
|
+
const addCandle = CandleBatcher.add(candle, currentBatchArray, desiredIntervalInMillis);
|
|
525
|
+
if (addCandle.newBatch !== void 0) batchesArray.push(addCandle.newBatch);
|
|
526
|
+
currentBatchArray = addCandle.currentBatchArray;
|
|
527
|
+
}
|
|
528
|
+
if (currentBatchArray.length !== 0) batchesArray.push(CandleBatcher.createBatchedCandle(currentBatchArray, desiredIntervalInMillis));
|
|
529
|
+
return batchesArray;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Create a new candle which starts at the beginning of the desired interval (even when beginning candles have been
|
|
533
|
+
* missed). Because of this fix, we won't get a shifted chart when candles are missing within an interval.
|
|
534
|
+
*/
|
|
535
|
+
static adjustCandle(candle, desiredIntervalInMillis) {
|
|
536
|
+
const newCandle = { ...candle };
|
|
537
|
+
newCandle.openTimeInMillis = candle.openTimeInMillis - candle.openTimeInMillis % desiredIntervalInMillis;
|
|
538
|
+
newCandle.openTimeInISO = new Date(newCandle.openTimeInMillis).toISOString();
|
|
539
|
+
return newCandle;
|
|
540
|
+
}
|
|
541
|
+
static add(candle, currentBatchArray, desiredIntervalInMillis) {
|
|
542
|
+
if (new Big(candle.volume).eq(0)) return {
|
|
543
|
+
currentBatchArray,
|
|
544
|
+
newBatch: void 0
|
|
545
|
+
};
|
|
546
|
+
if (currentBatchArray.length === 0) {
|
|
547
|
+
const adjustedCandle = CandleBatcher.adjustCandle(candle, desiredIntervalInMillis);
|
|
548
|
+
currentBatchArray.push(adjustedCandle);
|
|
549
|
+
if (adjustedCandle.sizeInMillis === desiredIntervalInMillis) return {
|
|
550
|
+
currentBatchArray: [],
|
|
551
|
+
newBatch: CandleBatcher.createBatchedCandle(currentBatchArray, desiredIntervalInMillis)
|
|
552
|
+
};
|
|
553
|
+
} else {
|
|
554
|
+
const firstCandle = currentBatchArray[0];
|
|
555
|
+
const isNextInterval = candle.openTimeInMillis + candle.sizeInMillis - firstCandle.openTimeInMillis > desiredIntervalInMillis;
|
|
556
|
+
const isEndOfCurrentInterval = candle.openTimeInMillis + candle.sizeInMillis - firstCandle.openTimeInMillis === desiredIntervalInMillis;
|
|
557
|
+
if (isNextInterval || isEndOfCurrentInterval) {
|
|
558
|
+
if (isEndOfCurrentInterval) currentBatchArray.push(candle);
|
|
559
|
+
const newBatch = CandleBatcher.createBatchedCandle(currentBatchArray, desiredIntervalInMillis);
|
|
560
|
+
if (isEndOfCurrentInterval) currentBatchArray = [];
|
|
561
|
+
else currentBatchArray = [CandleBatcher.adjustCandle(candle, desiredIntervalInMillis)];
|
|
562
|
+
return {
|
|
563
|
+
currentBatchArray,
|
|
564
|
+
newBatch
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
currentBatchArray.push(candle);
|
|
568
|
+
}
|
|
569
|
+
return {
|
|
570
|
+
currentBatchArray,
|
|
571
|
+
newBatch: void 0
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
static toBatchedCandles(candles) {
|
|
575
|
+
return CandleBatcher.batchMany(candles, candles[0].sizeInMillis);
|
|
576
|
+
}
|
|
577
|
+
static toBatchedCandle(candle) {
|
|
578
|
+
return CandleBatcher.batchMany([candle], candle.sizeInMillis)[0];
|
|
579
|
+
}
|
|
580
|
+
static toExchangeCandles(candles) {
|
|
581
|
+
return candles.map(CandleBatcher.toExchangeCandle);
|
|
582
|
+
}
|
|
583
|
+
static toExchangeCandle(candle) {
|
|
584
|
+
return {
|
|
585
|
+
base: candle.base,
|
|
586
|
+
close: candle.close.valueOf(),
|
|
587
|
+
counter: candle.counter,
|
|
588
|
+
high: candle.high.valueOf(),
|
|
589
|
+
low: candle.low.valueOf(),
|
|
590
|
+
open: candle.open.valueOf(),
|
|
591
|
+
openTimeInISO: candle.openTimeInISO,
|
|
592
|
+
openTimeInMillis: candle.openTimeInMillis,
|
|
593
|
+
sizeInMillis: candle.sizeInMillis,
|
|
594
|
+
volume: candle.volume.valueOf()
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
static toString(candle) {
|
|
598
|
+
return JSON.stringify(jsonabc.sortObj({
|
|
599
|
+
base: candle.base,
|
|
600
|
+
close: candle.close.valueOf(),
|
|
601
|
+
counter: candle.counter,
|
|
602
|
+
high: candle.high.valueOf(),
|
|
603
|
+
low: candle.low.valueOf(),
|
|
604
|
+
medianPrice: candle.medianPrice.valueOf(),
|
|
605
|
+
open: candle.open.valueOf(),
|
|
606
|
+
openTimeInISO: candle.openTimeInISO,
|
|
607
|
+
openTimeInMillis: candle.openTimeInMillis,
|
|
608
|
+
sizeInMillis: candle.sizeInMillis,
|
|
609
|
+
volume: candle.volume.valueOf(),
|
|
610
|
+
weightedMedianPrice: candle.weightedMedianPrice.valueOf()
|
|
611
|
+
}), null, 2);
|
|
612
|
+
}
|
|
613
|
+
static getHighAndLow(candles) {
|
|
614
|
+
const reducer = (accumulator, candle) => {
|
|
615
|
+
const { high, low } = accumulator;
|
|
616
|
+
return {
|
|
617
|
+
high: high.gte(candle.high) ? high : new Big(candle.high),
|
|
618
|
+
low: low.lte(candle.low) ? low : new Big(candle.low)
|
|
619
|
+
};
|
|
620
|
+
};
|
|
621
|
+
const [firstCandle] = candles;
|
|
622
|
+
return candles.reduce(reducer, {
|
|
623
|
+
high: new Big(firstCandle.high),
|
|
624
|
+
low: new Big(firstCandle.low)
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
//#endregion
|
|
630
|
+
//#region src/core/CurrencyPair.ts
|
|
631
|
+
/**
|
|
632
|
+
* A currency pair symbolizes an asset and the currency that you are trading the asset with.
|
|
633
|
+
*
|
|
634
|
+
* Examples:
|
|
635
|
+
* Base: TSLA, Counter: EUR
|
|
636
|
+
* Base: BTC, Counter: USDT
|
|
637
|
+
*/
|
|
638
|
+
var CurrencyPair = class CurrencyPair {
|
|
639
|
+
/**
|
|
640
|
+
* Constructs a currency pair.
|
|
641
|
+
*
|
|
642
|
+
* @param base - Symbol of the base asset (i.e. TSLA)
|
|
643
|
+
* @param counter - Symbol of the counter asset (i.e. EUR)
|
|
644
|
+
*/
|
|
645
|
+
constructor(base, counter) {
|
|
646
|
+
this.base = base;
|
|
647
|
+
this.counter = counter;
|
|
648
|
+
}
|
|
649
|
+
isEqual(other) {
|
|
650
|
+
return this.base === other.base && this.counter === other.counter;
|
|
651
|
+
}
|
|
652
|
+
asString(delimiter) {
|
|
653
|
+
return `${this.base}${delimiter}${this.counter}`;
|
|
654
|
+
}
|
|
655
|
+
static fromString(symbol, delimiter) {
|
|
656
|
+
const [base, counter] = symbol.split(delimiter);
|
|
657
|
+
if (base && counter) return new CurrencyPair(base, counter);
|
|
658
|
+
throw new Error(`Symbol "${symbol}" cannot be split with delimiter "${delimiter}".`);
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
//#endregion
|
|
663
|
+
//#region src/core/Exchange.ts
|
|
664
|
+
let ExchangeOrderSide = /* @__PURE__ */ function(ExchangeOrderSide$1) {
|
|
665
|
+
ExchangeOrderSide$1["BUY"] = "BUY";
|
|
666
|
+
ExchangeOrderSide$1["SELL"] = "SELL";
|
|
667
|
+
return ExchangeOrderSide$1;
|
|
668
|
+
}({});
|
|
669
|
+
const ExchangeCandleBaseSchema = z.object({
|
|
670
|
+
base: z.string(),
|
|
671
|
+
counter: z.string(),
|
|
672
|
+
isLatest: z.boolean().optional(),
|
|
673
|
+
openTimeInISO: z.string(),
|
|
674
|
+
openTimeInMillis: z.number(),
|
|
675
|
+
sizeInMillis: z.number()
|
|
676
|
+
});
|
|
677
|
+
let ExchangeOrderType = /* @__PURE__ */ function(ExchangeOrderType$1) {
|
|
678
|
+
/** Maker */
|
|
679
|
+
ExchangeOrderType$1["LIMIT"] = "LIMIT";
|
|
680
|
+
/** Taker */
|
|
681
|
+
ExchangeOrderType$1["MARKET"] = "MARKET";
|
|
682
|
+
return ExchangeOrderType$1;
|
|
683
|
+
}({});
|
|
684
|
+
const ExchangeCandleSchema = z.object({
|
|
685
|
+
close: z.string(),
|
|
686
|
+
closeAsk: z.string().optional(),
|
|
687
|
+
high: z.string(),
|
|
688
|
+
highAsk: z.string().optional(),
|
|
689
|
+
low: z.string(),
|
|
690
|
+
lowAsk: z.string().optional(),
|
|
691
|
+
open: z.string(),
|
|
692
|
+
openAsk: z.string().optional(),
|
|
693
|
+
volume: z.string()
|
|
694
|
+
}).merge(ExchangeCandleBaseSchema);
|
|
695
|
+
let ExchangeOrderPosition = /* @__PURE__ */ function(ExchangeOrderPosition$1) {
|
|
696
|
+
ExchangeOrderPosition$1["LONG"] = "LONG";
|
|
697
|
+
ExchangeOrderPosition$1["SHORT"] = "SHORT";
|
|
698
|
+
return ExchangeOrderPosition$1;
|
|
699
|
+
}({});
|
|
700
|
+
var Exchange = class {
|
|
701
|
+
constructor(loggerName) {
|
|
702
|
+
this.loggerName = loggerName;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Get the most recent candle with data by searching backwards in time.
|
|
706
|
+
* Useful when markets are closed or data hasn't been published yet for the latest interval.
|
|
707
|
+
*/
|
|
708
|
+
async getLatestAvailableCandle(pair, intervalInMillis) {
|
|
709
|
+
let endTime = Math.floor(Date.now() / intervalInMillis) * intervalInMillis;
|
|
710
|
+
let startTime = endTime - intervalInMillis;
|
|
711
|
+
let candles = await this.getCandles(pair, {
|
|
712
|
+
intervalInMillis,
|
|
713
|
+
startTimeFirstCandle: new Date(startTime).toISOString(),
|
|
714
|
+
startTimeLastCandle: new Date(endTime).toISOString()
|
|
715
|
+
});
|
|
716
|
+
while (candles.length === 0) {
|
|
717
|
+
endTime = startTime;
|
|
718
|
+
startTime -= intervalInMillis;
|
|
719
|
+
candles = await this.getCandles(pair, {
|
|
720
|
+
intervalInMillis,
|
|
721
|
+
startTimeFirstCandle: new Date(startTime).toISOString(),
|
|
722
|
+
startTimeLastCandle: new Date(endTime).toISOString()
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
return candles[0];
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
//#endregion
|
|
730
|
+
//#region src/exchange-alpaca/AlpacaExchangeMapper.ts
|
|
731
|
+
var AlpacaExchangeMapper = class {
|
|
732
|
+
static mapInterval(intervalInMillis) {
|
|
733
|
+
if (intervalInMillis < ms$1("1m")) throw new Error(`Timeframes below 1 minute are not supported.`);
|
|
734
|
+
if (intervalInMillis > ms$1("1d")) throw new Error(`Timeframes above 1 day are not supported.`);
|
|
735
|
+
return ms$1(intervalInMillis).replace("m", "Min").replace("h", "Hour").replace("d", "Day");
|
|
736
|
+
}
|
|
737
|
+
static toExchangeCandle(candle, pair, sizeInMillis) {
|
|
738
|
+
const date = new Date(candle.t);
|
|
739
|
+
return {
|
|
740
|
+
base: pair.base,
|
|
741
|
+
close: candle.c + "",
|
|
742
|
+
counter: pair.counter,
|
|
743
|
+
high: candle.h + "",
|
|
744
|
+
low: candle.l + "",
|
|
745
|
+
open: candle.o + "",
|
|
746
|
+
openTimeInISO: date.toISOString(),
|
|
747
|
+
openTimeInMillis: date.getTime(),
|
|
748
|
+
sizeInMillis,
|
|
749
|
+
volume: candle.v + ""
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
static toExchangePendingOrder(order, pair, options) {
|
|
753
|
+
if (order.type === "market") return {
|
|
754
|
+
id: order.id,
|
|
755
|
+
pair,
|
|
756
|
+
side: options.side,
|
|
757
|
+
size: order.notional ? `${order.notional}` : `${order.qty}`,
|
|
758
|
+
type: ExchangeOrderType.MARKET
|
|
759
|
+
};
|
|
760
|
+
return {
|
|
761
|
+
id: order.id,
|
|
762
|
+
pair,
|
|
763
|
+
price: `${order.limit_price}`,
|
|
764
|
+
side: options.side,
|
|
765
|
+
size: `${order.qty}`,
|
|
766
|
+
type: ExchangeOrderType.LIMIT
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
static toFilledOrder(order, pair) {
|
|
770
|
+
if (order.status !== OrderStatus.FILLED) throw new Error(`Order ID "${order.id}" is not filled.`);
|
|
771
|
+
return {
|
|
772
|
+
created_at: `${order.created_at}`,
|
|
773
|
+
fee: "0",
|
|
774
|
+
feeAsset: pair.counter,
|
|
775
|
+
order_id: `${order.id}`,
|
|
776
|
+
pair,
|
|
777
|
+
position: ExchangeOrderPosition.LONG,
|
|
778
|
+
price: `${order.filled_avg_price}`,
|
|
779
|
+
side: order.side === "buy" ? ExchangeOrderSide.BUY : ExchangeOrderSide.SELL,
|
|
780
|
+
size: `${order.filled_qty}`
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
//#endregion
|
|
786
|
+
//#region src/exchange-alpaca/AlpacaExchange.ts
|
|
787
|
+
const ms$2 = (timeString) => {
|
|
788
|
+
const units = {
|
|
789
|
+
s: 1e3,
|
|
790
|
+
m: 6e4
|
|
791
|
+
};
|
|
792
|
+
const match = timeString.match(/^(\d+)([sm])$/);
|
|
793
|
+
if (!match) throw new Error(`Invalid time string: ${timeString}`);
|
|
794
|
+
return parseInt(match[1]) * units[match[2]];
|
|
795
|
+
};
|
|
796
|
+
const hasErrorCode = (error) => {
|
|
797
|
+
return !!error && typeof error === "object" && "code" in error;
|
|
798
|
+
};
|
|
799
|
+
const hasErrorStatus = (error) => {
|
|
800
|
+
return !!error && typeof error === "object" && "status" in error && typeof error.status === "number";
|
|
801
|
+
};
|
|
802
|
+
var AlpacaExchange = class AlpacaExchange extends Exchange {
|
|
803
|
+
stream = void 0;
|
|
804
|
+
client;
|
|
805
|
+
SUBSCRIPTION_PLAN = "iex";
|
|
806
|
+
retryConfig = {
|
|
807
|
+
delay: ms$2("10s"),
|
|
808
|
+
retries: "INFINITELY",
|
|
809
|
+
retryIf: (error) => {
|
|
810
|
+
if (hasErrorCode(error)) switch (error.code) {
|
|
811
|
+
case 40310100: return false;
|
|
812
|
+
default: return true;
|
|
813
|
+
}
|
|
814
|
+
else if (hasErrorStatus(error)) {
|
|
815
|
+
if (error.status === 429) return true;
|
|
816
|
+
}
|
|
817
|
+
return false;
|
|
818
|
+
},
|
|
819
|
+
timeout: ms$2("5m")
|
|
820
|
+
};
|
|
821
|
+
constructor(options) {
|
|
822
|
+
super(AlpacaExchange.NAME);
|
|
823
|
+
this.client = new Client({
|
|
824
|
+
credentials: {
|
|
825
|
+
key: options.apiKey,
|
|
826
|
+
secret: options.apiSecret
|
|
827
|
+
},
|
|
828
|
+
paper: options.usePaperTrading
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
static NAME = "Alpaca";
|
|
832
|
+
createSymbol(pair, isCrypto) {
|
|
833
|
+
if (isCrypto ?? pair.counter !== "USD") return `${pair.base}/${pair.counter}`;
|
|
834
|
+
return pair.base;
|
|
835
|
+
}
|
|
836
|
+
async fetchLatestCryptoBars(pair) {
|
|
837
|
+
return retry(() => this.client.v1beta3.getCryptoBarsLatest({ symbols: this.createSymbol(pair, true) }), this.retryConfig);
|
|
838
|
+
}
|
|
839
|
+
async isCryptoSymbol(pair) {
|
|
840
|
+
try {
|
|
841
|
+
const response = await this.fetchLatestCryptoBars(pair);
|
|
842
|
+
return Object.keys(response.bars).length > 0;
|
|
843
|
+
} catch (error) {
|
|
844
|
+
if (error instanceof ApiError && error.status === 400) return false;
|
|
845
|
+
throw error;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
async fetchCryptoBars(pair, request, pageToken) {
|
|
849
|
+
return retry(() => this.client.v1beta3.getCryptoBars({
|
|
850
|
+
end: request.startTimeLastCandle,
|
|
851
|
+
limit: 1e4,
|
|
852
|
+
pageToken,
|
|
853
|
+
start: request.startTimeFirstCandle,
|
|
854
|
+
symbols: this.createSymbol(pair, true),
|
|
855
|
+
timeframe: AlpacaExchangeMapper.mapInterval(request.intervalInMillis)
|
|
856
|
+
}), this.retryConfig);
|
|
857
|
+
}
|
|
858
|
+
fetchStockBars(pair, request, pageToken) {
|
|
859
|
+
if (pair.counter !== "USD") throw new Error(`Cannot use "${pair.counter}". Stock "${pair.base}" can only be traded in USD on ${AlpacaExchange.NAME}.`);
|
|
860
|
+
return retry(() => this.client.v2.getStockBars({
|
|
861
|
+
end: request.startTimeLastCandle,
|
|
862
|
+
feed: this.SUBSCRIPTION_PLAN,
|
|
863
|
+
limit: 1e4,
|
|
864
|
+
pageToken,
|
|
865
|
+
start: request.startTimeFirstCandle,
|
|
866
|
+
symbols: this.createSymbol(pair, false),
|
|
867
|
+
timeframe: AlpacaExchangeMapper.mapInterval(request.intervalInMillis)
|
|
868
|
+
}), this.retryConfig);
|
|
869
|
+
}
|
|
870
|
+
async getCandles(pair, request) {
|
|
871
|
+
const candles = [];
|
|
872
|
+
let pageToken = void 0;
|
|
873
|
+
const fetchBars = await this.isCryptoSymbol(pair) ? this.fetchCryptoBars.bind(this) : this.fetchStockBars.bind(this);
|
|
874
|
+
do {
|
|
875
|
+
const { bars, next_page_token } = await fetchBars(pair, request, pageToken);
|
|
876
|
+
const entries = Object.values(bars);
|
|
877
|
+
for (const bars$1 of entries) bars$1.forEach((bar) => {
|
|
878
|
+
const candle = AlpacaExchangeMapper.toExchangeCandle(bar, pair, request.intervalInMillis);
|
|
879
|
+
candles.push(candle);
|
|
880
|
+
});
|
|
881
|
+
pageToken = next_page_token;
|
|
882
|
+
} while (pageToken);
|
|
883
|
+
return candles;
|
|
884
|
+
}
|
|
885
|
+
getName() {
|
|
886
|
+
return AlpacaExchange.NAME;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Original time format from Alpaca is "RFC 3339" (i.e. "2023-08-08T18:58:27.26720022-04:00"),
|
|
890
|
+
* we convert it to "ISO 8601 UTC" (i.e. "2023-08-08T22:58:27.267Z").
|
|
891
|
+
*/
|
|
892
|
+
async getTime() {
|
|
893
|
+
const clock = await retry(() => this.client.v2.getClock(), this.retryConfig);
|
|
894
|
+
return new Date(clock.timestamp).toISOString();
|
|
895
|
+
}
|
|
896
|
+
disconnect() {
|
|
897
|
+
this.stream?.getConnection().close();
|
|
898
|
+
}
|
|
899
|
+
};
|
|
900
|
+
|
|
901
|
+
//#endregion
|
|
902
|
+
//#region src/exchange-alpaca/getAlpacaClient.ts
|
|
903
|
+
function getAlpacaClient({ apiKey, apiSecret, usePaperTrading }) {
|
|
904
|
+
process.on("SIGINT", () => {
|
|
905
|
+
console.log("Received signal interrupt...");
|
|
906
|
+
exchange.disconnect();
|
|
907
|
+
console.log(`Sent WebSocket disconnect.`);
|
|
908
|
+
process.exit(0);
|
|
909
|
+
});
|
|
910
|
+
const exchange = new AlpacaExchange({
|
|
911
|
+
apiKey,
|
|
912
|
+
apiSecret,
|
|
913
|
+
usePaperTrading
|
|
914
|
+
});
|
|
915
|
+
return exchange;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
//#endregion
|
|
919
|
+
export { AlpacaExchange, AlpacaExchangeMapper, BasicCandlePrice, CandleBatcher, CurrencyPair, Exchange, ExchangeCandleBaseSchema, ExchangeCandleSchema, ExchangeOrderPosition, ExchangeOrderSide, ExchangeOrderType, getAlpacaClient, hasErrorCode, hasErrorStatus, ms };
|