openbroker 1.9.6 → 1.12.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 +35 -0
- package/README.md +52 -0
- package/SKILL.md +57 -5
- package/bin/cli.ts +11 -0
- package/dist/auto/examples/grid.js +1 -1
- package/dist/auto/examples/mm-maker.js +4 -4
- package/dist/auto/examples/mm-spread.js +4 -4
- package/dist/core/client.d.ts +83 -32
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/client.js +214 -29
- package/dist/core/types.d.ts +22 -0
- package/dist/core/types.d.ts.map +1 -1
- 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/core/ws.d.ts +5 -0
- package/dist/core/ws.d.ts.map +1 -1
- package/dist/core/ws.js +9 -7
- package/dist/guardian/cli.d.ts +2 -0
- package/dist/guardian/cli.d.ts.map +1 -0
- package/dist/guardian/cli.js +288 -0
- package/dist/guardian/engine.d.ts +107 -0
- package/dist/guardian/engine.d.ts.map +1 -0
- package/dist/guardian/engine.js +526 -0
- package/dist/guardian/rules.d.ts +35 -0
- package/dist/guardian/rules.d.ts.map +1 -0
- package/dist/guardian/rules.js +237 -0
- package/dist/guardian/telegram.d.ts +24 -0
- package/dist/guardian/telegram.d.ts.map +1 -0
- package/dist/guardian/telegram.js +125 -0
- package/dist/guardian/types.d.ts +90 -0
- package/dist/guardian/types.d.ts.map +1 -0
- package/dist/guardian/types.js +45 -0
- package/dist/lib.d.ts +10 -1
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +6 -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/cancel.js +4 -1
- package/dist/operations/chase.d.ts +4 -2
- package/dist/operations/chase.d.ts.map +1 -1
- package/dist/operations/chase.js +61 -24
- 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 +2 -2
- package/scripts/auto/examples/grid.ts +1 -1
- package/scripts/auto/examples/mm-maker.ts +4 -4
- package/scripts/auto/examples/mm-spread.ts +4 -4
- package/scripts/core/client.ts +278 -33
- package/scripts/core/types.ts +23 -0
- package/scripts/core/utils.ts +37 -0
- package/scripts/core/ws.ts +9 -6
- package/scripts/guardian/cli.ts +322 -0
- package/scripts/guardian/engine.ts +614 -0
- package/scripts/guardian/rules.ts +265 -0
- package/scripts/guardian/telegram.ts +147 -0
- package/scripts/guardian/types.ts +160 -0
- package/scripts/lib.ts +36 -0
- package/scripts/operations/advanced-orders.test.ts +347 -14
- package/scripts/operations/bracket.ts +252 -73
- package/scripts/operations/cancel.ts +4 -1
- package/scripts/operations/chase.ts +61 -26
- 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
|
@@ -30,6 +30,35 @@ class StaticFillWatcher implements FillWatcher {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
function bracketClientStub(overrides: Partial<BracketClient> = {}): BracketClient {
|
|
34
|
+
return {
|
|
35
|
+
verbose: false,
|
|
36
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
37
|
+
async getAllMids() {
|
|
38
|
+
return { ETH: '1000' };
|
|
39
|
+
},
|
|
40
|
+
async marketOrder() {
|
|
41
|
+
throw new Error('marketOrder should not be called');
|
|
42
|
+
},
|
|
43
|
+
async limitOrder() {
|
|
44
|
+
throw new Error('limitOrder should not be called');
|
|
45
|
+
},
|
|
46
|
+
async tpslOrders() {
|
|
47
|
+
throw new Error('tpslOrders should not be called');
|
|
48
|
+
},
|
|
49
|
+
async bracketOrder() {
|
|
50
|
+
throw new Error('bracketOrder should not be called');
|
|
51
|
+
},
|
|
52
|
+
async cancel() {
|
|
53
|
+
throw new Error('cancel should not be called');
|
|
54
|
+
},
|
|
55
|
+
async getUserFills() {
|
|
56
|
+
return [];
|
|
57
|
+
},
|
|
58
|
+
...overrides,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
33
62
|
test('scale rejects invalid levels and rolls back resting orders on partial ladder placement', async () => {
|
|
34
63
|
const cancelled: Array<{ coin: string; oid: number }> = [];
|
|
35
64
|
const client: ScaleClient = {
|
|
@@ -81,28 +110,130 @@ test('scale rejects invalid levels and rolls back resting orders on partial ladd
|
|
|
81
110
|
]);
|
|
82
111
|
});
|
|
83
112
|
|
|
84
|
-
test('
|
|
85
|
-
|
|
86
|
-
const client:
|
|
113
|
+
test('scale pre-checks the $10 exchange minimum on the thinnest level (waived for reduce-only)', async () => {
|
|
114
|
+
let placed = false;
|
|
115
|
+
const client: ScaleClient = {
|
|
87
116
|
verbose: false,
|
|
88
|
-
address: '0x0000000000000000000000000000000000000001',
|
|
89
117
|
async getAllMids() {
|
|
90
118
|
return { ETH: '1000' };
|
|
91
119
|
},
|
|
92
|
-
async
|
|
93
|
-
|
|
120
|
+
async bulkOrder(orders) {
|
|
121
|
+
placed = true;
|
|
122
|
+
return okOrder(orders.map((_, i) => ({ resting: { oid: 200 + i } })));
|
|
123
|
+
},
|
|
124
|
+
async bulkCancel() {
|
|
125
|
+
return okCancel();
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// Total ~$30 over 3 linear levels → thinnest level ~$5 < $10.
|
|
130
|
+
await assert.rejects(
|
|
131
|
+
() => runScale({
|
|
132
|
+
coin: 'ETH',
|
|
133
|
+
side: 'buy',
|
|
134
|
+
size: 0.03,
|
|
135
|
+
levels: 3,
|
|
136
|
+
rangePct: 2,
|
|
137
|
+
client,
|
|
138
|
+
output: () => {},
|
|
139
|
+
}),
|
|
140
|
+
/below the \$10 exchange minimum/,
|
|
141
|
+
);
|
|
142
|
+
assert.equal(placed, false);
|
|
143
|
+
|
|
144
|
+
// Same ladder reduce-only is allowed (exchange waives the minimum).
|
|
145
|
+
const result = await runScale({
|
|
146
|
+
coin: 'ETH',
|
|
147
|
+
side: 'buy',
|
|
148
|
+
size: 0.03,
|
|
149
|
+
levels: 3,
|
|
150
|
+
rangePct: 2,
|
|
151
|
+
reduceOnly: true,
|
|
152
|
+
client,
|
|
153
|
+
output: () => {},
|
|
154
|
+
});
|
|
155
|
+
assert.equal(result.status, 'complete');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('bracket limit entry defaults to one atomic normalTpsl batch', async () => {
|
|
159
|
+
const calls: Array<{ coin: string; isBuy: boolean; size: number; entryPrice: number; opts: Record<string, unknown> }> = [];
|
|
160
|
+
const client = bracketClientStub({
|
|
161
|
+
async bracketOrder(coin, isBuy, size, entryPrice, opts) {
|
|
162
|
+
calls.push({ coin, isBuy, size, entryPrice, opts: opts ?? {} });
|
|
163
|
+
return okOrder([
|
|
164
|
+
{ resting: { oid: 500 } },
|
|
165
|
+
'waitingForFill' as never,
|
|
166
|
+
'waitingForFill' as never,
|
|
167
|
+
]);
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const result = await runBracket({
|
|
172
|
+
coin: 'ETH',
|
|
173
|
+
side: 'buy',
|
|
174
|
+
size: 1,
|
|
175
|
+
tpPct: 5,
|
|
176
|
+
slPct: 2,
|
|
177
|
+
entryType: 'limit',
|
|
178
|
+
entryPrice: 990,
|
|
179
|
+
client,
|
|
180
|
+
output: () => {},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
assert.equal(result.status, 'armed');
|
|
184
|
+
assert.equal(result.entryOid, 500);
|
|
185
|
+
assert.equal(calls.length, 1);
|
|
186
|
+
assert.equal(calls[0].isBuy, true);
|
|
187
|
+
assert.equal(calls[0].entryPrice, 990);
|
|
188
|
+
assert.equal(calls[0].opts.takeProfitPrice, 990 * 1.05);
|
|
189
|
+
assert.equal(calls[0].opts.stopLossPrice, 990 * 0.98);
|
|
190
|
+
assert.equal(calls[0].opts.stopLossIsMarket, true);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('bracket atomic batch rolls back resting orders when a leg is rejected', async () => {
|
|
194
|
+
const cancelled: number[] = [];
|
|
195
|
+
const client = bracketClientStub({
|
|
196
|
+
async bracketOrder() {
|
|
197
|
+
return okOrder([
|
|
198
|
+
{ resting: { oid: 600 } },
|
|
199
|
+
'waitingForFill' as never,
|
|
200
|
+
{ error: 'Invalid TP/SL price.' },
|
|
201
|
+
]);
|
|
202
|
+
},
|
|
203
|
+
async cancel(_coin, oid) {
|
|
204
|
+
cancelled.push(oid);
|
|
205
|
+
return okCancel();
|
|
94
206
|
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const result = await runBracket({
|
|
210
|
+
coin: 'ETH',
|
|
211
|
+
side: 'buy',
|
|
212
|
+
size: 1,
|
|
213
|
+
tpPct: 5,
|
|
214
|
+
slPct: 2,
|
|
215
|
+
entryType: 'limit',
|
|
216
|
+
entryPrice: 990,
|
|
217
|
+
client,
|
|
218
|
+
output: () => {},
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
assert.equal(result.status, 'entry_failed');
|
|
222
|
+
assert.match(result.reason ?? '', /Invalid TP\/SL price/);
|
|
223
|
+
assert.deepEqual(cancelled, [600]);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test('bracket with --no-atomic waits for limit-entry fill and arms positionTpsl for confirmed size', async () => {
|
|
227
|
+
const pairCalls: Array<{ size: number; isBuy: boolean; opts: Record<string, unknown> }> = [];
|
|
228
|
+
const client = bracketClientStub({
|
|
95
229
|
async limitOrder() {
|
|
96
230
|
return okOrder([{ resting: { oid: 777 } }]);
|
|
97
231
|
},
|
|
98
|
-
async
|
|
99
|
-
pairCalls.push({ size,
|
|
232
|
+
async tpslOrders(_coin, isBuy, size, opts) {
|
|
233
|
+
pairCalls.push({ size, isBuy, opts: opts ?? {} });
|
|
100
234
|
return okOrder([{ resting: { oid: 778 } }, { resting: { oid: 779 } }]);
|
|
101
235
|
},
|
|
102
|
-
|
|
103
|
-
return [];
|
|
104
|
-
},
|
|
105
|
-
};
|
|
236
|
+
});
|
|
106
237
|
|
|
107
238
|
const fillWatcher = new StaticFillWatcher(new Map([
|
|
108
239
|
[777, { size: 0.4, notional: 392, avgPrice: 980 }],
|
|
@@ -117,6 +248,7 @@ test('bracket waits for limit-entry fill and arms linked TP/SL for confirmed siz
|
|
|
117
248
|
entryType: 'limit',
|
|
118
249
|
entryPrice: 990,
|
|
119
250
|
entryTimeoutSec: 5,
|
|
251
|
+
atomic: false,
|
|
120
252
|
client,
|
|
121
253
|
fillWatcher,
|
|
122
254
|
output: () => {},
|
|
@@ -128,8 +260,85 @@ test('bracket waits for limit-entry fill and arms linked TP/SL for confirmed siz
|
|
|
128
260
|
assert.equal(pairCalls.length, 1);
|
|
129
261
|
assert.equal(pairCalls[0].size, 0.4);
|
|
130
262
|
assert.equal(pairCalls[0].isBuy, false);
|
|
131
|
-
assert.equal(pairCalls[0].
|
|
132
|
-
assert.equal(pairCalls[0].
|
|
263
|
+
assert.equal(pairCalls[0].opts.grouping, 'positionTpsl');
|
|
264
|
+
assert.equal(pairCalls[0].opts.takeProfitPrice, 1029);
|
|
265
|
+
assert.equal(pairCalls[0].opts.stopLossPrice, 960.4);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test('bracket market entry supports a one-sided TP sized to the actual fill', async () => {
|
|
269
|
+
const calls: Array<{ isBuy: boolean; size: number; opts: Record<string, unknown> }> = [];
|
|
270
|
+
const client = bracketClientStub({
|
|
271
|
+
async marketOrder() {
|
|
272
|
+
return okOrder([{ filled: { totalSz: '0.8', avgPx: '1001', oid: 1 } }]);
|
|
273
|
+
},
|
|
274
|
+
async tpslOrders(_coin, isBuy, size, opts) {
|
|
275
|
+
calls.push({ isBuy, size, opts: opts ?? {} });
|
|
276
|
+
return okOrder([{ resting: { oid: 2 } }]);
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const result = await runBracket({
|
|
281
|
+
coin: 'ETH',
|
|
282
|
+
side: 'buy',
|
|
283
|
+
size: 1,
|
|
284
|
+
tpPct: 5,
|
|
285
|
+
entryType: 'market',
|
|
286
|
+
client,
|
|
287
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
288
|
+
output: () => {},
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
assert.equal(result.status, 'complete');
|
|
292
|
+
assert.equal(result.protectedSize, 0.8);
|
|
293
|
+
assert.equal(calls.length, 1);
|
|
294
|
+
assert.equal(calls[0].size, 0.8);
|
|
295
|
+
assert.equal(calls[0].isBuy, false);
|
|
296
|
+
assert.equal(calls[0].opts.grouping, 'positionTpsl');
|
|
297
|
+
assert.equal(calls[0].opts.takeProfitPrice, 1001 * 1.05);
|
|
298
|
+
assert.equal(calls[0].opts.stopLossPrice, undefined);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('bracket validates targets before any order goes out', async () => {
|
|
302
|
+
// SL% ≥ 100 resolves to a negative price.
|
|
303
|
+
await assert.rejects(
|
|
304
|
+
() => runBracket({
|
|
305
|
+
coin: 'ETH',
|
|
306
|
+
side: 'buy',
|
|
307
|
+
size: 1,
|
|
308
|
+
tpPct: 5,
|
|
309
|
+
slPct: 150,
|
|
310
|
+
client: bracketClientStub(),
|
|
311
|
+
output: () => {},
|
|
312
|
+
}),
|
|
313
|
+
/SL must resolve to a positive price/,
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
// Absolute TP on the wrong side of the entry.
|
|
317
|
+
await assert.rejects(
|
|
318
|
+
() => runBracket({
|
|
319
|
+
coin: 'ETH',
|
|
320
|
+
side: 'buy',
|
|
321
|
+
size: 1,
|
|
322
|
+
tpPrice: 900,
|
|
323
|
+
entryType: 'limit',
|
|
324
|
+
entryPrice: 990,
|
|
325
|
+
client: bracketClientStub(),
|
|
326
|
+
output: () => {},
|
|
327
|
+
}),
|
|
328
|
+
/TP price must be above entry/,
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
// No target at all.
|
|
332
|
+
await assert.rejects(
|
|
333
|
+
() => runBracket({
|
|
334
|
+
coin: 'ETH',
|
|
335
|
+
side: 'buy',
|
|
336
|
+
size: 1,
|
|
337
|
+
client: bracketClientStub(),
|
|
338
|
+
output: () => {},
|
|
339
|
+
}),
|
|
340
|
+
/provide a TP target, an SL target, or both/,
|
|
341
|
+
);
|
|
133
342
|
});
|
|
134
343
|
|
|
135
344
|
test('chase requotes only the remaining size after a partial fill', async () => {
|
|
@@ -189,6 +398,130 @@ test('chase requotes only the remaining size after a partial fill', async () =>
|
|
|
189
398
|
assert.deepEqual(cancelled, [901]);
|
|
190
399
|
});
|
|
191
400
|
|
|
401
|
+
test('chase reprices and continues after a post-only rejection', async () => {
|
|
402
|
+
const fills = new Map<number, FillSummary>();
|
|
403
|
+
let orderCount = 0;
|
|
404
|
+
|
|
405
|
+
const client: ChaseClient = {
|
|
406
|
+
verbose: false,
|
|
407
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
408
|
+
async getAllMids() {
|
|
409
|
+
return { ETH: '1000' };
|
|
410
|
+
},
|
|
411
|
+
async getOpenOrders(): Promise<OpenOrder[]> {
|
|
412
|
+
return [];
|
|
413
|
+
},
|
|
414
|
+
async getUserFills() {
|
|
415
|
+
return [];
|
|
416
|
+
},
|
|
417
|
+
async limitOrder(_coin, _isBuy, size) {
|
|
418
|
+
orderCount += 1;
|
|
419
|
+
if (orderCount === 1) {
|
|
420
|
+
return okOrder([{ error: 'Post only order would have immediately matched, bbo was 999.9@1000.1' }]);
|
|
421
|
+
}
|
|
422
|
+
const oid = 900 + orderCount;
|
|
423
|
+
fills.set(oid, { size, notional: size * 1000, avgPrice: 1000 });
|
|
424
|
+
return okOrder([{ resting: { oid } }]);
|
|
425
|
+
},
|
|
426
|
+
async cancel() {
|
|
427
|
+
return okCancel();
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const result = await runChase({
|
|
432
|
+
coin: 'ETH',
|
|
433
|
+
side: 'buy',
|
|
434
|
+
size: 1,
|
|
435
|
+
offsetBps: 5,
|
|
436
|
+
timeoutSec: 2,
|
|
437
|
+
intervalMs: 1,
|
|
438
|
+
maxChaseBps: 1_000,
|
|
439
|
+
client,
|
|
440
|
+
fillWatcher: new StaticFillWatcher(fills),
|
|
441
|
+
output: () => {},
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
assert.equal(result.status, 'filled');
|
|
445
|
+
assert.equal(orderCount, 2);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
test('chase cancels the resting order even when the loop throws', async () => {
|
|
449
|
+
const cancelled: number[] = [];
|
|
450
|
+
|
|
451
|
+
const client: ChaseClient = {
|
|
452
|
+
verbose: false,
|
|
453
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
454
|
+
async getAllMids() {
|
|
455
|
+
return { ETH: '1000' };
|
|
456
|
+
},
|
|
457
|
+
async getOpenOrders(): Promise<OpenOrder[]> {
|
|
458
|
+
throw new Error('open-orders lookup exploded');
|
|
459
|
+
},
|
|
460
|
+
async getUserFills() {
|
|
461
|
+
return [];
|
|
462
|
+
},
|
|
463
|
+
async limitOrder() {
|
|
464
|
+
return okOrder([{ resting: { oid: 950 } }]);
|
|
465
|
+
},
|
|
466
|
+
async cancel(_coin, oid) {
|
|
467
|
+
cancelled.push(oid);
|
|
468
|
+
return okCancel();
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
await assert.rejects(
|
|
473
|
+
() => runChase({
|
|
474
|
+
coin: 'ETH',
|
|
475
|
+
side: 'buy',
|
|
476
|
+
size: 1,
|
|
477
|
+
offsetBps: 5,
|
|
478
|
+
timeoutSec: 2,
|
|
479
|
+
intervalMs: 1,
|
|
480
|
+
maxChaseBps: 1_000,
|
|
481
|
+
client,
|
|
482
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
483
|
+
output: () => {},
|
|
484
|
+
}),
|
|
485
|
+
/open-orders lookup exploded/,
|
|
486
|
+
);
|
|
487
|
+
|
|
488
|
+
assert.deepEqual(cancelled, [950]);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
test('chase rejects a start size below the $10 exchange minimum', async () => {
|
|
492
|
+
const client: ChaseClient = {
|
|
493
|
+
verbose: false,
|
|
494
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
495
|
+
async getAllMids() {
|
|
496
|
+
return { ETH: '1000' };
|
|
497
|
+
},
|
|
498
|
+
async getOpenOrders(): Promise<OpenOrder[]> {
|
|
499
|
+
return [];
|
|
500
|
+
},
|
|
501
|
+
async getUserFills() {
|
|
502
|
+
return [];
|
|
503
|
+
},
|
|
504
|
+
async limitOrder() {
|
|
505
|
+
throw new Error('limitOrder should not be called');
|
|
506
|
+
},
|
|
507
|
+
async cancel() {
|
|
508
|
+
return okCancel();
|
|
509
|
+
},
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
await assert.rejects(
|
|
513
|
+
() => runChase({
|
|
514
|
+
coin: 'ETH',
|
|
515
|
+
side: 'buy',
|
|
516
|
+
size: 0.005,
|
|
517
|
+
client,
|
|
518
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
519
|
+
output: () => {},
|
|
520
|
+
}),
|
|
521
|
+
/below the \$10 exchange minimum/,
|
|
522
|
+
);
|
|
523
|
+
});
|
|
524
|
+
|
|
192
525
|
test('user fill watcher de-duplicates repeated REST fallback fills', async () => {
|
|
193
526
|
const fillTime = Date.now();
|
|
194
527
|
const watcher = new UserFillWatcher({
|