@tradejs/connectors 3.1.29-beta.289 → 3.1.29-beta.291
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 +58 -16
- package/dist/index.mjs +58 -16
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1220,26 +1220,64 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1220
1220
|
}
|
|
1221
1221
|
return positions;
|
|
1222
1222
|
},
|
|
1223
|
+
getEntryOrderObservation: async ({ symbol, orderId }) => {
|
|
1224
|
+
const client = await getPrivateClient();
|
|
1225
|
+
if (!client) throw new Error("Bybit private client is unavailable");
|
|
1226
|
+
const query = { category: BYBIT_CATEGORY2, symbol, orderLinkId: orderId };
|
|
1227
|
+
const active = await client.getActiveOrders(query);
|
|
1228
|
+
if (active.retCode !== 0)
|
|
1229
|
+
throw new Error(`Bybit order observation failed: ${active.retCode}`);
|
|
1230
|
+
let order = active.result.list.find(
|
|
1231
|
+
(o) => o.orderLinkId === orderId && o.symbol === symbol
|
|
1232
|
+
);
|
|
1233
|
+
if (!order) {
|
|
1234
|
+
const history = await client.getHistoricOrders(query);
|
|
1235
|
+
if (history.retCode !== 0)
|
|
1236
|
+
throw new Error(`Bybit order history failed: ${history.retCode}`);
|
|
1237
|
+
order = history.result.list.find(
|
|
1238
|
+
(o) => o.orderLinkId === orderId && o.symbol === symbol
|
|
1239
|
+
);
|
|
1240
|
+
}
|
|
1241
|
+
if (!order) return null;
|
|
1242
|
+
const filledQty = Number(order.cumExecQty);
|
|
1243
|
+
if (!Number.isFinite(filledQty) || filledQty < 0)
|
|
1244
|
+
throw new Error("Invalid Bybit cumulative execution quantity");
|
|
1245
|
+
const status = order.orderStatus === "Filled" ? "filled" : order.orderStatus === "Rejected" ? "rejected" : ["Cancelled", "PartiallyFilledCanceled", "Deactivated"].includes(
|
|
1246
|
+
order.orderStatus
|
|
1247
|
+
) ? "cancelled" : filledQty > 0 ? "partial" : "pending";
|
|
1248
|
+
return { id: orderId, status, filledQty, positionId: orderId };
|
|
1249
|
+
},
|
|
1223
1250
|
getOpenPositionPnl: async () => {
|
|
1224
1251
|
const client = await getPrivateClient();
|
|
1225
1252
|
if (!client) {
|
|
1226
1253
|
throw new Error("Bybit private client is unavailable");
|
|
1227
1254
|
}
|
|
1228
|
-
const
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1255
|
+
const observedAt = Date.now();
|
|
1256
|
+
const rows = [];
|
|
1257
|
+
let cursor;
|
|
1258
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1259
|
+
do {
|
|
1260
|
+
const response = await client.getPositionInfo({
|
|
1261
|
+
category: BYBIT_CATEGORY2,
|
|
1262
|
+
settleCoin: "USDT",
|
|
1263
|
+
limit: 200,
|
|
1264
|
+
...cursor ? { cursor } : {}
|
|
1265
|
+
});
|
|
1266
|
+
if (response.retCode !== 0)
|
|
1267
|
+
throw new Error(
|
|
1268
|
+
`Bybit positions request failed: ${response.retCode} ${response.retMsg ?? ""}`.trim()
|
|
1269
|
+
);
|
|
1270
|
+
rows.push(
|
|
1271
|
+
...response.result.list
|
|
1240
1272
|
);
|
|
1241
|
-
|
|
1242
|
-
|
|
1273
|
+
cursor = response.result.nextPageCursor || void 0;
|
|
1274
|
+
if (cursor) {
|
|
1275
|
+
if (seen.has(cursor))
|
|
1276
|
+
throw new Error("Bybit position pagination repeated cursor");
|
|
1277
|
+
seen.add(cursor);
|
|
1278
|
+
}
|
|
1279
|
+
} while (cursor);
|
|
1280
|
+
return rows.map((item) => {
|
|
1243
1281
|
const qty = Number(item.size ?? Number.NaN);
|
|
1244
1282
|
const entryPrice = Number(item.avgPrice ?? Number.NaN);
|
|
1245
1283
|
const currentPrice = Number(item.markPrice ?? Number.NaN);
|
|
@@ -1247,14 +1285,16 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1247
1285
|
const takeProfitPrice = toFiniteNumberOrNull(item.takeProfit);
|
|
1248
1286
|
const stopLossPrice = toFiniteNumberOrNull(item.stopLoss);
|
|
1249
1287
|
const side = String(item.side ?? "");
|
|
1288
|
+
if (qty === 0) return null;
|
|
1250
1289
|
if (!Number.isFinite(qty) || qty <= 0 || !Number.isFinite(entryPrice) || !Number.isFinite(currentPrice) || !Number.isFinite(unrealizedPnl) || side !== "Buy" && side !== "Sell") {
|
|
1251
|
-
|
|
1290
|
+
throw new Error("Invalid active Bybit position snapshot");
|
|
1252
1291
|
}
|
|
1253
1292
|
return {
|
|
1254
1293
|
symbol: String(item.symbol ?? ""),
|
|
1255
1294
|
qty,
|
|
1256
1295
|
price: entryPrice,
|
|
1257
1296
|
currentPrice,
|
|
1297
|
+
observedAt,
|
|
1258
1298
|
unrealizedPnl,
|
|
1259
1299
|
direction: side === "Buy" ? "LONG" : "SHORT",
|
|
1260
1300
|
...takeProfitPrice != null && takeProfitPrice > 0 ? { takeProfitPrice } : {},
|
|
@@ -1455,7 +1495,8 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1455
1495
|
orderId,
|
|
1456
1496
|
signal,
|
|
1457
1497
|
leverage,
|
|
1458
|
-
beforeSubmit
|
|
1498
|
+
beforeSubmit,
|
|
1499
|
+
onRejected
|
|
1459
1500
|
}) => {
|
|
1460
1501
|
return withBybitOrderLock(
|
|
1461
1502
|
JSON.stringify([
|
|
@@ -1639,6 +1680,7 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1639
1680
|
if (signal) {
|
|
1640
1681
|
signal.orderFailureReason = typeof orderRes.retMsg === "string" && orderRes.retMsg.trim() ? orderRes.retMsg.trim() : `BYBIT_RETCODE_${orderRes.retCode}`;
|
|
1641
1682
|
}
|
|
1683
|
+
await onRejected?.();
|
|
1642
1684
|
return false;
|
|
1643
1685
|
}
|
|
1644
1686
|
if (signal) {
|
package/dist/index.mjs
CHANGED
|
@@ -1172,26 +1172,64 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1172
1172
|
}
|
|
1173
1173
|
return positions;
|
|
1174
1174
|
},
|
|
1175
|
+
getEntryOrderObservation: async ({ symbol, orderId }) => {
|
|
1176
|
+
const client = await getPrivateClient();
|
|
1177
|
+
if (!client) throw new Error("Bybit private client is unavailable");
|
|
1178
|
+
const query = { category: BYBIT_CATEGORY2, symbol, orderLinkId: orderId };
|
|
1179
|
+
const active = await client.getActiveOrders(query);
|
|
1180
|
+
if (active.retCode !== 0)
|
|
1181
|
+
throw new Error(`Bybit order observation failed: ${active.retCode}`);
|
|
1182
|
+
let order = active.result.list.find(
|
|
1183
|
+
(o) => o.orderLinkId === orderId && o.symbol === symbol
|
|
1184
|
+
);
|
|
1185
|
+
if (!order) {
|
|
1186
|
+
const history = await client.getHistoricOrders(query);
|
|
1187
|
+
if (history.retCode !== 0)
|
|
1188
|
+
throw new Error(`Bybit order history failed: ${history.retCode}`);
|
|
1189
|
+
order = history.result.list.find(
|
|
1190
|
+
(o) => o.orderLinkId === orderId && o.symbol === symbol
|
|
1191
|
+
);
|
|
1192
|
+
}
|
|
1193
|
+
if (!order) return null;
|
|
1194
|
+
const filledQty = Number(order.cumExecQty);
|
|
1195
|
+
if (!Number.isFinite(filledQty) || filledQty < 0)
|
|
1196
|
+
throw new Error("Invalid Bybit cumulative execution quantity");
|
|
1197
|
+
const status = order.orderStatus === "Filled" ? "filled" : order.orderStatus === "Rejected" ? "rejected" : ["Cancelled", "PartiallyFilledCanceled", "Deactivated"].includes(
|
|
1198
|
+
order.orderStatus
|
|
1199
|
+
) ? "cancelled" : filledQty > 0 ? "partial" : "pending";
|
|
1200
|
+
return { id: orderId, status, filledQty, positionId: orderId };
|
|
1201
|
+
},
|
|
1175
1202
|
getOpenPositionPnl: async () => {
|
|
1176
1203
|
const client = await getPrivateClient();
|
|
1177
1204
|
if (!client) {
|
|
1178
1205
|
throw new Error("Bybit private client is unavailable");
|
|
1179
1206
|
}
|
|
1180
|
-
const
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1207
|
+
const observedAt = Date.now();
|
|
1208
|
+
const rows = [];
|
|
1209
|
+
let cursor;
|
|
1210
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1211
|
+
do {
|
|
1212
|
+
const response = await client.getPositionInfo({
|
|
1213
|
+
category: BYBIT_CATEGORY2,
|
|
1214
|
+
settleCoin: "USDT",
|
|
1215
|
+
limit: 200,
|
|
1216
|
+
...cursor ? { cursor } : {}
|
|
1217
|
+
});
|
|
1218
|
+
if (response.retCode !== 0)
|
|
1219
|
+
throw new Error(
|
|
1220
|
+
`Bybit positions request failed: ${response.retCode} ${response.retMsg ?? ""}`.trim()
|
|
1221
|
+
);
|
|
1222
|
+
rows.push(
|
|
1223
|
+
...response.result.list
|
|
1192
1224
|
);
|
|
1193
|
-
|
|
1194
|
-
|
|
1225
|
+
cursor = response.result.nextPageCursor || void 0;
|
|
1226
|
+
if (cursor) {
|
|
1227
|
+
if (seen.has(cursor))
|
|
1228
|
+
throw new Error("Bybit position pagination repeated cursor");
|
|
1229
|
+
seen.add(cursor);
|
|
1230
|
+
}
|
|
1231
|
+
} while (cursor);
|
|
1232
|
+
return rows.map((item) => {
|
|
1195
1233
|
const qty = Number(item.size ?? Number.NaN);
|
|
1196
1234
|
const entryPrice = Number(item.avgPrice ?? Number.NaN);
|
|
1197
1235
|
const currentPrice = Number(item.markPrice ?? Number.NaN);
|
|
@@ -1199,14 +1237,16 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1199
1237
|
const takeProfitPrice = toFiniteNumberOrNull(item.takeProfit);
|
|
1200
1238
|
const stopLossPrice = toFiniteNumberOrNull(item.stopLoss);
|
|
1201
1239
|
const side = String(item.side ?? "");
|
|
1240
|
+
if (qty === 0) return null;
|
|
1202
1241
|
if (!Number.isFinite(qty) || qty <= 0 || !Number.isFinite(entryPrice) || !Number.isFinite(currentPrice) || !Number.isFinite(unrealizedPnl) || side !== "Buy" && side !== "Sell") {
|
|
1203
|
-
|
|
1242
|
+
throw new Error("Invalid active Bybit position snapshot");
|
|
1204
1243
|
}
|
|
1205
1244
|
return {
|
|
1206
1245
|
symbol: String(item.symbol ?? ""),
|
|
1207
1246
|
qty,
|
|
1208
1247
|
price: entryPrice,
|
|
1209
1248
|
currentPrice,
|
|
1249
|
+
observedAt,
|
|
1210
1250
|
unrealizedPnl,
|
|
1211
1251
|
direction: side === "Buy" ? "LONG" : "SHORT",
|
|
1212
1252
|
...takeProfitPrice != null && takeProfitPrice > 0 ? { takeProfitPrice } : {},
|
|
@@ -1407,7 +1447,8 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1407
1447
|
orderId,
|
|
1408
1448
|
signal,
|
|
1409
1449
|
leverage,
|
|
1410
|
-
beforeSubmit
|
|
1450
|
+
beforeSubmit,
|
|
1451
|
+
onRejected
|
|
1411
1452
|
}) => {
|
|
1412
1453
|
return withBybitOrderLock(
|
|
1413
1454
|
JSON.stringify([
|
|
@@ -1591,6 +1632,7 @@ var ByBitConnectorCreator = async (config, runtime) => {
|
|
|
1591
1632
|
if (signal) {
|
|
1592
1633
|
signal.orderFailureReason = typeof orderRes.retMsg === "string" && orderRes.retMsg.trim() ? orderRes.retMsg.trim() : `BYBIT_RETCODE_${orderRes.retCode}`;
|
|
1593
1634
|
}
|
|
1635
|
+
await onRejected?.();
|
|
1594
1636
|
return false;
|
|
1595
1637
|
}
|
|
1596
1638
|
if (signal) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tradejs/connectors",
|
|
3
|
-
"version": "3.1.29-beta.
|
|
3
|
+
"version": "3.1.29-beta.291",
|
|
4
4
|
"description": "MIT-licensed exchange connectors and market data providers for the TradeJS framework.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tradejs",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@tradejs/core": "^3.1.29-beta.
|
|
43
|
-
"@tradejs/types": "^3.1.29-beta.
|
|
42
|
+
"@tradejs/core": "^3.1.29-beta.291",
|
|
43
|
+
"@tradejs/types": "^3.1.29-beta.291",
|
|
44
44
|
"bybit-api": "^4.7.1",
|
|
45
45
|
"chalk": "4.1.2",
|
|
46
46
|
"lodash": "^4.18.1"
|