diff --git a/src/rest-client-v5.ts b/src/rest-client-v5.ts index ebe96f4..703eb58 100644 --- a/src/rest-client-v5.ts +++ b/src/rest-client-v5.ts @@ -52,6 +52,19 @@ import { TickerSpotV5, TickerOptionV5, TickerLinearInverseV5, + SetLeverageParamsV5, + SwitchIsolatedMarginParamsV5, + SetTPSLModeParamsV5, + TPSLModeV5, + SwitchPositionModeParamsV5, + SetRiskLimitParamsV5, + SetRiskLimitResultV5, + SetTradingStopParamsV5, + SetAutoAddMarginParamsV5, + GetExecutionListParamsV5, + ExecutionV5, + GetClosedPnLParamsV5, + ClosedPnLV5, } from './types'; import { REST_CLIENT_TYPE_ENUM } from './util'; import BaseRestClient from './util/BaseRestClient'; @@ -410,6 +423,8 @@ export class RestClientV5 extends BaseRestClient { * Unified account covers: Linear contract / Options * * Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures + * + * Note: this will give a 404 error if you query the `option` category if your account is not unified */ getPositionInfo( params: PositionInfoParamsV5 @@ -417,6 +432,118 @@ export class RestClientV5 extends BaseRestClient { return this.getPrivate('/v5/position/list', params); } + /** + * Set the leverage + * + * Unified account covers: Linear contract + * + * Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures + * + * Note: Under one-way mode, buyLeverage must be the same as sellLeverage + */ + setLeverage(params: SetLeverageParamsV5): Promise> { + return this.postPrivate('/v5/position/set-leverage', params); + } + + /** + * Select cross margin mode or isolated margin mode. + * + * Covers: USDT perpetual (Normal account) / Inverse contract (Normal account). + * + * Switching margin modes will cause orders in progress to be cancelled. Please make sure that there are no open orders before you switch margin modes. + */ + switchIsolatedMargin( + params: SwitchIsolatedMarginParamsV5 + ): Promise> { + return this.postPrivate('/v5/position/switch-isolated', params); + } + + /** + * This endpoint sets the take profit/stop loss (TP/SL) mode to full or partial. + * + * Unified account covers: Linear contract; normal account covers: USDT perpetual, inverse perpetual, inverse futures. + * + * For partial TP/SL mode, you can set the TP/SL size smaller than position size. + */ + setTPSLMode( + params: SetTPSLModeParamsV5 + ): Promise> { + return this.postPrivate('/v5/position/set-tpsl-mode', params); + } + + /** + * Switches the position mode for USDT perpetual and Inverse futures. + * + * If you are in one-way Mode, you can only open one position on Buy or Sell side. + * + * If you are in hedge mode, you can open both Buy and Sell side positions simultaneously. + * + * Position mode. 0: Merged Single. 3: Both Sides. + */ + switchPositionMode( + params: SwitchPositionModeParamsV5 + ): Promise> { + return this.postPrivate('/v5/position/switch-mode', params); + } + + /** + * The risk limit will limit the maximum position value you can hold under different margin requirements. If you want to hold a bigger position size, you need more margin. This interface can set the risk limit of a single position. If the order exceeds the current risk limit when placing an order, it will be rejected. + */ + setRiskLimit( + params: SetRiskLimitParamsV5 + ): Promise> { + return this.postPrivate('/v5/position/set-risk-limit', params); + } + + /** + * This endpoint allows you to set the take profit, stop loss or trailing stop for a position. + * Passing these parameters will create conditional orders by the system internally. + * + * The system will cancel these orders if the position is closed, and adjust the qty according to the size of the open position. + * + * Unified account covers: Linear contract. + * Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures. + */ + setTradingStop( + params: SetTradingStopParamsV5 + ): Promise> { + return this.postPrivate('/v5/position/trading-stop', params); + } + /** + * This endpoint allows you to turn on/off auto-add-margin for an isolated margin position. + * + * Covers: USDT perpetual (Normal Account). + */ + setAutoAddMargin( + params: SetAutoAddMarginParamsV5 + ): Promise> { + return this.postPrivate('/v5/position/set-auto-add-margin', params); + } + + /** + * Query users' execution records, sorted by execTime in descending order + * + * Unified account covers: Spot / Linear contract / Options + * Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures + */ + getExecutionListV5( + params: GetExecutionListParamsV5 + ): Promise>> { + return this.getPrivate('/v5/execution/list', params); + } + + /** + * Query user's closed profit and loss records. The results are sorted by createdTime in descending order. + * + * Unified account covers: Linear contract + * Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures + */ + getClosedPnL( + params: GetClosedPnLParamsV5 + ): Promise>> { + return this.getPrivate('/v5/position/closed-pnl', params); + } + // // // diff --git a/src/types/request/v5-position.ts b/src/types/request/v5-position.ts index 91e0c1f..f378771 100644 --- a/src/types/request/v5-position.ts +++ b/src/types/request/v5-position.ts @@ -1,4 +1,4 @@ -import { CategoryV5 } from '../v5-shared'; +import { CategoryV5, TPSLModeV5 } from '../v5-shared'; export interface PositionInfoParamsV5 { category: CategoryV5; @@ -8,3 +8,81 @@ export interface PositionInfoParamsV5 { limit?: number; cursor?: string; } + +export interface SetLeverageParamsV5 { + category: 'linear' | 'inverse'; + symbol: string; + buyLeverage: string; + sellLeverage: string; +} + +export interface SwitchIsolatedMarginParamsV5 { + category: 'linear' | 'inverse'; + symbol: string; + tradeMode: 0 | 1; + buyLeverage: string; + sellLeverage: string; +} + +export interface SetTPSLModeParamsV5 { + category: 'linear' | 'inverse'; + symbol: string; + tpSlMode: TPSLModeV5; +} + +export interface SwitchPositionModeParamsV5 { + category: 'linear' | 'inverse'; + symbol?: string; + coin?: string; + mode: 0 | 3; +} + +export interface SetRiskLimitParamsV5 { + category: 'linear' | 'inverse'; + symbol: string; + riskId: number; + positionIdx?: number; +} + +export interface SetTradingStopParamsV5 { + symbol: string; + category: CategoryV5; + takeProfit?: string; + stopLoss?: string; + trailingStop?: string; + tpTriggerBy?: string; + slTriggerBy?: string; + activePrice?: string; + tpSize?: string; + slSize?: string; + positionIdx: number; +} + +export interface SetAutoAddMarginParamsV5 { + category: 'linear'; + symbol: string; + autoAddMargin: 0 | 1; + positionIdx?: number; +} + +export interface GetExecutionListParamsV5 { + category: CategoryV5; + symbol?: string; + orderId?: string; + orderLinkId?: string; + baseCoin?: string; + startTime?: number; + endTime?: number; + execType?: string; + limit?: number; + cursor?: string; +} + +export interface GetClosedPnLParamsV5 { + category: CategoryV5; + symbol?: string; + startTime?: number; + endTime?: number; + limit?: number; + cursor?: string; +} diff --git a/src/types/response/v5-position.ts b/src/types/response/v5-position.ts index ae283b9..768d226 100644 --- a/src/types/response/v5-position.ts +++ b/src/types/response/v5-position.ts @@ -1,7 +1,14 @@ -import { CategoryV5 } from '../v5-shared'; +import { + CategoryV5, + OrderSideV5, + OrderTypeV5, + PositionIdx, + TPSLModeV5, + TradeModeV5, +} from '../v5-shared'; export interface PositionV5 { - positionIdx: number; + positionIdx: PositionIdx; riskId: number; riskLimitValue: string; symbol: string; @@ -9,7 +16,7 @@ export interface PositionV5 { size: string; avgPrice: string; positionValue: string; - tradeMode: number; + tradeMode: TradeModeV5; autoAddMargin?: number; positionStatus: 'Normal' | 'Liq' | 'Adl'; leverage?: string; @@ -18,7 +25,7 @@ export interface PositionV5 { bustPrice?: string; positionIM?: string; positionMM?: string; - tpslMode?: 'Full' | 'Partial'; + tpslMode?: TPSLModeV5; takeProfit?: string; stopLoss?: string; trailingStop?: string; @@ -27,3 +34,56 @@ export interface PositionV5 { createdTime: string; updatedTime: string; } + +export interface SetRiskLimitResultV5 { + category: CategoryV5; + riskId: number; + riskLimitValue: string; +} + +export interface ExecutionV5 { + symbol: string; + orderId: string; + orderLinkId: string; + side: OrderSideV5; + orderPrice: string; + orderQty: string; + leavesQty: string; + orderType: OrderTypeV5; + stopOrderType?: string; + execFee: string; + execId: string; + execPrice: string; + execQty: string; + execType: string; + execValue: string; + execTime: string; + isMaker: boolean; + feeRate: string; + tradeIv?: string; + markIv?: string; + markPrice: string; + indexPrice: string; + underlyingPrice?: string; + blockTradeId?: string; +} + +export interface ClosedPnLV5 { + symbol: string; + orderId: string; + side: string; + qty: string; + orderPrice: string; + orderType: string; + execType: string; + closedSize: string; + cumEntryValue: string; + avgEntryPrice: string; + cumExitValue: string; + avgExitPrice: string; + closedPnl: string; + fillCount: string; + leverage: string; + createdTime: string; + updatedTime: string; +} diff --git a/src/types/v5-shared.ts b/src/types/v5-shared.ts index afd2655..1fc21bb 100644 --- a/src/types/v5-shared.ts +++ b/src/types/v5-shared.ts @@ -2,10 +2,15 @@ export type CategoryV5 = 'spot' | 'linear' | 'inverse' | 'option'; export type OrderFilterV5 = 'Order' | 'tpslOrder'; export type OrderSideV5 = 'Buy' | 'Sell'; +export type OrderTypeV5 = 'Market' | 'Limit'; export type OrderTimeInForceV5 = 'GTC' | 'IOC' | 'FOK' | 'PostOnly'; export type OrderTriggerByV5 = 'LastPrice' | 'IndexPrice' | 'MarkPrice'; -export type OrderTypeV5 = 'Market' | 'Limit'; export type PositionIdx = 0 | 1 | 2; +/** + * Trade mode. 0: cross-margin, 1: isolated margin + */ +export type TradeModeV5 = 0 | 1; +export type TPSLModeV5 = 'Full' | 'Partial'; export interface CategoryCursorListV5 { category: CategoryV5;