From 51265548ee395a791583a5b99c38726aab0159a7 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 16:32:41 +0200 Subject: [PATCH 01/13] Update `OrderCreateTypeV5` --- src/types/v5-shared.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/types/v5-shared.ts b/src/types/v5-shared.ts index 7714548..b35fbe5 100644 --- a/src/types/v5-shared.ts +++ b/src/types/v5-shared.ts @@ -95,7 +95,9 @@ export type OrderCreateTypeV5 = /** Order created by Ice berg strategy - web/app. */ | 'CreateByIceBerg' /** Order created by arbitrage - web/app. */ - | 'CreateByArbitrage'; + | 'CreateByArbitrage' + /** Option dynamic delta hedge order - web/app */ + | 'CreateByDdh'; export type OrderCancelTypeV5 = | 'CancelByUser' From bdd20218cd1b389dc398a820de78acd5718867e6 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 16:33:02 +0200 Subject: [PATCH 02/13] Update `ExecTypeV5` --- src/types/v5-shared.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/types/v5-shared.ts b/src/types/v5-shared.ts index b35fbe5..9e60a30 100644 --- a/src/types/v5-shared.ts +++ b/src/types/v5-shared.ts @@ -223,7 +223,10 @@ export type ExecTypeV5 = | 'AdlTrade' | 'Funding' | 'BustTrade' - | 'Settle'; + | 'Settle' + | 'BlockTrade' + | 'MovePosition' + | 'UNKNOWN'; /** * Withdraw type. 0(default): on chain. 1: off chain. 2: all. From 98cf9655a15e937ab77b8f34a5d2878f6c8b1c9c Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 16:33:47 +0200 Subject: [PATCH 03/13] Adds `WSExecutionEventV5` and `WSExecutionV5` interfaces --- src/types/websocket.events.ts | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/types/websocket.events.ts b/src/types/websocket.events.ts index b55d964..bf0a7f4 100644 --- a/src/types/websocket.events.ts +++ b/src/types/websocket.events.ts @@ -1,6 +1,8 @@ import { CategoryV5, + ExecTypeV5, OCOTriggerTypeV5, + OrderCreateTypeV5, OrderRejectReasonV5, OrderSMPTypeV5, OrderSideV5, @@ -96,3 +98,44 @@ export interface WSAccountOrderEventV5 { creationTime: number; data: WSAccountOrderV5[]; } + +export interface WSExecutionV5 { + category: CategoryV5; + symbol: string; + isLeverage: string; + orderId: string; + orderLinkId: string; + side: OrderSideV5; + orderPrice: string; + orderQty: string; + leavesQty: string; + createType: OrderCreateTypeV5; + orderType: OrderTypeV5; + stopOrderType: StopOrderTypeV5; + execFee: string; + execId: string; + execPrice: string; + execQty: string; + execType: ExecTypeV5; + execValue: string; + execTime: string; + isMaker: boolean; + feeRate: string; + tradeIv: string; + markIv: string; + markPrice: string; + indexPrice: string; + underlyingPrice: string; + blockTradeId: string; + closedSize: string; + seq: number; + marketUnit: string; +} + +export interface WSExecutionEventV5 { + id: string; + topic: 'execution'; + creationTime: number; + data: WSExecutionV5[]; + wsKey: WsKey; +} \ No newline at end of file From 9dcf48eb6fa52b4a53057df29c0e59d474b71a40 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 16:53:49 +0200 Subject: [PATCH 04/13] Reorder `WSAccountOrderV5` and add missing parameters `createType` and `marketUnit` --- src/types/websocket.events.ts | 71 ++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/types/websocket.events.ts b/src/types/websocket.events.ts index bf0a7f4..f636adb 100644 --- a/src/types/websocket.events.ts +++ b/src/types/websocket.events.ts @@ -2,6 +2,7 @@ import { CategoryV5, ExecTypeV5, OCOTriggerTypeV5, + OrderCancelTypeV5, OrderCreateTypeV5, OrderRejectReasonV5, OrderSMPTypeV5, @@ -45,58 +46,60 @@ export interface WSOrderbookEventV5 { } export interface WSAccountOrderV5 { - qty: string; - price: string; - symbol: string; + category: CategoryV5; orderId: string; - orderIv: string; - stopLoss: string; - smpGroup: number; + orderLinkId: string; + isLeverage: string; + blockTradeId: string; + symbol: string; + price: string; + qty: string; side: OrderSideV5; - placeType: string; + positionIdx: number; + orderStatus: OrderStatusV5; + createType: OrderCreateTypeV5; + cancelType: OrderCancelTypeV5; + rejectReason?: OrderRejectReasonV5; avgPrice?: string; leavesQty?: string; - isLeverage: string; - cancelType: string; + leavesValue?: string; cumExecQty: string; + cumExecValue: string; cumExecFee: string; - smpOrderId: string; + feeCurrency: string; + timeInForce: OrderTimeInForceV5; + orderType: OrderTypeV5; + stopOrderType: StopOrderTypeV5; + ocoTriggerType?: OCOTriggerTypeV5; + orderIv: string; + marketUnit?: 'baseCoin' | 'quoteCoin'; + triggerPrice: string; takeProfit: string; - reduceOnly: boolean; - orderLinkId: string; - positionIdx: number; + stopLoss: string; + tpslMode?: TPSLModeV5; + tpLimitPrice?: string; + slLimitPrice?: string; tpTriggerBy: string; slTriggerBy: string; + triggerDirection: number; + triggerBy: OrderTriggerByV5; + lastPriceOnCreated: string; + reduceOnly: boolean; + closeOnTrigger: boolean; + placeType: string; + smpType: OrderSMPTypeV5; + smpGroup: number; + smpOrderId: string; createdTime: string; updatedTime: string; - feeCurrency: string; - triggerPrice: string; - category: CategoryV5; - cumExecValue: string; - blockTradeId: string; - leavesValue?: string; - slLimitPrice?: string; - tpLimitPrice?: string; - tpslMode?: TPSLModeV5; - orderType: OrderTypeV5; - smpType: OrderSMPTypeV5; - closeOnTrigger: boolean; - triggerDirection: number; - orderStatus: OrderStatusV5; - lastPriceOnCreated: string; - triggerBy: OrderTriggerByV5; - stopOrderType: StopOrderTypeV5; - timeInForce: OrderTimeInForceV5; - ocoTriggerType?: OCOTriggerTypeV5; - rejectReason?: OrderRejectReasonV5; } export interface WSAccountOrderEventV5 { id: string; - wsKey: WsKey; topic: 'order'; creationTime: number; data: WSAccountOrderV5[]; + wsKey: WsKey; } export interface WSExecutionV5 { From a2f40b6b6ec2806b51393ea2386ed3a096bf2d36 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 17:04:03 +0200 Subject: [PATCH 05/13] Add type guards for ws order events and ws execution events --- src/util/typeGuards.ts | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/util/typeGuards.ts b/src/util/typeGuards.ts index e6de059..c8b42a0 100644 --- a/src/util/typeGuards.ts +++ b/src/util/typeGuards.ts @@ -2,7 +2,7 @@ * Use type guards to narrow down types with minimal efforts. */ -import { WSOrderbookEventV5 } from '../types/websocket.events'; +import { WSAccountOrderEventV5, WSExecutionEventV5, WSOrderbookEventV5 } from '../types/websocket.events'; /** * Type guard to detect a V5 orderbook event (delta & snapshots) @@ -27,3 +27,43 @@ export function isWsOrderbookEventV5( event['topic'].startsWith('orderbook') ); } + +/** + * Type guard to detect a V5 order event. + * + * @param event + * @returns + */ +export function isWsAccountOrderEventV5( + event: unknown, +): event is WSAccountOrderEventV5 { + if ( + typeof event !== 'object' || + !event || + typeof event['topic'] !== 'string' + ) { + return false; + } + + return event['topic'] === 'order'; +} + +/** + * Type guard to detect a V5 execution event. + * + * @param event + * @returns + */ +export function isWsExecutionEventV5( + event: unknown, +): event is WSExecutionEventV5 { + if ( + typeof event !== 'object' || + !event || + typeof event['topic'] !== 'string' + ) { + return false; + } + + return event['topic'] === 'execution'; +} \ No newline at end of file From ed20b625c5d904dcb29e57267a70b48ddf22dc7c Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 17:12:29 +0200 Subject: [PATCH 06/13] Update `WSAccountOrderV5` --- src/types/websocket.events.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/types/websocket.events.ts b/src/types/websocket.events.ts index f636adb..1fbbc6e 100644 --- a/src/types/websocket.events.ts +++ b/src/types/websocket.events.ts @@ -11,6 +11,7 @@ import { OrderTimeInForceV5, OrderTriggerByV5, OrderTypeV5, + PositionIdx, StopOrderTypeV5, TPSLModeV5, } from './v5-shared'; @@ -55,7 +56,7 @@ export interface WSAccountOrderV5 { price: string; qty: string; side: OrderSideV5; - positionIdx: number; + positionIdx: PositionIdx; orderStatus: OrderStatusV5; createType: OrderCreateTypeV5; cancelType: OrderCancelTypeV5; From 400e86a7777ab5fcf776a1a47eeb3fab238f4707 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 17:26:07 +0200 Subject: [PATCH 07/13] Add `PositionStatusV5` and `PositionSideV5` types --- src/types/v5-shared.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/types/v5-shared.ts b/src/types/v5-shared.ts index 9e60a30..4677c9f 100644 --- a/src/types/v5-shared.ts +++ b/src/types/v5-shared.ts @@ -153,6 +153,16 @@ export type StopOrderTypeV5 = */ export type PositionIdx = 0 | 1 | 2; +/** + * Position status. + * + * - 'Normal' + * - 'Liq' in the liquidation progress + * - 'Adl' in the auto-deleverage progress + */ +export type PositionStatusV5 = 'Normal' | 'Liq' | 'Adl'; +export type PositionSideV5 = 'Buy' | 'Sell'; + export type OptionTypeV5 = 'Call' | 'Put'; /** From ccaae1be1bacec5faeafaa77f8af1b655a027549 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 17:26:34 +0200 Subject: [PATCH 08/13] Adds `WSPositionEventV5` and `WSPositionV5` interfaces --- src/types/websocket.events.ts | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/types/websocket.events.ts b/src/types/websocket.events.ts index 1fbbc6e..c9da412 100644 --- a/src/types/websocket.events.ts +++ b/src/types/websocket.events.ts @@ -12,8 +12,11 @@ import { OrderTriggerByV5, OrderTypeV5, PositionIdx, + PositionSideV5, + PositionStatusV5, StopOrderTypeV5, TPSLModeV5, + TradeModeV5, } from './v5-shared'; import { WsKey } from './websockets'; @@ -46,6 +49,55 @@ export interface WSOrderbookEventV5 { wsKey: WsKey; } +export interface WSPositionV5 { + category: string; + symbol: string; + side: PositionSideV5; + size: string; + positionIdx: PositionIdx; + tradeMode: TradeModeV5; + positionValue: string; + riskId: number; + riskLimitValue: string; + entryPrice: string; + markPrice: string; + leverage: string; + positionBalance: string; + autoAddMargin: number; + positionMM: string; + positionIM: string; + liqPrice: string; + bustPrice: string; + tpslMode: string; + takeProfit: string; + stopLoss: string; + trailingStop: string; + unrealisedPnl: string; + curRealisedPnl: string; + sessionAvgPrice: string; + delta: string; + gamma: string; + vega: string; + theta: string; + cumRealisedPnl: string; + positionStatus: PositionStatusV5; + adlRankIndicator: number; + isReduceOnly: boolean; + mmrSysUpdatedTime: string; + leverageSysUpdatedTime: string; + createdTime: string; + updatedTime: string; + seq: number; +} + +export interface WSPositionEventV5 { + id: string; + topic: 'position'; + creationTime: number; + data: WSPositionV5[]; + wsKey: WsKey; +} + export interface WSAccountOrderV5 { category: CategoryV5; orderId: string; From 12b5b0a41812add4087fcdcdc3228dfcc9dd8927 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 17:28:28 +0200 Subject: [PATCH 09/13] Add type guards for ws position events --- src/util/typeGuards.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/util/typeGuards.ts b/src/util/typeGuards.ts index c8b42a0..44308f8 100644 --- a/src/util/typeGuards.ts +++ b/src/util/typeGuards.ts @@ -2,7 +2,7 @@ * Use type guards to narrow down types with minimal efforts. */ -import { WSAccountOrderEventV5, WSExecutionEventV5, WSOrderbookEventV5 } from '../types/websocket.events'; +import { WSAccountOrderEventV5, WSExecutionEventV5, WSOrderbookEventV5, WSPositionEventV5 } from '../types/websocket.events'; /** * Type guard to detect a V5 orderbook event (delta & snapshots) @@ -28,6 +28,26 @@ export function isWsOrderbookEventV5( ); } +/** + * Type guard to detect a V5 position event. + * + * @param event + * @returns + */ +export function isWsPositionEventV5( + event: unknown, +): event is WSPositionEventV5 { + if ( + typeof event !== 'object' || + !event || + typeof event['topic'] !== 'string' + ) { + return false; + } + + return event['topic'] === 'position'; +} + /** * Type guard to detect a V5 order event. * From 06190843384608116da89de022724506c2efbd4c Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 17:29:37 +0200 Subject: [PATCH 10/13] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 719472f..77b759b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bybit-api", - "version": "3.10.1", + "version": "3.10.2", "description": "Complete & robust Node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & strong end to end tests.", "main": "lib/index.js", "types": "lib/index.d.ts", From 866c555fd1dd148dbc3f4462882a637d94ed77b6 Mon Sep 17 00:00:00 2001 From: William Date: Sun, 28 Apr 2024 14:46:12 +0200 Subject: [PATCH 11/13] Update `PositionSideV5` type --- src/types/v5-shared.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/v5-shared.ts b/src/types/v5-shared.ts index 4677c9f..1eb3864 100644 --- a/src/types/v5-shared.ts +++ b/src/types/v5-shared.ts @@ -161,7 +161,7 @@ export type PositionIdx = 0 | 1 | 2; * - 'Adl' in the auto-deleverage progress */ export type PositionStatusV5 = 'Normal' | 'Liq' | 'Adl'; -export type PositionSideV5 = 'Buy' | 'Sell'; +export type PositionSideV5 = 'Buy' | 'Sell' | 'None' | ''; export type OptionTypeV5 = 'Call' | 'Put'; From 510eb35fea5b88675a45bc553530bdfeba9db6d3 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 30 Apr 2024 22:16:27 +0200 Subject: [PATCH 12/13] Introduce `WSPublicTopicEventV5` and `WSPrivateTopicEventV5` --- src/types/websocket.events.ts | 71 ++++++++++++++++------------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/src/types/websocket.events.ts b/src/types/websocket.events.ts index c9da412..cb11da5 100644 --- a/src/types/websocket.events.ts +++ b/src/types/websocket.events.ts @@ -20,25 +20,15 @@ import { } from './v5-shared'; import { WsKey } from './websockets'; -export interface WSOrderbookEventV5 { - topic: string; +export interface WSPublicTopicEventV5 { + id?: string; + topic: TTopic; + type: TType; + /** Cross sequence */ + cs?: number; /** Event timestamp */ ts: number; - type: 'delta' | 'snapshot'; - data: { - /** Symbol */ - s: string; - /** [price, qty][] */ - b: [string, string][]; - /** [price, qty][] */ - a: [string, string][]; - /** Update ID */ - u: number; - /** - * Cross sequence - */ - seq: number; - }; + data: TData; /** * matching engine timestamp (correlated with T from public trade channel) */ @@ -49,6 +39,29 @@ export interface WSOrderbookEventV5 { wsKey: WsKey; } +export interface WSPrivateTopicEventV5 { + id?: string; + topic: TTopic; + creationTime: number; + data: TData; + wsKey: WsKey; +} + +export interface WSOrderbookV5 { + /** Symbol */ + s: string; + /** [price, qty][] */ + b: [string, string][]; + /** [price, qty][] */ + a: [string, string][]; + /** Update ID */ + u: number; + /** Cross sequence */ + seq: number; +} + +export type WSOrderbookEventV5 = WSPublicTopicEventV5; + export interface WSPositionV5 { category: string; symbol: string; @@ -90,13 +103,7 @@ export interface WSPositionV5 { seq: number; } -export interface WSPositionEventV5 { - id: string; - topic: 'position'; - creationTime: number; - data: WSPositionV5[]; - wsKey: WsKey; -} +export type WSPositionEventV5 = WSPrivateTopicEventV5<'position', WSPositionV5[]>; export interface WSAccountOrderV5 { category: CategoryV5; @@ -147,13 +154,7 @@ export interface WSAccountOrderV5 { updatedTime: string; } -export interface WSAccountOrderEventV5 { - id: string; - topic: 'order'; - creationTime: number; - data: WSAccountOrderV5[]; - wsKey: WsKey; -} +export type WSAccountOrderEventV5 = WSPrivateTopicEventV5<'order', WSAccountOrderV5[]>; export interface WSExecutionV5 { category: CategoryV5; @@ -188,10 +189,4 @@ export interface WSExecutionV5 { marketUnit: string; } -export interface WSExecutionEventV5 { - id: string; - topic: 'execution'; - creationTime: number; - data: WSExecutionV5[]; - wsKey: WsKey; -} \ No newline at end of file +export type WSExecutionEventV5 = WSPrivateTopicEventV5<'execution', WSExecutionV5[]>; \ No newline at end of file From d4bd0859e16449593c12925893ac92d46baad06b Mon Sep 17 00:00:00 2001 From: William Date: Wed, 1 May 2024 04:32:45 +0200 Subject: [PATCH 13/13] Update `PositionV5` and `AddOrReduceMarginResultV5` --- src/types/response/v5-position.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/types/response/v5-position.ts b/src/types/response/v5-position.ts index 66fd0a6..6c66be1 100644 --- a/src/types/response/v5-position.ts +++ b/src/types/response/v5-position.ts @@ -4,6 +4,8 @@ import { OrderSideV5, OrderTypeV5, PositionIdx, + PositionSideV5, + PositionStatusV5, StopOrderTypeV5, TPSLModeV5, TradeModeV5, @@ -14,16 +16,16 @@ export interface PositionV5 { riskId: number; riskLimitValue: string; symbol: string; - side: 'Buy' | 'Sell' | 'None'; + side: PositionSideV5; size: string; avgPrice: string; positionValue: string; tradeMode: TradeModeV5; autoAddMargin?: number; - positionStatus: 'Normal' | 'Liq' | 'Adl'; + positionStatus: PositionStatusV5; leverage?: string; markPrice: string; - liqPrice: string; + liqPrice: string | ''; bustPrice?: string; positionIM?: string; positionMM?: string; @@ -31,10 +33,21 @@ export interface PositionV5 { takeProfit?: string; stopLoss?: string; trailingStop?: string; + sessionAvgPrice: string | ''; + delta?: string; + gamma?: string; + vega?: string; + theta?: string; unrealisedPnl: string; + curRealisedPnl: string; cumRealisedPnl: string; + adlRankIndicator: number; + isReduceOnly: boolean; + mmrSysUpdatedTime: string | ''; + leverageSysUpdatedTime: string | ''; createdTime: string; updatedTime: string; + seq: number; } export interface SetRiskLimitResultV5 { @@ -57,7 +70,7 @@ export interface AddOrReduceMarginResultV5 { positionValue: string; leverage: string; autoAddMargin: 0 | 1; - positionStatus: 'Normal' | 'Liq' | 'Adl'; + positionStatus: PositionStatusV5; positionIM: string; positionMM: string; takeProfit: string;