From 7b6a413bc181dd75d540561367dd45fbaace6462 Mon Sep 17 00:00:00 2001 From: MiniTiago Date: Mon, 9 Nov 2026 15:37:34 +0000 Subject: [PATCH 1/6] docs: correct Bybit order and stream examples --- sdks/bybit/examples/first-order.ts | 23 +++++++++++++++++++---- sdks/bybit/examples/private-stream.ts | 6 ++---- sdks/bybit/examples/stream.ts | 18 +++++++++++------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/sdks/bybit/examples/first-order.ts b/sdks/bybit/examples/first-order.ts index f403c8de..d4df4d47 100644 --- a/sdks/bybit/examples/first-order.ts +++ b/sdks/bybit/examples/first-order.ts @@ -16,10 +16,25 @@ async function printOrderStatus(client: RestClientV5, orderId: string) { (item) => item.orderId === orderId, ); if (!order) { - const historyResponse = await client.getHistoricOrders(query); - order = historyResponse.result.list.find( - (item) => item.orderId === orderId, - ); + // History excludes these terminal states unless requested explicitly. + const historyStatuses = [ + undefined, + 'Cancelled', + 'Rejected', + 'Deactivated', + ] as const; + for (const orderStatus of historyStatuses) { + const historyResponse = await client.getHistoricOrders({ + ...query, + orderStatus, + }); + order = historyResponse.result.list.find( + (item) => item.orderId === orderId, + ); + if (order) { + break; + } + } } if (!order) { throw new Error( diff --git a/sdks/bybit/examples/private-stream.ts b/sdks/bybit/examples/private-stream.ts index 7c33125a..5734f7fd 100644 --- a/sdks/bybit/examples/private-stream.ts +++ b/sdks/bybit/examples/private-stream.ts @@ -36,12 +36,10 @@ function main() { if (stopped) { return; } - if (message.op !== 'subscribe') { - console.log('Private stream response:', message); - } else if (message.success === true) { + if (message.op === 'subscribe' && message.success === true) { console.log('Subscribed to order.linear. Waiting for order changes.'); } else { - console.error('Private subscription rejected:', message); + console.log('Private stream response:', message); } }); ws.on('update', (message) => { diff --git a/sdks/bybit/examples/stream.ts b/sdks/bybit/examples/stream.ts index a3cd908f..f30efb01 100644 --- a/sdks/bybit/examples/stream.ts +++ b/sdks/bybit/examples/stream.ts @@ -4,7 +4,9 @@ const ws = new WebsocketClient({}); let stopped = false; let timer: ReturnType | undefined; function stop() { - if (stopped) return; + if (stopped) { + return; + } stopped = true; clearTimeout(timer); ws.closeAll(); @@ -12,15 +14,17 @@ function stop() { process.once('SIGINT', stop); process.once('SIGTERM', stop); ws.on('update', (message) => console.log(message)); -ws.on('exception', () => { - console.error('WebSocket exception. Check the topic, environment and connection.'); -}); +ws.on('response', (message) => console.log('Subscription response:', message)); +ws.on('exception', (error) => console.error('WebSocket exception:', error)); async function main() { + // Subscription acceptance is reported by response, and rejection by exception. await Promise.all(ws.subscribeV5('tickers.BTCUSDT', 'spot')); - if (!stopped) timer = setTimeout(stop, 30000); + if (!stopped) { + timer = setTimeout(stop, 30000); + } } -main().catch(() => { - console.error('Subscription failed.'); +main().catch((error) => { + console.error('Could not start the stream:', error); process.exitCode = 1; stop(); }); From ae55113011e58c3655a056ce016844ebf26e083e Mon Sep 17 00:00:00 2001 From: MiniTiago Date: Mon, 9 Nov 2026 15:38:05 +0000 Subject: [PATCH 2/6] docs: clarify Bybit status and subscription behavior --- sdks/bybit/docs/guides/orders.md | 2 +- sdks/bybit/docs/guides/troubleshooting.md | 2 +- sdks/bybit/docs/guides/websockets.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdks/bybit/docs/guides/orders.md b/sdks/bybit/docs/guides/orders.md index 27f24e5a..33c7be6c 100644 --- a/sdks/bybit/docs/guides/orders.md +++ b/sdks/bybit/docs/guides/orders.md @@ -56,7 +56,7 @@ Query the same order: node --env-file=.env first-order.ts status ``` -The status command reads [`getActiveOrders()`](/api/classes/RestClientV5#getactiveorders), then [`getHistoricOrders()`](/api/classes/RestClientV5#gethistoricorders) if the order is absent. Inspect `orderStatus`, `cumExecQty` and `leavesQty`. A `New` or `PartiallyFilled` order can have remaining quantity to cancel: +The status command reads [`getActiveOrders()`](/api/classes/RestClientV5#getactiveorders), then [`getHistoricOrders()`](/api/classes/RestClientV5#gethistoricorders) if the order is absent. The history fallback explicitly checks `Cancelled`, `Rejected` and `Deactivated`, which Bybit excludes from unfiltered recent history from 3 December 2026. Inspect `orderStatus`, `cumExecQty` and `leavesQty`. A `New` or `PartiallyFilled` order can have remaining quantity to cancel: ```sh node --env-file=.env first-order.ts cancel diff --git a/sdks/bybit/docs/guides/troubleshooting.md b/sdks/bybit/docs/guides/troubleshooting.md index 1c00aa7e..b17b9e11 100644 --- a/sdks/bybit/docs/guides/troubleshooting.md +++ b/sdks/bybit/docs/guides/troubleshooting.md @@ -12,7 +12,7 @@ description: "Diagnose Bybit installation, authentication, response and WebSocke | Promise resolves, but the operation failed | Inspect `retCode` and `retMsg`; default REST behaviour does not throw on nonzero `retCode`. | | Empty ticker list | Check the symbol and `category`. Verify the instrument exists in that market and environment. | | API key or permission error | Match the key to live, Testnet or demo; check account permissions and IP restrictions. | -| Timestamp or receive-window error | Synchronize the host clock, check network latency, and review `recv_window`. | +| Timestamp or receive-window error | Synchronize the host clock, check network latency, and review `recv_window` for REST or `recvWindow` for WebSocket authentication. | | HTTP or rate-limit rejection | Inspect HTTP status and exchange code separately; slow down using the current endpoint limits. | | No WebSocket market data | Register an `update` listener before subscribing; inspect `exception`, topic and category. | | Old data after a reconnect | Reconcile application state and apply the topic's snapshot/delta rules. | diff --git a/sdks/bybit/docs/guides/websockets.md b/sdks/bybit/docs/guides/websockets.md index f6e41a23..9f785ddf 100644 --- a/sdks/bybit/docs/guides/websockets.md +++ b/sdks/bybit/docs/guides/websockets.md @@ -15,7 +15,7 @@ Save this as `stream.ts` in a TypeScript project. It prints updates for 30 secon ## Subscription results and messages -Await the subscription promise and catch failures. Market data arrives on `update`; `response` carries subscription replies. +`subscribeV5()` returns an array of promises, so use `Promise.all()` to await them. With the default settings, completion does not mean Bybit accepted the subscription. Successful subscription replies arrive on `response`; rejected requests arrive on `exception`. Market data arrives on `update`. ## Recover after a disconnect From 287b18c9421f2dcbfb1631ce5b0a9bf8209648c6 Mon Sep 17 00:00:00 2001 From: MiniTiago Date: Mon, 9 Nov 2026 15:55:45 +0000 Subject: [PATCH 3/6] docs: simplify cancellation status lookup --- sdks/bybit/examples/first-order.ts | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/sdks/bybit/examples/first-order.ts b/sdks/bybit/examples/first-order.ts index d4df4d47..b94b390d 100644 --- a/sdks/bybit/examples/first-order.ts +++ b/sdks/bybit/examples/first-order.ts @@ -10,31 +10,20 @@ function requiredEnvVar(name: string): string { async function printOrderStatus(client: RestClientV5, orderId: string) { const query = { category: 'linear' as const, symbol: 'BTCUSDT', orderId }; - // Realtime results include recent closed orders; use history if absent. + // Realtime results include recent closed orders. const activeResponse = await client.getActiveOrders(query); let order = activeResponse.result.list.find( (item) => item.orderId === orderId, ); if (!order) { - // History excludes these terminal states unless requested explicitly. - const historyStatuses = [ - undefined, - 'Cancelled', - 'Rejected', - 'Deactivated', - ] as const; - for (const orderStatus of historyStatuses) { - const historyResponse = await client.getHistoricOrders({ - ...query, - orderStatus, - }); - order = historyResponse.result.list.find( - (item) => item.orderId === orderId, - ); - if (order) { - break; - } - } + // Confirm cancellation if the order is no longer in realtime results. + const historyResponse = await client.getHistoricOrders({ + ...query, + orderStatus: 'Cancelled', + }); + order = historyResponse.result.list.find( + (item) => item.orderId === orderId, + ); } if (!order) { throw new Error( From e583d4011d1284a653f5bc37c7a8d5e1b2d1ccda Mon Sep 17 00:00:00 2001 From: MiniTiago Date: Mon, 9 Nov 2026 15:56:02 +0000 Subject: [PATCH 4/6] docs: clarify the cancellation-only history fallback --- sdks/bybit/docs/guides/orders.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdks/bybit/docs/guides/orders.md b/sdks/bybit/docs/guides/orders.md index 33c7be6c..5b99b68c 100644 --- a/sdks/bybit/docs/guides/orders.md +++ b/sdks/bybit/docs/guides/orders.md @@ -56,7 +56,7 @@ Query the same order: node --env-file=.env first-order.ts status ``` -The status command reads [`getActiveOrders()`](/api/classes/RestClientV5#getactiveorders), then [`getHistoricOrders()`](/api/classes/RestClientV5#gethistoricorders) if the order is absent. The history fallback explicitly checks `Cancelled`, `Rejected` and `Deactivated`, which Bybit excludes from unfiltered recent history from 3 December 2026. Inspect `orderStatus`, `cumExecQty` and `leavesQty`. A `New` or `PartiallyFilled` order can have remaining quantity to cancel: +The status command reads [`getActiveOrders()`](/api/classes/RestClientV5#getactiveorders). If the order is absent, it makes one [`getHistoricOrders()`](/api/classes/RestClientV5#gethistoricorders) request with `orderStatus: 'Cancelled'` to check for cancellation. Inspect `orderStatus`, `cumExecQty` and `leavesQty`. A `New` or `PartiallyFilled` order can have remaining quantity to cancel: ```sh node --env-file=.env first-order.ts cancel @@ -73,6 +73,8 @@ A timeout or lost response can happen after Bybit accepts a request. The example If you received an `orderId`, query `status` with that ID and the same account. If submission timed out before returning an ID, inspect realtime orders and order history for BTCUSDT, and reconcile executions and positions using the request time, side, price and quantity. Avoid resubmitting until the outcome is resolved. +The history fallback is specific to cancellation, not a search across every order state. When querying other historical states, set the corresponding `orderStatus`; from 3 December 2026, unfiltered recent history excludes `Cancelled`, `Rejected` and `Deactivated`. + An empty result is inconclusive: recent closed orders can disappear from the realtime endpoint after a service restart, and history can lag. See [open and closed orders](https://bybit-exchange.github.io/docs/v5/order/open-order). Stopping the process or closing a WebSocket does not cancel an open order. From 4e3b2be54ce11e456c2727dc402cf4e50df356f1 Mon Sep 17 00:00:00 2001 From: MiniTiago Date: Mon, 9 Nov 2026 16:02:17 +0000 Subject: [PATCH 5/6] docs: use one request to read order status --- sdks/bybit/examples/first-order.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/sdks/bybit/examples/first-order.ts b/sdks/bybit/examples/first-order.ts index b94b390d..c1e23fd3 100644 --- a/sdks/bybit/examples/first-order.ts +++ b/sdks/bybit/examples/first-order.ts @@ -12,19 +12,9 @@ async function printOrderStatus(client: RestClientV5, orderId: string) { const query = { category: 'linear' as const, symbol: 'BTCUSDT', orderId }; // Realtime results include recent closed orders. const activeResponse = await client.getActiveOrders(query); - let order = activeResponse.result.list.find( + const order = activeResponse.result.list.find( (item) => item.orderId === orderId, ); - if (!order) { - // Confirm cancellation if the order is no longer in realtime results. - const historyResponse = await client.getHistoricOrders({ - ...query, - orderStatus: 'Cancelled', - }); - order = historyResponse.result.list.find( - (item) => item.orderId === orderId, - ); - } if (!order) { throw new Error( 'Order not found. Check Testnet order history before placing another order.', From 35eba29a027225fb409e90919ec39bd5eb313571 Mon Sep 17 00:00:00 2001 From: MiniTiago Date: Mon, 9 Nov 2026 16:02:36 +0000 Subject: [PATCH 6/6] docs: separate recent order status from historical recovery --- sdks/bybit/docs/guides/orders.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdks/bybit/docs/guides/orders.md b/sdks/bybit/docs/guides/orders.md index 5b99b68c..2442e7cb 100644 --- a/sdks/bybit/docs/guides/orders.md +++ b/sdks/bybit/docs/guides/orders.md @@ -56,7 +56,7 @@ Query the same order: node --env-file=.env first-order.ts status ``` -The status command reads [`getActiveOrders()`](/api/classes/RestClientV5#getactiveorders). If the order is absent, it makes one [`getHistoricOrders()`](/api/classes/RestClientV5#gethistoricorders) request with `orderStatus: 'Cancelled'` to check for cancellation. Inspect `orderStatus`, `cumExecQty` and `leavesQty`. A `New` or `PartiallyFilled` order can have remaining quantity to cancel: +The status command makes one [`getActiveOrders()`](/api/classes/RestClientV5#getactiveorders) request using the returned order ID. Despite the method name, this also returns recent closed orders. Inspect `orderStatus`, `cumExecQty` and `leavesQty`. A `New` or `PartiallyFilled` order can have remaining quantity to cancel: ```sh node --env-file=.env first-order.ts cancel @@ -73,9 +73,9 @@ A timeout or lost response can happen after Bybit accepts a request. The example If you received an `orderId`, query `status` with that ID and the same account. If submission timed out before returning an ID, inspect realtime orders and order history for BTCUSDT, and reconcile executions and positions using the request time, side, price and quantity. Avoid resubmitting until the outcome is resolved. -The history fallback is specific to cancellation, not a search across every order state. When querying other historical states, set the corresponding `orderStatus`; from 3 December 2026, unfiltered recent history excludes `Cancelled`, `Rejected` and `Deactivated`. +For older records, or closed orders no longer available after a service restart, use [`getHistoricOrders()`](/api/classes/RestClientV5#gethistoricorders). Set `orderStatus: 'Cancelled'` when looking for a cancelled order; from 3 December 2026, unfiltered recent history excludes `Cancelled`, `Rejected` and `Deactivated`. -An empty result is inconclusive: recent closed orders can disappear from the realtime endpoint after a service restart, and history can lag. See [open and closed orders](https://bybit-exchange.github.io/docs/v5/order/open-order). +An empty result is inconclusive, and history can lag behind order updates. See [open and closed orders](https://bybit-exchange.github.io/docs/v5/order/open-order). Stopping the process or closing a WebSocket does not cancel an open order.