diff --git a/src/rest-client-v5.ts b/src/rest-client-v5.ts index a535b9a..ebe96f4 100644 --- a/src/rest-client-v5.ts +++ b/src/rest-client-v5.ts @@ -18,23 +18,14 @@ import { GetRiskLimitParamsV5, GetTickersParamsV5, HistoricalVolatilityV5, - IndexPriceKlineResponseV5, InstrumentInfoV5, InsuranceResponseV5, - KlineResponseV5, - MarkPriceKlineResponseV5, - AccountOrdersResultV5, OpenInterestResponseV5, - OptionDeliveryPriceResponseV5, OrderbookResponseV5, OrderParamsV5, - PaginatedListV5, - PremiumIndexPriceKlineResponse, + CursorListV5, PublicTradeV5, RiskLimitV5, - TickersLinearInverseResponseV5, - TickersOptionResponseV5, - TickersSpotResponseV5, AmendOrderParamsV5, CancelOrderParamsV5, GetAccountOrdersParams, @@ -50,12 +41,24 @@ import { OrderSideV5, SpotBorrowCheckResult, APIResponseV3, + PositionInfoParamsV5, + CategoryCursorListV5, + PositionV5, + AccountOrderV5, + OptionDeliveryPriceV5, + CategorySymbolListV5, + OHLCV5, + KlineV5, + TickerSpotV5, + TickerOptionV5, + TickerLinearInverseV5, } from './types'; import { REST_CLIENT_TYPE_ENUM } from './util'; import BaseRestClient from './util/BaseRestClient'; /** * REST API client for V5 REST APIs + * * https://bybit-exchange.github.io/docs/v5/intro */ export class RestClientV5 extends BaseRestClient { @@ -82,57 +85,72 @@ export class RestClientV5 extends BaseRestClient { /** * Query the kline data. Charts are returned in groups based on the requested interval. + * * Covers: Spot / Linear contract / Inverse contract */ getKline( params: GetKlineParamsV5 - ): Promise> { + ): Promise< + APIResponseV3WithTime< + CategorySymbolListV5 + > + > { return this.get(`/v5/market/kline`, params); } /** * Query the mark price kline data. Charts are returned in groups based on the requested interval. + * * Covers: Linear contract / Inverse contract */ getMarkPriceKline( params: GetMarkPriceKlineParamsV5 - ): Promise> { + ): Promise< + APIResponseV3WithTime> + > { return this.get(`/v5/market/mark-price-kline`, params); } /** * Query the index price kline data. Charts are returned in groups based on the requested interval. + * * Covers: Linear contract / Inverse contract */ getIndexPriceKline( params: GetIndexPriceKlineParamsV5 - ): Promise> { + ): Promise< + APIResponseV3WithTime> + > { return this.get(`/v5/market/index-price-kline`, params); } /** * Retrieve the premium index price kline data. Charts are returned in groups based on the requested interval. + * * Covers: Linear contract */ getPremiumIndexPriceKline( params: GetPremiumIndexPriceKlineParams - ): Promise> { + ): Promise>> { return this.get(`/v5/market/premium-index-price-kline`, params); } /** * Query a list of instruments of online trading pair. + * * Covers: Spot / Linear contract / Inverse contract / Option + * * Note: Spot does not support pagination, so limit & cursor are invalid. */ getInstrumentsInfo( params: GetInstrumentsInfoParamsV5 - ): Promise>> { + ): Promise>> { return this.get(`/v5/market/instruments-info`, params); } /** * Query orderbook data + * * Covers: Spot / Linear contract / Inverse contract / Option */ getOrderbook( @@ -143,15 +161,16 @@ export class RestClientV5 extends BaseRestClient { /** * Query the latest price snapshot, best bid/ask price, and trading volume in the last 24 hours. + * * Covers: Spot / Linear contract / Inverse contract / Option */ getTickers( params: GetTickersParamsV5 ): Promise< APIResponseV3WithTime< - | TickersLinearInverseResponseV5 - | TickersOptionResponseV5 - | TickersSpotResponseV5 + | CategoryListV5 + | CategoryListV5 + | CategoryListV5 > > { return this.get(`/v5/market/tickers`, params); @@ -159,13 +178,14 @@ export class RestClientV5 extends BaseRestClient { /** * Query historical funding rate. Each symbol has a different funding interval. + * * Covers: Linear contract / Inverse perpetual */ getFundingRateHistory( params: GetFundingRateHistoryParamsV5 ): Promise< APIResponseV3WithTime< - CategoryListV5 + CategoryListV5 > > { return this.get(`/v5/market/funding/history`, params); @@ -173,16 +193,20 @@ export class RestClientV5 extends BaseRestClient { /** * Query recent public trading data in Bybit. + * * Covers: Spot / Linear contract / Inverse contract / Option */ getPublicTradingHistory( params: GetPublicTradingHistoryParamsV5 - ): Promise>> { + ): Promise< + APIResponseV3WithTime> + > { return this.get(`/v5/market/recent-trade`, params); } /** * Get open interest of each symbol. + * * Covers: Linear contract / Inverse contract */ getOpenInterest( @@ -198,7 +222,7 @@ export class RestClientV5 extends BaseRestClient { getHistoricalVolatility( params: GetHistoricalVolatilityParamsV5 ): Promise< - APIResponseV3WithTime> + APIResponseV3WithTime> > { return this.get(`/v5/market/historical-volatility`, params); } @@ -214,23 +238,27 @@ export class RestClientV5 extends BaseRestClient { /** * Query risk limit of futures + * * Covers: Linear contract / Inverse contract */ getRiskLimit( params?: GetRiskLimitParamsV5 ): Promise< - APIResponseV3WithTime> + APIResponseV3WithTime> > { return this.get(`/v5/market/risk-limit`, params); } /** * Get the delivery price for option + * * Covers: Option */ getOptionDeliveryPrice( params: GetOptionDeliveryPriceParamsV5 - ): Promise> { + ): Promise< + APIResponseV3WithTime> + > { return this.get(`/v5/market/delivery-price`, params); } @@ -263,7 +291,7 @@ export class RestClientV5 extends BaseRestClient { */ getActiveOrders( params: GetAccountOrdersParams - ): Promise> { + ): Promise>> { return this.getPrivate('/v5/order/realtime', params); } @@ -275,17 +303,20 @@ export class RestClientV5 extends BaseRestClient { /** * Query order history. As order creation/cancellation is asynchronous, the data returned from this endpoint may delay. + * * If you want to get real-time order information, you could query this endpoint or rely on the websocket stream (recommended). */ getHistoricOrders( params: GetAccountOrdersParams - ): Promise> { + ): Promise>> { return this.getPrivate(`/v5/order/history`, params); } /** * This endpoint allows you to place more than one order in a single request. Covers: option (unified account). + * * Make sure you have sufficient funds in your account when placing an order. Once an order is placed, according to the funds required by the order, the funds in your account will be frozen by the corresponding amount during the life cycle of the order. + * * A maximum of 20 orders can be placed per request. The returned data list is divided into two lists. The first list indicates whether or not the order creation was successful and the second list details the created order information. The structure of the two lists are completely consistent. */ batchSubmitOrders( @@ -300,7 +331,9 @@ export class RestClientV5 extends BaseRestClient { /** * This endpoint allows you to amend more than one open order in a single request. Covers: option (unified account). + * * You can modify unfilled or partially filled orders. Conditional orders are not supported. + * * A maximum of 20 orders can be amended per request. */ batchAmendOrders( @@ -317,7 +350,9 @@ export class RestClientV5 extends BaseRestClient { /** * This endpoint allows you to cancel more than one open order in a single request. Covers: option (unified account). + * * You must specify orderId or orderLinkId. If orderId and orderLinkId is not matched, the system will process orderId first. + * * You can cancel unfilled or partially filled orders. A maximum of 20 orders can be cancelled per request. */ batchCancelOrders( @@ -334,6 +369,7 @@ export class RestClientV5 extends BaseRestClient { /** * Query the qty and amount of borrowable coins in spot account. + * * Covers: Spot (Unified Account) */ getSpotBorrowCheck( @@ -349,6 +385,7 @@ export class RestClientV5 extends BaseRestClient { /** * This endpoint allows you to set the disconnection protect time window. Covers: option (unified account). + * * If you need to turn it on/off, you can contact your client manager for consultation and application. The default time window is 10 seconds. */ setDisconnectCancelAllWindow( @@ -361,6 +398,25 @@ export class RestClientV5 extends BaseRestClient { }); } + /** + * + * Position APIs + * + */ + + /** + * Query real-time position data, such as position size, cumulative realizedPNL. + * + * Unified account covers: Linear contract / Options + * + * Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures + */ + getPositionInfo( + params: PositionInfoParamsV5 + ): Promise>> { + return this.getPrivate('/v5/position/list', params); + } + // // // diff --git a/src/types/request/index.ts b/src/types/request/index.ts index 35a7a46..46749cf 100644 --- a/src/types/request/index.ts +++ b/src/types/request/index.ts @@ -1,5 +1,6 @@ export * from './account-asset'; export * from './copy-trading'; +export * from './contract'; export * from './linear'; export * from './inverse'; export * from './spot'; @@ -8,5 +9,5 @@ export * from './usdc-options'; export * from './usdc-shared'; export * from './unified-margin'; export * from './v5-market'; -export * from './contract'; +export * from './v5-position'; export * from './v5-trade'; diff --git a/src/types/request/v5-position.ts b/src/types/request/v5-position.ts new file mode 100644 index 0000000..91e0c1f --- /dev/null +++ b/src/types/request/v5-position.ts @@ -0,0 +1,10 @@ +import { CategoryV5 } from '../v5-shared'; + +export interface PositionInfoParamsV5 { + category: CategoryV5; + symbol?: string; + baseCoin?: string; + settleCoin?: string; + limit?: number; + cursor?: string; +} diff --git a/src/types/response/index.ts b/src/types/response/index.ts index 7322cae..d93addb 100644 --- a/src/types/response/index.ts +++ b/src/types/response/index.ts @@ -5,4 +5,5 @@ export * from './spot'; export * from './usdt-perp'; export * from './unified-margin'; export * from './v5-market'; +export * from './v5-position'; export * from './v5-trade'; diff --git a/src/types/response/v5-market.ts b/src/types/response/v5-market.ts index f53d863..5c00194 100644 --- a/src/types/response/v5-market.ts +++ b/src/types/response/v5-market.ts @@ -1,64 +1,37 @@ -import { CategoryV5 } from '../v5-shared'; - -/** - * Next page cursor does not exist for spot! - */ -export interface PaginatedListV5 { - nextPageCursor: string; - list: T[]; -} - -export interface CategoryListV5 { - category: TCategory; - list: T[]; -} - /** * OHLCVT candle used by v5 APIs -> list[0]: startTime string Start time of the candle (ms) -> list[1]: openPrice string Open price -> list[2]: highPrice string Highest price -> list[3]: lowPrice string Lowest price -> list[4]: closePrice string Close price. Is the last traded price when the candle is not closed -> list[5]: volume string Trade volume. Unit of contract: pieces of contract. Unit of spot: quantity of coins -> list[6]: turnover string Turnover. Unit of figure: quantity of quota coin + * + * > list[0]: startTime string Start time of the candle (ms) + * + * > list[1]: openPrice string Open price + * + * > list[2]: highPrice string Highest price + * + * > list[3]: lowPrice string Lowest price + * + * > list[4]: closePrice string Close price. Is the last traded price when the candle is not closed + * + * > list[5]: volume string Trade volume. Unit of contract: pieces of contract. Unit of spot: quantity of coins + * + * > list[6]: turnover string Turnover. Unit of figure: quantity of quota coin */ export type KlineV5 = [string, string, string, string, string, string, string]; -export interface KlineResponseV5 { - category: 'spot' | 'linear' | 'inverse'; - symbol: string; - list: KlineV5[]; -} - /** * OHLC candle used by v5 APIs -> list[0]: startTime string Start time of the candle (ms) -> list[1]: openPrice string Open price -> list[2]: highPrice string Highest price -> list[3]: lowPrice string Lowest price -> list[4]: closePrice string Close price. Is the last traded price when the candle is not closed + * + * > list[0]: startTime string Start time of the candle (ms) + * + * > list[1]: openPrice string Open price + * + * > list[2]: highPrice string Highest price + * + * > list[3]: lowPrice string Lowest price + * + * > list[4]: closePrice string Close price. Is the last traded price when the candle is not closed */ export type OHLCV5 = [string, string, string, string, string]; -export interface MarkPriceKlineResponseV5 { - category: 'linear' | 'inverse'; - symbol: string; - list: OHLCV5[]; -} - -export interface IndexPriceKlineResponseV5 { - category: 'linear' | 'inverse'; - symbol: string; - list: OHLCV5[]; -} - -export interface PremiumIndexPriceKlineResponse { - category: 'linear'; - symbol: string; - list: OHLCV5[]; -} - export interface LinearInverseInstrumentInfoV5 { category: 'linear' | 'inverse'; symbol: string; @@ -208,21 +181,6 @@ export interface TickerSpotV5 { usdIndexPrice: string; } -export interface TickersSpotResponseV5 { - category: 'spot'; - list: TickerSpotV5[]; -} - -export interface TickersLinearInverseResponseV5 { - category: 'linear' | 'inverse'; - list: TickerLinearInverseV5[]; -} - -export interface TickersOptionResponseV5 { - category: 'option'; - list: TickerOptionV5[]; -} - export interface FundingRateHistoryResponseV5 { symbol: string; fundingRate: string; @@ -241,6 +199,7 @@ export interface PublicTradeV5 { /** > openInterest string Open interest + > timestamp string The timestamp (ms) */ export type OpenInterestV5 = [string, string]; @@ -280,19 +239,8 @@ export interface RiskLimitV5 { maxLeverage: string; } -export interface RiskLimitResponseV5 { - category: CategoryV5; - list: RiskLimitV5[]; -} - export interface OptionDeliveryPriceV5 { symbol: string; deliveryPrice: string; deliveryTime: string; } - -export interface OptionDeliveryPriceResponseV5 { - category: CategoryV5; - list: OptionDeliveryPriceV5[]; - nextPageCursor?: string; -} diff --git a/src/types/response/v5-position.ts b/src/types/response/v5-position.ts new file mode 100644 index 0000000..ae283b9 --- /dev/null +++ b/src/types/response/v5-position.ts @@ -0,0 +1,29 @@ +import { CategoryV5 } from '../v5-shared'; + +export interface PositionV5 { + positionIdx: number; + riskId: number; + riskLimitValue: string; + symbol: string; + side: 'Buy' | 'Sell' | 'None'; + size: string; + avgPrice: string; + positionValue: string; + tradeMode: number; + autoAddMargin?: number; + positionStatus: 'Normal' | 'Liq' | 'Adl'; + leverage?: string; + markPrice: string; + liqPrice: string; + bustPrice?: string; + positionIM?: string; + positionMM?: string; + tpslMode?: 'Full' | 'Partial'; + takeProfit?: string; + stopLoss?: string; + trailingStop?: string; + unrealisedPnl: string; + cumRealisedPnl: string; + createdTime: string; + updatedTime: string; +} diff --git a/src/types/response/v5-trade.ts b/src/types/response/v5-trade.ts index bb9181a..78f7fdf 100644 --- a/src/types/response/v5-trade.ts +++ b/src/types/response/v5-trade.ts @@ -49,12 +49,6 @@ export interface AccountOrderV5 { updatedTime: string; } -export interface AccountOrdersResultV5 { - category: CategoryV5; - nextPageCursor?: string; - list: AccountOrderV5[]; -} - export interface BatchOrderResult { category: CategoryV5; symbol: string; diff --git a/src/types/v5-shared.ts b/src/types/v5-shared.ts index cb87116..afd2655 100644 --- a/src/types/v5-shared.ts +++ b/src/types/v5-shared.ts @@ -6,3 +6,34 @@ export type OrderTimeInForceV5 = 'GTC' | 'IOC' | 'FOK' | 'PostOnly'; export type OrderTriggerByV5 = 'LastPrice' | 'IndexPrice' | 'MarkPrice'; export type OrderTypeV5 = 'Market' | 'Limit'; export type PositionIdx = 0 | 1 | 2; + +export interface CategoryCursorListV5 { + category: CategoryV5; + list: T; + nextPageCursor?: string; +} + +/** + * Next page cursor does not exist for spot! + */ +export interface CursorListV5 { + nextPageCursor: string; + list: T; +} + +export interface CategoryListV5< + T extends unknown[], + TCategory extends CategoryV5 +> { + category: TCategory; + list: T; +} + +export interface CategorySymbolListV5< + T extends unknown[], + TCategory extends CategoryV5 +> { + category: TCategory; + symbol: string; + list: T; +}