@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.
Files changed (2) hide show
  1. package/dist/index.js +134 -168
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,180 +1,146 @@
1
- import a from "bignumber.js";
2
- function S(e, t, r) {
3
- const o = r?.unitSize ?? 1;
4
- let l = new a(o * t).abs();
5
- return l = l[r?.marketType === "future-c" ? "div" : "times"](e), l.toNumber();
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 w(e, t) {
8
- let r = Math.sign(e.quantity);
9
- t?.marketType === "future-c" && t?.positionSide === "short" && (r = -r);
10
- const o = new a(e.quantity).abs().times(r), l = new a(
11
- -r * S(e.price, e.quantity, t)
12
- );
13
- let n, c;
14
- t?.marketType === "future-c" ? (n = l, c = o) : (n = o, c = l);
15
- const { feeMode: i, feeRate: u } = P({
16
- ...t,
17
- ...e
18
- });
19
- return i === "A" || i === "buy" && r > 0 ? n = n.minus(n.abs().times(u)) : (i === "B" || i === "buy" && r < 0) && (c = c.minus(c.abs().times(u))), {
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 B(e, t) {
25
- const r = t?.unitSize ?? 1;
26
- let o = 0, l = 0;
27
- if (e.price) {
28
- const n = t?.positionSide === "short" ? -1 : 1;
29
- let c, i = new a(e.balanceB);
30
- t?.marketType === "future-c" ? (i = i.plus(e.balanceA * e.price / r), c = i.times(r).div(e.price)) : (i = i.plus(n * S(e.price, e.balanceA, t)), c = i.div(r).div(e.price).times(n)), o = c.toNumber(), l = i.toNumber();
31
- }
32
- return {
33
- balanceA: e.balanceA,
34
- balanceB: e.balanceB,
35
- totalAsA: o,
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 P(e) {
40
- return {
41
- feeMode: e?.feeMode ?? "buy",
42
- feeRate: e?.feeRate ?? 0
43
- };
34
+ function i(e) {
35
+ return {
36
+ feeMode: e?.feeMode ?? "buy",
37
+ feeRate: e?.feeRate ?? 0
38
+ };
44
39
  }
45
- function C(e) {
46
- const {
47
- startPrice: t,
48
- stopPrice: r,
49
- minDifference: o,
50
- minRatio: l,
51
- maxCount: n,
52
- precision: c
53
- } = e;
54
- if (!o && l === 1 && !n)
55
- throw new Error("Invalid grid config");
56
- const i = {
57
- totalCount: 0,
58
- arithmeticCount: 0,
59
- geometricStartPrice: new a(t)
60
- };
61
- let u = new a(t), s = !o;
62
- for (; n && i.totalCount < n; ) {
63
- let m = u.times(l);
64
- if (!s) {
65
- const p = u.plus(o);
66
- p.lte(m) ? (s = !0, i.geometricStartPrice = u) : m = p;
67
- }
68
- if (m.gt(r)) break;
69
- s || (i.arithmeticCount += 1), i.totalCount += 1, u = m;
70
- }
71
- s ? u.isEqualTo(t) || (i.geometricStartPrice = i.geometricStartPrice.minus(t).div(u.minus(t)).times(r - t).plus(t)) : i.geometricStartPrice = new a(r);
72
- const h = [], x = i.arithmeticCount ? i.geometricStartPrice.minus(t).div(i.arithmeticCount) : 0, A = i.totalCount > i.arithmeticCount ? Math.pow(
73
- r / i.geometricStartPrice.toNumber(),
74
- 1 / (i.totalCount - i.arithmeticCount)
75
- ) : 0;
76
- let d = 0, f = new a(t);
77
- for (; d < i.arithmeticCount; ) {
78
- const m = +f.toFixed(c);
79
- f = f.plus(x);
80
- const p = +f.toFixed(c), g = {
81
- index: d,
82
- price: m,
83
- sellPrice: p
84
- };
85
- h.push(g), d += 1;
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 N(e, t) {
100
- const { enabledStartPrice: r, enabledStopPrice: o, placesPerSide: l, firstSellIndex: n } = {
101
- enabledStartPrice: 0,
102
- enabledStopPrice: e.length,
103
- firstSellIndex: -1,
104
- ...t
105
- };
106
- let c = 0, i = e.length;
107
- for (; c < e.length && e[c].price < r; )
108
- c += 1;
109
- if (o)
110
- for (; i >= 0 && e[i].sellPrice > o; )
111
- i -= 1;
112
- let u = c, s = i;
113
- return n >= 0 && l && (u = Math.max(
114
- u,
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 y(e, t) {
119
- let r = e.findIndex((o) => o.sellPrice >= t);
120
- return r < 0 && (r = e.length), r;
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 R(e, t, r) {
123
- return t / e * (1 - r) - 1 / (1 - r);
104
+ function c(e, t, n) {
105
+ return t / e * (1 - n) - 1 / (1 - n);
124
106
  }
125
- function F(e, t, r) {
126
- const o = e ? w(e, r) : { deltaA: 0, deltaB: 0 }, l = t ? w(t, r) : { deltaA: 0, deltaB: 0 };
127
- return {
128
- profitA: new a(o.deltaA).plus(l.deltaA).toNumber(),
129
- profitB: new a(o.deltaB).plus(l.deltaB).toNumber()
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 M(e, t) {
133
- const { feeMode: r, feeRate: o } = P(t);
134
- return t.marketType !== "future-c" && ["buy", "A"].includes(r) && (e /= 1 - o), t.precisionA && (e = +new a(e).toFixed(
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 I(e, t) {
140
- const { feeMode: r, feeRate: o } = P(t);
141
- return t.marketType !== "future-c" && r === "A" && (e /= 1 + o), t.precisionA && (e = +new a(e).toFixed(
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 O(e, t, r, o) {
147
- const l = [];
148
- r.sort((c, i) => c.price - i.price), o.sort((c, i) => i.price - c.price);
149
- let n = 0;
150
- for (const c of r) {
151
- for (; n < e.length && (e[n].price < c.price || l[n]); )
152
- n += 1;
153
- if (n >= e.length || n >= t)
154
- throw new Error(`Unmatched order: ${JSON.stringify(c)}`);
155
- l[n] = c;
156
- }
157
- n = e.length - 1;
158
- for (const c of o) {
159
- for (; n >= 0 && (e[n].sellPrice > c.price || l[n]); )
160
- n -= 1;
161
- if (n < 0 || n < t)
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
- export {
168
- B as calcBalance,
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.1",
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": "^9.3.1"
19
+ "bignumber.js": "^11.0.0"
20
20
  },
21
21
  "scripts": {
22
22
  "clean": "del-cli dist tsconfig.tsbuildinfo",