@gera2ld/lib-trading 0.0.1 → 0.0.3
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/dist/index.js +134 -168
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,180 +1,146 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import e from "bignumber.js";
|
|
2
|
+
//#region src/common.ts
|
|
3
|
+
function t(t, n, r) {
|
|
4
|
+
let i = new e((r?.unitSize ?? 1) * n).abs();
|
|
5
|
+
return i = i[r?.marketType === "future-c" ? "div" : "times"](t), i.toNumber();
|
|
6
6
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
deltaA: n.toNumber(),
|
|
21
|
-
deltaB: c.toNumber()
|
|
22
|
-
};
|
|
7
|
+
function n(n, r) {
|
|
8
|
+
let a = Math.sign(n.quantity);
|
|
9
|
+
r?.marketType === "future-c" && r?.positionSide === "short" && (a = -a);
|
|
10
|
+
let o = new e(n.quantity).abs().times(a), s = new e(-a * t(n.price, n.quantity, r)), c, l;
|
|
11
|
+
r?.marketType === "future-c" ? (c = s, l = o) : (c = o, l = s);
|
|
12
|
+
let { feeMode: u, feeRate: d } = i({
|
|
13
|
+
...r,
|
|
14
|
+
...n
|
|
15
|
+
});
|
|
16
|
+
return u === "A" || u === "buy" && a > 0 ? c = c.minus(c.abs().times(d)) : (u === "B" || u === "buy" && a < 0) && (l = l.minus(l.abs().times(d))), {
|
|
17
|
+
deltaA: c.toNumber(),
|
|
18
|
+
deltaB: l.toNumber()
|
|
19
|
+
};
|
|
23
20
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
totalAsB: l
|
|
37
|
-
};
|
|
21
|
+
function r(n, r) {
|
|
22
|
+
let i = r?.unitSize ?? 1, a = 0, o = 0;
|
|
23
|
+
if (n.price) {
|
|
24
|
+
let s = r?.positionSide === "short" ? -1 : 1, c, l = new e(n.balanceB);
|
|
25
|
+
r?.marketType === "future-c" ? (l = l.plus(n.balanceA * n.price / i), c = l.times(i).div(n.price)) : (l = l.plus(s * t(n.price, n.balanceA, r)), c = l.div(i).div(n.price).times(s)), a = c.toNumber(), o = l.toNumber();
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
balanceA: n.balanceA,
|
|
29
|
+
balanceB: n.balanceB,
|
|
30
|
+
totalAsA: a,
|
|
31
|
+
totalAsB: o
|
|
32
|
+
};
|
|
38
33
|
}
|
|
39
|
-
function
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
function i(e) {
|
|
35
|
+
return {
|
|
36
|
+
feeMode: e?.feeMode ?? "buy",
|
|
37
|
+
feeRate: e?.feeRate ?? 0
|
|
38
|
+
};
|
|
44
39
|
}
|
|
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
|
-
for (; d < i.totalCount; ) {
|
|
88
|
-
const m = +f.toFixed(c);
|
|
89
|
-
f = f.times(A);
|
|
90
|
-
const p = +f.toFixed(c), g = {
|
|
91
|
-
index: d,
|
|
92
|
-
price: m,
|
|
93
|
-
sellPrice: p
|
|
94
|
-
};
|
|
95
|
-
h.push(g), d += 1;
|
|
96
|
-
}
|
|
97
|
-
return h;
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/grid.ts
|
|
42
|
+
function a(t) {
|
|
43
|
+
let { startPrice: n, stopPrice: r, minDifference: i, minRatio: a, maxCount: o, precision: s } = t;
|
|
44
|
+
if (!i && a === 1 && !o) throw Error("Invalid grid config");
|
|
45
|
+
let c = {
|
|
46
|
+
totalCount: 0,
|
|
47
|
+
arithmeticCount: 0,
|
|
48
|
+
geometricStartPrice: new e(n)
|
|
49
|
+
}, l = new e(n), u = !i;
|
|
50
|
+
for (; o && c.totalCount < o;) {
|
|
51
|
+
let e = l.times(a);
|
|
52
|
+
if (!u) {
|
|
53
|
+
let t = l.plus(i);
|
|
54
|
+
t.lte(e) ? (u = !0, c.geometricStartPrice = l) : e = t;
|
|
55
|
+
}
|
|
56
|
+
if (e.gt(r)) break;
|
|
57
|
+
u || (c.arithmeticCount += 1), c.totalCount += 1, l = e;
|
|
58
|
+
}
|
|
59
|
+
u ? l.isEqualTo(n) || (c.geometricStartPrice = c.geometricStartPrice.minus(n).div(l.minus(n)).times(r - n).plus(n)) : c.geometricStartPrice = new e(r);
|
|
60
|
+
let d = [], f = c.arithmeticCount ? c.geometricStartPrice.minus(n).div(c.arithmeticCount) : 0, p = c.totalCount > c.arithmeticCount ? (r / c.geometricStartPrice.toNumber()) ** (1 / (c.totalCount - c.arithmeticCount)) : 0, m = 0, h = new e(n);
|
|
61
|
+
for (; m < c.arithmeticCount;) {
|
|
62
|
+
let e = +h.toFixed(s);
|
|
63
|
+
h = h.plus(f);
|
|
64
|
+
let t = +h.toFixed(s), n = {
|
|
65
|
+
index: m,
|
|
66
|
+
price: e,
|
|
67
|
+
sellPrice: t
|
|
68
|
+
};
|
|
69
|
+
d.push(n), m += 1;
|
|
70
|
+
}
|
|
71
|
+
for (; m < c.totalCount;) {
|
|
72
|
+
let e = +h.toFixed(s);
|
|
73
|
+
h = h.times(p);
|
|
74
|
+
let t = +h.toFixed(s), n = {
|
|
75
|
+
index: m,
|
|
76
|
+
price: e,
|
|
77
|
+
sellPrice: t
|
|
78
|
+
};
|
|
79
|
+
d.push(n), m += 1;
|
|
80
|
+
}
|
|
81
|
+
return d;
|
|
98
82
|
}
|
|
99
|
-
function
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
n - l
|
|
116
|
-
), s = Math.min(s, n + l)), { startIndex: c, stopIndex: i, actualStartIndex: u, actualStopIndex: s };
|
|
83
|
+
function o(e, t) {
|
|
84
|
+
let { enabledStartPrice: n, enabledStopPrice: r, placesPerSide: i, firstSellIndex: a } = {
|
|
85
|
+
enabledStartPrice: 0,
|
|
86
|
+
enabledStopPrice: e.length,
|
|
87
|
+
firstSellIndex: -1,
|
|
88
|
+
...t
|
|
89
|
+
}, o = 0, s = e.length;
|
|
90
|
+
for (; o < e.length && e[o].price < n;) o += 1;
|
|
91
|
+
if (r) for (; s >= 0 && e[s].sellPrice > r;) --s;
|
|
92
|
+
let c = o, l = s;
|
|
93
|
+
return a >= 0 && i && (c = Math.max(c, a - i), l = Math.min(l, a + i)), {
|
|
94
|
+
startIndex: o,
|
|
95
|
+
stopIndex: s,
|
|
96
|
+
actualStartIndex: c,
|
|
97
|
+
actualStopIndex: l
|
|
98
|
+
};
|
|
117
99
|
}
|
|
118
|
-
function
|
|
119
|
-
|
|
120
|
-
|
|
100
|
+
function s(e, t) {
|
|
101
|
+
let n = e.findIndex((e) => e.sellPrice >= t);
|
|
102
|
+
return n < 0 && (n = e.length), n;
|
|
121
103
|
}
|
|
122
|
-
function
|
|
123
|
-
|
|
104
|
+
function c(e, t, n) {
|
|
105
|
+
return t / e * (1 - n) - 1 / (1 - n);
|
|
124
106
|
}
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
107
|
+
function l(t, r, i) {
|
|
108
|
+
let a = t ? n(t, i) : {
|
|
109
|
+
deltaA: 0,
|
|
110
|
+
deltaB: 0
|
|
111
|
+
}, o = r ? n(r, i) : {
|
|
112
|
+
deltaA: 0,
|
|
113
|
+
deltaB: 0
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
profitA: new e(a.deltaA).plus(o.deltaA).toNumber(),
|
|
117
|
+
profitB: new e(a.deltaB).plus(o.deltaB).toNumber()
|
|
118
|
+
};
|
|
131
119
|
}
|
|
132
|
-
function
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
t.precisionA,
|
|
136
|
-
a.ROUND_UP
|
|
137
|
-
)), e;
|
|
120
|
+
function u(t, n) {
|
|
121
|
+
let { feeMode: r, feeRate: a } = i(n);
|
|
122
|
+
return n.marketType !== "future-c" && ["buy", "A"].includes(r) && (t /= 1 - a), n.precisionA && (t = +new e(t).toFixed(n.precisionA, e.ROUND_UP)), t;
|
|
138
123
|
}
|
|
139
|
-
function
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
t.precisionA,
|
|
143
|
-
a.ROUND_DOWN
|
|
144
|
-
)), e;
|
|
124
|
+
function d(t, n) {
|
|
125
|
+
let { feeMode: r, feeRate: a } = i(n);
|
|
126
|
+
return n.marketType !== "future-c" && r === "A" && (t /= 1 + a), n.precisionA && (t = +new e(t).toFixed(n.precisionA, e.ROUND_DOWN)), t;
|
|
145
127
|
}
|
|
146
|
-
function
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
throw new Error(`Unmatched order: ${JSON.stringify(c)}`);
|
|
163
|
-
l[n] = c;
|
|
164
|
-
}
|
|
165
|
-
return l;
|
|
128
|
+
function f(e, t, n, r) {
|
|
129
|
+
let i = [];
|
|
130
|
+
n.sort((e, t) => e.price - t.price), r.sort((e, t) => t.price - e.price);
|
|
131
|
+
let a = 0;
|
|
132
|
+
for (let r of n) {
|
|
133
|
+
for (; a < e.length && (e[a].price < r.price || i[a]);) a += 1;
|
|
134
|
+
if (a >= e.length || a >= t) throw Error(`Unmatched order: ${JSON.stringify(r)}`);
|
|
135
|
+
i[a] = r;
|
|
136
|
+
}
|
|
137
|
+
a = e.length - 1;
|
|
138
|
+
for (let n of r) {
|
|
139
|
+
for (; a >= 0 && (e[a].sellPrice > n.price || i[a]);) --a;
|
|
140
|
+
if (a < 0 || a < t) throw Error(`Unmatched order: ${JSON.stringify(n)}`);
|
|
141
|
+
i[a] = n;
|
|
142
|
+
}
|
|
143
|
+
return i;
|
|
166
144
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
w as calcOrderDelta,
|
|
170
|
-
C as createGrids,
|
|
171
|
-
M as getBuyQuantity,
|
|
172
|
-
y as getFirstSellIndex,
|
|
173
|
-
F as getGridProfit,
|
|
174
|
-
N as getGridRange,
|
|
175
|
-
S as getOrderCost,
|
|
176
|
-
R as getProfitRate,
|
|
177
|
-
I as getSellQuantity,
|
|
178
|
-
O as matchOrders,
|
|
179
|
-
P as normalizeFeeOptions
|
|
180
|
-
};
|
|
145
|
+
//#endregion
|
|
146
|
+
export { r as calcBalance, n as calcOrderDelta, a as createGrids, u as getBuyQuantity, s as getFirstSellIndex, l as getGridProfit, o as getGridRange, t as getOrderCost, c as getProfitRate, d as getSellQuantity, f as matchOrders, i as normalizeFeeOptions };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gera2ld/lib-trading",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"registry": "https://registry.npmjs.org/"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"bignumber.js": "^
|
|
19
|
+
"bignumber.js": "^11.0.0"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"clean": "del-cli dist tsconfig.tsbuildinfo",
|