openbroker 1.9.6 → 1.10.0
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/CHANGELOG.md +19 -0
- package/SKILL.md +13 -4
- package/dist/core/client.d.ts +60 -6
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/client.js +190 -26
- package/dist/core/utils.d.ts +29 -0
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/core/utils.js +27 -0
- package/dist/lib.d.ts +2 -1
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +1 -1
- package/dist/operations/advanced-orders.test.js +302 -13
- package/dist/operations/bracket.d.ts +35 -5
- package/dist/operations/bracket.d.ts.map +1 -1
- package/dist/operations/bracket.js +238 -64
- package/dist/operations/chase.d.ts +1 -1
- package/dist/operations/chase.d.ts.map +1 -1
- package/dist/operations/chase.js +58 -23
- package/dist/operations/scale.d.ts.map +1 -1
- package/dist/operations/scale.js +10 -1
- package/dist/operations/set-tpsl.js +69 -57
- package/dist/operations/trigger-order.js +18 -6
- package/dist/operations/twap.js +13 -1
- package/package.json +1 -1
- package/scripts/core/client.ts +249 -30
- package/scripts/core/utils.ts +37 -0
- package/scripts/lib.ts +3 -0
- package/scripts/operations/advanced-orders.test.ts +347 -14
- package/scripts/operations/bracket.ts +252 -73
- package/scripts/operations/chase.ts +57 -24
- package/scripts/operations/scale.ts +10 -1
- package/scripts/operations/set-tpsl.ts +62 -57
- package/scripts/operations/trigger-order.ts +20 -6
- package/scripts/operations/twap.ts +14 -1
package/dist/lib.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../scripts/lib.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,iBAAiB,EACjB,SAAS,GACV,MAAM,kBAAkB,CAAC;AAK1B,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,cAAc,GACf,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,UA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Q,EAAE,MAAM,uBAAuB,CAAC;AACjD,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEvE,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAClE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAInF,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,aAAa,EACb,wBAAwB,GACzB,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,YAAY,EACZ,kBAAkB,EAClB,oBAAoB,EACpB,cAAc,GACf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,eAAe,EACf,4BAA4B,EAC5B,2BAA2B,EAC3B,uBAAuB,GACxB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAErE,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,aAAa,EACb,uBAAuB,EACvB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAE5B,mBAAmB,iBAAiB,CAAC"}
|
package/dist/lib.js
CHANGED
|
@@ -11,7 +11,7 @@ export { HyperliquidClient, getClient, } from './core/client.js';
|
|
|
11
11
|
// REST-polling. `userFill` / `orderUpdate` are emitted per own-order event; see WsEventMap.
|
|
12
12
|
export { WebSocketManager, getWebSocket, resetWebSocket, } from './core/ws.js';
|
|
13
13
|
export { loadConfig, isConfigured, getNetwork, isMainnet, ensureConfigDir, getConfigPath, GLOBAL_CONFIG_DIR, GLOBAL_ENV_PATH, OPEN_BROKER_BUILDER_ADDRESS, } from './core/config.js';
|
|
14
|
-
export { roundPrice, roundSize, sleep, normalizeCoin, formatUsd, formatPercent, annualizeFundingRate, parseArgs, getSlippagePrice, getTimestampMs, generateCloid, orderToWire, checkBuilderFeeApproval, } from './core/utils.js';
|
|
14
|
+
export { roundPrice, roundSize, sleep, normalizeCoin, formatUsd, formatPercent, annualizeFundingRate, parseArgs, getSlippagePrice, getTimestampMs, generateCloid, orderToWire, checkBuilderFeeApproval, MIN_ORDER_NOTIONAL_USD, parseOrderStatus, } from './core/utils.js';
|
|
15
15
|
// ── Operations (in-process callable) ────────────────────────────────
|
|
16
16
|
export { runBracket } from './operations/bracket.js';
|
|
17
17
|
export { runChase } from './operations/chase.js';
|
|
@@ -26,6 +26,34 @@ class StaticFillWatcher {
|
|
|
26
26
|
return this.getFilled(oid);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
+
function bracketClientStub(overrides = {}) {
|
|
30
|
+
return {
|
|
31
|
+
verbose: false,
|
|
32
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
33
|
+
async getAllMids() {
|
|
34
|
+
return { ETH: '1000' };
|
|
35
|
+
},
|
|
36
|
+
async marketOrder() {
|
|
37
|
+
throw new Error('marketOrder should not be called');
|
|
38
|
+
},
|
|
39
|
+
async limitOrder() {
|
|
40
|
+
throw new Error('limitOrder should not be called');
|
|
41
|
+
},
|
|
42
|
+
async tpslOrders() {
|
|
43
|
+
throw new Error('tpslOrders should not be called');
|
|
44
|
+
},
|
|
45
|
+
async bracketOrder() {
|
|
46
|
+
throw new Error('bracketOrder should not be called');
|
|
47
|
+
},
|
|
48
|
+
async cancel() {
|
|
49
|
+
throw new Error('cancel should not be called');
|
|
50
|
+
},
|
|
51
|
+
async getUserFills() {
|
|
52
|
+
return [];
|
|
53
|
+
},
|
|
54
|
+
...overrides,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
29
57
|
test('scale rejects invalid levels and rolls back resting orders on partial ladder placement', async () => {
|
|
30
58
|
const cancelled = [];
|
|
31
59
|
const client = {
|
|
@@ -70,28 +98,118 @@ test('scale rejects invalid levels and rolls back resting orders on partial ladd
|
|
|
70
98
|
{ coin: 'ETH', oid: 103 },
|
|
71
99
|
]);
|
|
72
100
|
});
|
|
73
|
-
test('
|
|
74
|
-
|
|
101
|
+
test('scale pre-checks the $10 exchange minimum on the thinnest level (waived for reduce-only)', async () => {
|
|
102
|
+
let placed = false;
|
|
75
103
|
const client = {
|
|
76
104
|
verbose: false,
|
|
77
|
-
address: '0x0000000000000000000000000000000000000001',
|
|
78
105
|
async getAllMids() {
|
|
79
106
|
return { ETH: '1000' };
|
|
80
107
|
},
|
|
81
|
-
async
|
|
82
|
-
|
|
108
|
+
async bulkOrder(orders) {
|
|
109
|
+
placed = true;
|
|
110
|
+
return okOrder(orders.map((_, i) => ({ resting: { oid: 200 + i } })));
|
|
111
|
+
},
|
|
112
|
+
async bulkCancel() {
|
|
113
|
+
return okCancel();
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
// Total ~$30 over 3 linear levels → thinnest level ~$5 < $10.
|
|
117
|
+
await assert.rejects(() => runScale({
|
|
118
|
+
coin: 'ETH',
|
|
119
|
+
side: 'buy',
|
|
120
|
+
size: 0.03,
|
|
121
|
+
levels: 3,
|
|
122
|
+
rangePct: 2,
|
|
123
|
+
client,
|
|
124
|
+
output: () => { },
|
|
125
|
+
}), /below the \$10 exchange minimum/);
|
|
126
|
+
assert.equal(placed, false);
|
|
127
|
+
// Same ladder reduce-only is allowed (exchange waives the minimum).
|
|
128
|
+
const result = await runScale({
|
|
129
|
+
coin: 'ETH',
|
|
130
|
+
side: 'buy',
|
|
131
|
+
size: 0.03,
|
|
132
|
+
levels: 3,
|
|
133
|
+
rangePct: 2,
|
|
134
|
+
reduceOnly: true,
|
|
135
|
+
client,
|
|
136
|
+
output: () => { },
|
|
137
|
+
});
|
|
138
|
+
assert.equal(result.status, 'complete');
|
|
139
|
+
});
|
|
140
|
+
test('bracket limit entry defaults to one atomic normalTpsl batch', async () => {
|
|
141
|
+
const calls = [];
|
|
142
|
+
const client = bracketClientStub({
|
|
143
|
+
async bracketOrder(coin, isBuy, size, entryPrice, opts) {
|
|
144
|
+
calls.push({ coin, isBuy, size, entryPrice, opts: opts ?? {} });
|
|
145
|
+
return okOrder([
|
|
146
|
+
{ resting: { oid: 500 } },
|
|
147
|
+
'waitingForFill',
|
|
148
|
+
'waitingForFill',
|
|
149
|
+
]);
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
const result = await runBracket({
|
|
153
|
+
coin: 'ETH',
|
|
154
|
+
side: 'buy',
|
|
155
|
+
size: 1,
|
|
156
|
+
tpPct: 5,
|
|
157
|
+
slPct: 2,
|
|
158
|
+
entryType: 'limit',
|
|
159
|
+
entryPrice: 990,
|
|
160
|
+
client,
|
|
161
|
+
output: () => { },
|
|
162
|
+
});
|
|
163
|
+
assert.equal(result.status, 'armed');
|
|
164
|
+
assert.equal(result.entryOid, 500);
|
|
165
|
+
assert.equal(calls.length, 1);
|
|
166
|
+
assert.equal(calls[0].isBuy, true);
|
|
167
|
+
assert.equal(calls[0].entryPrice, 990);
|
|
168
|
+
assert.equal(calls[0].opts.takeProfitPrice, 990 * 1.05);
|
|
169
|
+
assert.equal(calls[0].opts.stopLossPrice, 990 * 0.98);
|
|
170
|
+
assert.equal(calls[0].opts.stopLossIsMarket, true);
|
|
171
|
+
});
|
|
172
|
+
test('bracket atomic batch rolls back resting orders when a leg is rejected', async () => {
|
|
173
|
+
const cancelled = [];
|
|
174
|
+
const client = bracketClientStub({
|
|
175
|
+
async bracketOrder() {
|
|
176
|
+
return okOrder([
|
|
177
|
+
{ resting: { oid: 600 } },
|
|
178
|
+
'waitingForFill',
|
|
179
|
+
{ error: 'Invalid TP/SL price.' },
|
|
180
|
+
]);
|
|
181
|
+
},
|
|
182
|
+
async cancel(_coin, oid) {
|
|
183
|
+
cancelled.push(oid);
|
|
184
|
+
return okCancel();
|
|
83
185
|
},
|
|
186
|
+
});
|
|
187
|
+
const result = await runBracket({
|
|
188
|
+
coin: 'ETH',
|
|
189
|
+
side: 'buy',
|
|
190
|
+
size: 1,
|
|
191
|
+
tpPct: 5,
|
|
192
|
+
slPct: 2,
|
|
193
|
+
entryType: 'limit',
|
|
194
|
+
entryPrice: 990,
|
|
195
|
+
client,
|
|
196
|
+
output: () => { },
|
|
197
|
+
});
|
|
198
|
+
assert.equal(result.status, 'entry_failed');
|
|
199
|
+
assert.match(result.reason ?? '', /Invalid TP\/SL price/);
|
|
200
|
+
assert.deepEqual(cancelled, [600]);
|
|
201
|
+
});
|
|
202
|
+
test('bracket with --no-atomic waits for limit-entry fill and arms positionTpsl for confirmed size', async () => {
|
|
203
|
+
const pairCalls = [];
|
|
204
|
+
const client = bracketClientStub({
|
|
84
205
|
async limitOrder() {
|
|
85
206
|
return okOrder([{ resting: { oid: 777 } }]);
|
|
86
207
|
},
|
|
87
|
-
async
|
|
88
|
-
pairCalls.push({ size,
|
|
208
|
+
async tpslOrders(_coin, isBuy, size, opts) {
|
|
209
|
+
pairCalls.push({ size, isBuy, opts: opts ?? {} });
|
|
89
210
|
return okOrder([{ resting: { oid: 778 } }, { resting: { oid: 779 } }]);
|
|
90
211
|
},
|
|
91
|
-
|
|
92
|
-
return [];
|
|
93
|
-
},
|
|
94
|
-
};
|
|
212
|
+
});
|
|
95
213
|
const fillWatcher = new StaticFillWatcher(new Map([
|
|
96
214
|
[777, { size: 0.4, notional: 392, avgPrice: 980 }],
|
|
97
215
|
]));
|
|
@@ -104,6 +222,7 @@ test('bracket waits for limit-entry fill and arms linked TP/SL for confirmed siz
|
|
|
104
222
|
entryType: 'limit',
|
|
105
223
|
entryPrice: 990,
|
|
106
224
|
entryTimeoutSec: 5,
|
|
225
|
+
atomic: false,
|
|
107
226
|
client,
|
|
108
227
|
fillWatcher,
|
|
109
228
|
output: () => { },
|
|
@@ -114,8 +233,70 @@ test('bracket waits for limit-entry fill and arms linked TP/SL for confirmed siz
|
|
|
114
233
|
assert.equal(pairCalls.length, 1);
|
|
115
234
|
assert.equal(pairCalls[0].size, 0.4);
|
|
116
235
|
assert.equal(pairCalls[0].isBuy, false);
|
|
117
|
-
assert.equal(pairCalls[0].
|
|
118
|
-
assert.equal(pairCalls[0].
|
|
236
|
+
assert.equal(pairCalls[0].opts.grouping, 'positionTpsl');
|
|
237
|
+
assert.equal(pairCalls[0].opts.takeProfitPrice, 1029);
|
|
238
|
+
assert.equal(pairCalls[0].opts.stopLossPrice, 960.4);
|
|
239
|
+
});
|
|
240
|
+
test('bracket market entry supports a one-sided TP sized to the actual fill', async () => {
|
|
241
|
+
const calls = [];
|
|
242
|
+
const client = bracketClientStub({
|
|
243
|
+
async marketOrder() {
|
|
244
|
+
return okOrder([{ filled: { totalSz: '0.8', avgPx: '1001', oid: 1 } }]);
|
|
245
|
+
},
|
|
246
|
+
async tpslOrders(_coin, isBuy, size, opts) {
|
|
247
|
+
calls.push({ isBuy, size, opts: opts ?? {} });
|
|
248
|
+
return okOrder([{ resting: { oid: 2 } }]);
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
const result = await runBracket({
|
|
252
|
+
coin: 'ETH',
|
|
253
|
+
side: 'buy',
|
|
254
|
+
size: 1,
|
|
255
|
+
tpPct: 5,
|
|
256
|
+
entryType: 'market',
|
|
257
|
+
client,
|
|
258
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
259
|
+
output: () => { },
|
|
260
|
+
});
|
|
261
|
+
assert.equal(result.status, 'complete');
|
|
262
|
+
assert.equal(result.protectedSize, 0.8);
|
|
263
|
+
assert.equal(calls.length, 1);
|
|
264
|
+
assert.equal(calls[0].size, 0.8);
|
|
265
|
+
assert.equal(calls[0].isBuy, false);
|
|
266
|
+
assert.equal(calls[0].opts.grouping, 'positionTpsl');
|
|
267
|
+
assert.equal(calls[0].opts.takeProfitPrice, 1001 * 1.05);
|
|
268
|
+
assert.equal(calls[0].opts.stopLossPrice, undefined);
|
|
269
|
+
});
|
|
270
|
+
test('bracket validates targets before any order goes out', async () => {
|
|
271
|
+
// SL% ≥ 100 resolves to a negative price.
|
|
272
|
+
await assert.rejects(() => runBracket({
|
|
273
|
+
coin: 'ETH',
|
|
274
|
+
side: 'buy',
|
|
275
|
+
size: 1,
|
|
276
|
+
tpPct: 5,
|
|
277
|
+
slPct: 150,
|
|
278
|
+
client: bracketClientStub(),
|
|
279
|
+
output: () => { },
|
|
280
|
+
}), /SL must resolve to a positive price/);
|
|
281
|
+
// Absolute TP on the wrong side of the entry.
|
|
282
|
+
await assert.rejects(() => runBracket({
|
|
283
|
+
coin: 'ETH',
|
|
284
|
+
side: 'buy',
|
|
285
|
+
size: 1,
|
|
286
|
+
tpPrice: 900,
|
|
287
|
+
entryType: 'limit',
|
|
288
|
+
entryPrice: 990,
|
|
289
|
+
client: bracketClientStub(),
|
|
290
|
+
output: () => { },
|
|
291
|
+
}), /TP price must be above entry/);
|
|
292
|
+
// No target at all.
|
|
293
|
+
await assert.rejects(() => runBracket({
|
|
294
|
+
coin: 'ETH',
|
|
295
|
+
side: 'buy',
|
|
296
|
+
size: 1,
|
|
297
|
+
client: bracketClientStub(),
|
|
298
|
+
output: () => { },
|
|
299
|
+
}), /provide a TP target, an SL target, or both/);
|
|
119
300
|
});
|
|
120
301
|
test('chase requotes only the remaining size after a partial fill', async () => {
|
|
121
302
|
const limitSizes = [];
|
|
@@ -171,6 +352,114 @@ test('chase requotes only the remaining size after a partial fill', async () =>
|
|
|
171
352
|
assert(Math.abs(limitSizes[1] - 0.6) < 1e-9);
|
|
172
353
|
assert.deepEqual(cancelled, [901]);
|
|
173
354
|
});
|
|
355
|
+
test('chase reprices and continues after a post-only rejection', async () => {
|
|
356
|
+
const fills = new Map();
|
|
357
|
+
let orderCount = 0;
|
|
358
|
+
const client = {
|
|
359
|
+
verbose: false,
|
|
360
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
361
|
+
async getAllMids() {
|
|
362
|
+
return { ETH: '1000' };
|
|
363
|
+
},
|
|
364
|
+
async getOpenOrders() {
|
|
365
|
+
return [];
|
|
366
|
+
},
|
|
367
|
+
async getUserFills() {
|
|
368
|
+
return [];
|
|
369
|
+
},
|
|
370
|
+
async limitOrder(_coin, _isBuy, size) {
|
|
371
|
+
orderCount += 1;
|
|
372
|
+
if (orderCount === 1) {
|
|
373
|
+
return okOrder([{ error: 'Post only order would have immediately matched, bbo was 999.9@1000.1' }]);
|
|
374
|
+
}
|
|
375
|
+
const oid = 900 + orderCount;
|
|
376
|
+
fills.set(oid, { size, notional: size * 1000, avgPrice: 1000 });
|
|
377
|
+
return okOrder([{ resting: { oid } }]);
|
|
378
|
+
},
|
|
379
|
+
async cancel() {
|
|
380
|
+
return okCancel();
|
|
381
|
+
},
|
|
382
|
+
};
|
|
383
|
+
const result = await runChase({
|
|
384
|
+
coin: 'ETH',
|
|
385
|
+
side: 'buy',
|
|
386
|
+
size: 1,
|
|
387
|
+
offsetBps: 5,
|
|
388
|
+
timeoutSec: 2,
|
|
389
|
+
intervalMs: 1,
|
|
390
|
+
maxChaseBps: 1_000,
|
|
391
|
+
client,
|
|
392
|
+
fillWatcher: new StaticFillWatcher(fills),
|
|
393
|
+
output: () => { },
|
|
394
|
+
});
|
|
395
|
+
assert.equal(result.status, 'filled');
|
|
396
|
+
assert.equal(orderCount, 2);
|
|
397
|
+
});
|
|
398
|
+
test('chase cancels the resting order even when the loop throws', async () => {
|
|
399
|
+
const cancelled = [];
|
|
400
|
+
const client = {
|
|
401
|
+
verbose: false,
|
|
402
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
403
|
+
async getAllMids() {
|
|
404
|
+
return { ETH: '1000' };
|
|
405
|
+
},
|
|
406
|
+
async getOpenOrders() {
|
|
407
|
+
throw new Error('open-orders lookup exploded');
|
|
408
|
+
},
|
|
409
|
+
async getUserFills() {
|
|
410
|
+
return [];
|
|
411
|
+
},
|
|
412
|
+
async limitOrder() {
|
|
413
|
+
return okOrder([{ resting: { oid: 950 } }]);
|
|
414
|
+
},
|
|
415
|
+
async cancel(_coin, oid) {
|
|
416
|
+
cancelled.push(oid);
|
|
417
|
+
return okCancel();
|
|
418
|
+
},
|
|
419
|
+
};
|
|
420
|
+
await assert.rejects(() => runChase({
|
|
421
|
+
coin: 'ETH',
|
|
422
|
+
side: 'buy',
|
|
423
|
+
size: 1,
|
|
424
|
+
offsetBps: 5,
|
|
425
|
+
timeoutSec: 2,
|
|
426
|
+
intervalMs: 1,
|
|
427
|
+
maxChaseBps: 1_000,
|
|
428
|
+
client,
|
|
429
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
430
|
+
output: () => { },
|
|
431
|
+
}), /open-orders lookup exploded/);
|
|
432
|
+
assert.deepEqual(cancelled, [950]);
|
|
433
|
+
});
|
|
434
|
+
test('chase rejects a start size below the $10 exchange minimum', async () => {
|
|
435
|
+
const client = {
|
|
436
|
+
verbose: false,
|
|
437
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
438
|
+
async getAllMids() {
|
|
439
|
+
return { ETH: '1000' };
|
|
440
|
+
},
|
|
441
|
+
async getOpenOrders() {
|
|
442
|
+
return [];
|
|
443
|
+
},
|
|
444
|
+
async getUserFills() {
|
|
445
|
+
return [];
|
|
446
|
+
},
|
|
447
|
+
async limitOrder() {
|
|
448
|
+
throw new Error('limitOrder should not be called');
|
|
449
|
+
},
|
|
450
|
+
async cancel() {
|
|
451
|
+
return okCancel();
|
|
452
|
+
},
|
|
453
|
+
};
|
|
454
|
+
await assert.rejects(() => runChase({
|
|
455
|
+
coin: 'ETH',
|
|
456
|
+
side: 'buy',
|
|
457
|
+
size: 0.005,
|
|
458
|
+
client,
|
|
459
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
460
|
+
output: () => { },
|
|
461
|
+
}), /below the \$10 exchange minimum/);
|
|
462
|
+
});
|
|
174
463
|
test('user fill watcher de-duplicates repeated REST fallback fills', async () => {
|
|
175
464
|
const fillTime = Date.now();
|
|
176
465
|
const watcher = new UserFillWatcher({
|
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
#!/usr/bin/env npx tsx
|
|
2
|
-
import type { OrderResponse } from '../core/types.js';
|
|
2
|
+
import type { CancelResponse, OrderResponse } from '../core/types.js';
|
|
3
3
|
import { type FillWatcher } from './execution.js';
|
|
4
4
|
export interface BracketOptions {
|
|
5
5
|
coin: string;
|
|
6
6
|
side: 'buy' | 'sell';
|
|
7
7
|
size: number;
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/** Take profit distance in % from entry. Optional if tpPrice/sl* given. */
|
|
9
|
+
tpPct?: number;
|
|
10
|
+
/** Stop loss distance in % from entry. Optional if slPrice/tp* given. */
|
|
11
|
+
slPct?: number;
|
|
12
|
+
/** Absolute take profit price (takes precedence over tpPct). */
|
|
13
|
+
tpPrice?: number;
|
|
14
|
+
/** Absolute stop loss trigger price (takes precedence over slPct). */
|
|
15
|
+
slPrice?: number;
|
|
10
16
|
entryType?: 'market' | 'limit';
|
|
11
17
|
entryPrice?: number;
|
|
12
18
|
slippage?: number;
|
|
13
19
|
entryTimeoutSec?: number;
|
|
14
20
|
slSlippageBps?: number;
|
|
21
|
+
/** SL fires as a market trigger (default true); false = stop-limit. */
|
|
22
|
+
slMarket?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Limit entries only: submit entry + TP/SL as one atomic normalTpsl batch
|
|
25
|
+
* (default true) — the exchange arms the exits when the entry fills, so the
|
|
26
|
+
* bracket survives this process exiting. false = legacy fill-watch path.
|
|
27
|
+
*/
|
|
28
|
+
atomic?: boolean;
|
|
15
29
|
leverage?: number;
|
|
16
30
|
dryRun?: boolean;
|
|
17
31
|
verbose?: boolean;
|
|
@@ -25,7 +39,23 @@ export interface BracketClient {
|
|
|
25
39
|
getAllMids(): Promise<Record<string, string>>;
|
|
26
40
|
marketOrder(coin: string, isBuy: boolean, size: number, slippageBps?: number, leverage?: number): Promise<OrderResponse>;
|
|
27
41
|
limitOrder(coin: string, isBuy: boolean, size: number, price: number, tif?: 'Gtc' | 'Ioc' | 'Alo', reduceOnly?: boolean, leverage?: number): Promise<OrderResponse>;
|
|
28
|
-
|
|
42
|
+
tpslOrders(coin: string, isBuy: boolean, size: number, opts?: {
|
|
43
|
+
takeProfitPrice?: number;
|
|
44
|
+
stopLossPrice?: number;
|
|
45
|
+
stopLossSlippageBps?: number;
|
|
46
|
+
stopLossIsMarket?: boolean;
|
|
47
|
+
grouping?: 'positionTpsl' | 'normalTpsl';
|
|
48
|
+
leverage?: number;
|
|
49
|
+
}): Promise<OrderResponse>;
|
|
50
|
+
bracketOrder(coin: string, isBuy: boolean, size: number, entryPrice: number, opts?: {
|
|
51
|
+
entryTif?: 'Gtc' | 'Alo';
|
|
52
|
+
takeProfitPrice?: number;
|
|
53
|
+
stopLossPrice?: number;
|
|
54
|
+
stopLossSlippageBps?: number;
|
|
55
|
+
stopLossIsMarket?: boolean;
|
|
56
|
+
leverage?: number;
|
|
57
|
+
}): Promise<OrderResponse>;
|
|
58
|
+
cancel(coin: string, oid: number): Promise<CancelResponse>;
|
|
29
59
|
address: string;
|
|
30
60
|
getUserFills(user?: string): Promise<Array<{
|
|
31
61
|
coin: string;
|
|
@@ -36,7 +66,7 @@ export interface BracketClient {
|
|
|
36
66
|
}>>;
|
|
37
67
|
}
|
|
38
68
|
export interface BracketResult {
|
|
39
|
-
status: 'dry' | 'limit_resting' | 'complete' | 'entry_failed' | 'partial';
|
|
69
|
+
status: 'dry' | 'armed' | 'limit_resting' | 'complete' | 'entry_failed' | 'partial';
|
|
40
70
|
entryPrice?: number;
|
|
41
71
|
tpPrice?: number;
|
|
42
72
|
slPrice?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bracket.d.ts","sourceRoot":"","sources":["../../scripts/operations/bracket.ts"],"names":[],"mappings":";AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"bracket.d.ts","sourceRoot":"","sources":["../../scripts/operations/bracket.ts"],"names":[],"mappings":";AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEtE,OAAO,EAAmB,KAAK,WAAW,EAAE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