add position v5 rest apis

This commit is contained in:
tiagosiebler
2023-02-16 12:25:25 +00:00
parent 47e1255159
commit 71c6c9b6a5
4 changed files with 276 additions and 6 deletions

View File

@@ -52,6 +52,19 @@ import {
TickerSpotV5, TickerSpotV5,
TickerOptionV5, TickerOptionV5,
TickerLinearInverseV5, TickerLinearInverseV5,
SetLeverageParamsV5,
SwitchIsolatedMarginParamsV5,
SetTPSLModeParamsV5,
TPSLModeV5,
SwitchPositionModeParamsV5,
SetRiskLimitParamsV5,
SetRiskLimitResultV5,
SetTradingStopParamsV5,
SetAutoAddMarginParamsV5,
GetExecutionListParamsV5,
ExecutionV5,
GetClosedPnLParamsV5,
ClosedPnLV5,
} from './types'; } from './types';
import { REST_CLIENT_TYPE_ENUM } from './util'; import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
@@ -410,6 +423,8 @@ export class RestClientV5 extends BaseRestClient {
* Unified account covers: Linear contract / Options * Unified account covers: Linear contract / Options
* *
* Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures * 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( getPositionInfo(
params: PositionInfoParamsV5 params: PositionInfoParamsV5
@@ -417,6 +432,118 @@ export class RestClientV5 extends BaseRestClient {
return this.getPrivate('/v5/position/list', params); 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<APIResponseV3WithTime<{}>> {
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<APIResponseV3WithTime<{}>> {
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<APIResponseV3WithTime<{ tpSlMode: TPSLModeV5 }>> {
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<APIResponseV3WithTime<{}>> {
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<APIResponseV3WithTime<SetRiskLimitResultV5>> {
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<APIResponseV3WithTime<{}>> {
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<APIResponseV3WithTime<{}>> {
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<APIResponseV3WithTime<CategoryCursorListV5<ExecutionV5[]>>> {
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<APIResponseV3WithTime<CategoryCursorListV5<ClosedPnLV5[]>>> {
return this.getPrivate('/v5/position/closed-pnl', params);
}
// //
// //
// //

View File

@@ -1,4 +1,4 @@
import { CategoryV5 } from '../v5-shared'; import { CategoryV5, TPSLModeV5 } from '../v5-shared';
export interface PositionInfoParamsV5 { export interface PositionInfoParamsV5 {
category: CategoryV5; category: CategoryV5;
@@ -8,3 +8,81 @@ export interface PositionInfoParamsV5 {
limit?: number; limit?: number;
cursor?: string; 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;
}

View File

@@ -1,7 +1,14 @@
import { CategoryV5 } from '../v5-shared'; import {
CategoryV5,
OrderSideV5,
OrderTypeV5,
PositionIdx,
TPSLModeV5,
TradeModeV5,
} from '../v5-shared';
export interface PositionV5 { export interface PositionV5 {
positionIdx: number; positionIdx: PositionIdx;
riskId: number; riskId: number;
riskLimitValue: string; riskLimitValue: string;
symbol: string; symbol: string;
@@ -9,7 +16,7 @@ export interface PositionV5 {
size: string; size: string;
avgPrice: string; avgPrice: string;
positionValue: string; positionValue: string;
tradeMode: number; tradeMode: TradeModeV5;
autoAddMargin?: number; autoAddMargin?: number;
positionStatus: 'Normal' | 'Liq' | 'Adl'; positionStatus: 'Normal' | 'Liq' | 'Adl';
leverage?: string; leverage?: string;
@@ -18,7 +25,7 @@ export interface PositionV5 {
bustPrice?: string; bustPrice?: string;
positionIM?: string; positionIM?: string;
positionMM?: string; positionMM?: string;
tpslMode?: 'Full' | 'Partial'; tpslMode?: TPSLModeV5;
takeProfit?: string; takeProfit?: string;
stopLoss?: string; stopLoss?: string;
trailingStop?: string; trailingStop?: string;
@@ -27,3 +34,56 @@ export interface PositionV5 {
createdTime: string; createdTime: string;
updatedTime: 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;
}

View File

@@ -2,10 +2,15 @@ export type CategoryV5 = 'spot' | 'linear' | 'inverse' | 'option';
export type OrderFilterV5 = 'Order' | 'tpslOrder'; export type OrderFilterV5 = 'Order' | 'tpslOrder';
export type OrderSideV5 = 'Buy' | 'Sell'; export type OrderSideV5 = 'Buy' | 'Sell';
export type OrderTypeV5 = 'Market' | 'Limit';
export type OrderTimeInForceV5 = 'GTC' | 'IOC' | 'FOK' | 'PostOnly'; export type OrderTimeInForceV5 = 'GTC' | 'IOC' | 'FOK' | 'PostOnly';
export type OrderTriggerByV5 = 'LastPrice' | 'IndexPrice' | 'MarkPrice'; export type OrderTriggerByV5 = 'LastPrice' | 'IndexPrice' | 'MarkPrice';
export type OrderTypeV5 = 'Market' | 'Limit';
export type PositionIdx = 0 | 1 | 2; 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<T extends unknown[]> { export interface CategoryCursorListV5<T extends unknown[]> {
category: CategoryV5; category: CategoryV5;