From 05d63839da61d384ebd4267c33b0cd29c2c08a40 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Wed, 7 Jun 2023 16:56:36 +0100 Subject: [PATCH] feat(#261): add tpsl params missing from position endpoints. add new endpoint for add/reduce margin. add doc examples --- examples/apidoc/V5/Position/README.md | 3 ++ .../V5/Position/add-or-reduce-margin.js | 20 ++++++++++++ examples/apidoc/V5/Position/get-closed-pnl.js | 19 ++++++++++++ examples/apidoc/V5/Position/get-execution.js | 20 ++++++++++++ .../apidoc/V5/Position/get-position-info.js | 19 ++++++++++++ .../apidoc/V5/Position/set-auto-add-margin.js | 20 ++++++++++++ examples/apidoc/V5/Position/set-leverage.js | 21 +++++++++++++ examples/apidoc/V5/Position/set-risk-limit.js | 20 ++++++++++++ examples/apidoc/V5/Position/set-tpsl-mode.js | 20 ++++++++++++ .../apidoc/V5/Position/set-trading-stop.js | 31 +++++++++++++++++++ .../Position/switch-cross-isolated-margin.js | 22 +++++++++++++ .../V5/Position/switch-position-mode.js | 20 ++++++++++++ src/rest-client-v5.ts | 14 +++++++++ src/types/request/v5-position.ts | 15 ++++++++- src/types/response/v5-position.ts | 26 ++++++++++++++++ 15 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 examples/apidoc/V5/Position/README.md create mode 100644 examples/apidoc/V5/Position/add-or-reduce-margin.js create mode 100644 examples/apidoc/V5/Position/get-closed-pnl.js create mode 100644 examples/apidoc/V5/Position/get-execution.js create mode 100644 examples/apidoc/V5/Position/get-position-info.js create mode 100644 examples/apidoc/V5/Position/set-auto-add-margin.js create mode 100644 examples/apidoc/V5/Position/set-leverage.js create mode 100644 examples/apidoc/V5/Position/set-risk-limit.js create mode 100644 examples/apidoc/V5/Position/set-tpsl-mode.js create mode 100644 examples/apidoc/V5/Position/set-trading-stop.js create mode 100644 examples/apidoc/V5/Position/switch-cross-isolated-margin.js create mode 100644 examples/apidoc/V5/Position/switch-position-mode.js diff --git a/examples/apidoc/V5/Position/README.md b/examples/apidoc/V5/Position/README.md new file mode 100644 index 0000000..d155ac5 --- /dev/null +++ b/examples/apidoc/V5/Position/README.md @@ -0,0 +1,3 @@ +# V5 - REST - Position + +https://bybit-exchange.github.io/docs/v5/position diff --git a/examples/apidoc/V5/Position/add-or-reduce-margin.js b/examples/apidoc/V5/Position/add-or-reduce-margin.js new file mode 100644 index 0000000..bf8791a --- /dev/null +++ b/examples/apidoc/V5/Position/add-or-reduce-margin.js @@ -0,0 +1,20 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .addOrReduceMargin({ + category: 'linear', + symbol: 'BTCUSDT', + margin: '10', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/get-closed-pnl.js b/examples/apidoc/V5/Position/get-closed-pnl.js new file mode 100644 index 0000000..5e6d69e --- /dev/null +++ b/examples/apidoc/V5/Position/get-closed-pnl.js @@ -0,0 +1,19 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .getClosedPnL({ + category: 'linear', + limit: 1, + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/get-execution.js b/examples/apidoc/V5/Position/get-execution.js new file mode 100644 index 0000000..bf8791a --- /dev/null +++ b/examples/apidoc/V5/Position/get-execution.js @@ -0,0 +1,20 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .addOrReduceMargin({ + category: 'linear', + symbol: 'BTCUSDT', + margin: '10', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/get-position-info.js b/examples/apidoc/V5/Position/get-position-info.js new file mode 100644 index 0000000..7215403 --- /dev/null +++ b/examples/apidoc/V5/Position/get-position-info.js @@ -0,0 +1,19 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .getPositionInfo({ + category: 'inverse', + symbol: 'BTCUSD', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/set-auto-add-margin.js b/examples/apidoc/V5/Position/set-auto-add-margin.js new file mode 100644 index 0000000..24b67cd --- /dev/null +++ b/examples/apidoc/V5/Position/set-auto-add-margin.js @@ -0,0 +1,20 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .setAutoAddMargin({ + category: 'linear', + symbol: 'BTCUSDT', + autoAddMargin: 1, + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/set-leverage.js b/examples/apidoc/V5/Position/set-leverage.js new file mode 100644 index 0000000..fa6817a --- /dev/null +++ b/examples/apidoc/V5/Position/set-leverage.js @@ -0,0 +1,21 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .setLeverage({ + category: 'linear', + symbol: 'BTCUSDT', + buyLeverage: '6', + sellLeverage: '6', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/set-risk-limit.js b/examples/apidoc/V5/Position/set-risk-limit.js new file mode 100644 index 0000000..5547185 --- /dev/null +++ b/examples/apidoc/V5/Position/set-risk-limit.js @@ -0,0 +1,20 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .setRiskLimit({ + category: 'linear', + symbol: 'BTCUSDT', + riskId: 4, + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/set-tpsl-mode.js b/examples/apidoc/V5/Position/set-tpsl-mode.js new file mode 100644 index 0000000..a7472a8 --- /dev/null +++ b/examples/apidoc/V5/Position/set-tpsl-mode.js @@ -0,0 +1,20 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .setTPSLMode({ + symbol: 'XRPUSDT', + category: 'linear', + tpSlMode: 'Full', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/set-trading-stop.js b/examples/apidoc/V5/Position/set-trading-stop.js new file mode 100644 index 0000000..fa53711 --- /dev/null +++ b/examples/apidoc/V5/Position/set-trading-stop.js @@ -0,0 +1,31 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .setTradingStop({ + category: 'linear', + symbol: 'XRPUSDT', + takeProfit: '0.5', + stopLoss: '0.2', + tpTriggerBy: 'MarkPrice', + slTriggerBy: 'IndexPrice', + tpslMode: 'Partial', + tpOrderType: 'Limit', + slOrderType: 'Limit', + tpSize: '50', + slSize: '50', + tpLimitPrice: '0.49', + slLimitPrice: '0.21', + positionIdx: 0, + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/switch-cross-isolated-margin.js b/examples/apidoc/V5/Position/switch-cross-isolated-margin.js new file mode 100644 index 0000000..b58113b --- /dev/null +++ b/examples/apidoc/V5/Position/switch-cross-isolated-margin.js @@ -0,0 +1,22 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .switchIsolatedMargin({ + category: 'linear', + symbol: 'ETHUSDT', + tradeMode: 1, + buyLeverage: '10', + sellLeverage: '10', + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/examples/apidoc/V5/Position/switch-position-mode.js b/examples/apidoc/V5/Position/switch-position-mode.js new file mode 100644 index 0000000..5c4d4fd --- /dev/null +++ b/examples/apidoc/V5/Position/switch-position-mode.js @@ -0,0 +1,20 @@ +const { RestClientV5 } = require('bybit-api'); + +const client = new RestClientV5({ + testnet: true, + key: 'apikey', + secret: 'apisecret', +}); + +client + .switchPositionMode({ + category: 'inverse', + symbol: 'BTCUSDH23', + mode: 0, + }) + .then((response) => { + console.log(response); + }) + .catch((error) => { + console.error(error); + }); diff --git a/src/rest-client-v5.ts b/src/rest-client-v5.ts index 12f099e..2d4fabb 100644 --- a/src/rest-client-v5.ts +++ b/src/rest-client-v5.ts @@ -6,6 +6,8 @@ import { AccountMarginModeV5, AccountOrderV5, AccountTypeV5, + AddOrReduceMarginParamsV5, + AddOrReduceMarginResultV5, AllCoinsBalanceV5, AllowedDepositCoinInfoV5, AmendOrderParamsV5, @@ -618,6 +620,18 @@ export class RestClientV5 extends BaseRestClient { return this.postPrivate('/v5/position/set-auto-add-margin', params); } + /** + * Manually add or reduce margin for isolated margin position + * + * Unified account covers: USDT perpetual / USDC perpetual / USDC futures / Inverse contract + * Normal account covers: USDT perpetual / Inverse contract + */ + addOrReduceMargin( + params: AddOrReduceMarginParamsV5, + ): Promise> { + return this.postPrivate('/v5/position/add-margin', params); + } + /** * Query users' execution records, sorted by execTime in descending order * diff --git a/src/types/request/v5-position.ts b/src/types/request/v5-position.ts index 6b22a7e..160ee34 100644 --- a/src/types/request/v5-position.ts +++ b/src/types/request/v5-position.ts @@ -2,6 +2,7 @@ import { CategoryV5, ExecTypeV5, OrderTriggerByV5, + OrderTypeV5, PositionIdx, TPSLModeV5, } from '../v5-shared'; @@ -51,16 +52,21 @@ export interface SetRiskLimitParamsV5 { } export interface SetTradingStopParamsV5 { - symbol: string; category: CategoryV5; + symbol: string; takeProfit?: string; stopLoss?: string; trailingStop?: string; tpTriggerBy?: OrderTriggerByV5; slTriggerBy?: OrderTriggerByV5; activePrice?: string; + tpslMode?: TPSLModeV5; tpSize?: string; slSize?: string; + tpLimitPrice?: string; + slLimitPrice?: string; + tpOrderType?: OrderTypeV5; + slOrderType?: OrderTypeV5; positionIdx: PositionIdx; } @@ -71,6 +77,13 @@ export interface SetAutoAddMarginParamsV5 { positionIdx?: PositionIdx; } +export interface AddOrReduceMarginParamsV5 { + category: 'linear' | 'inverse'; + symbol: string; + margin: string; + positionIDex?: PositionIdx; +} + export interface GetExecutionListParamsV5 { category: CategoryV5; symbol?: string; diff --git a/src/types/response/v5-position.ts b/src/types/response/v5-position.ts index 7cedeec..0ea3a00 100644 --- a/src/types/response/v5-position.ts +++ b/src/types/response/v5-position.ts @@ -43,6 +43,32 @@ export interface SetRiskLimitResultV5 { riskLimitValue: string; } +export interface AddOrReduceMarginResultV5 { + category: CategoryV5; + symbol: string; + positionIdx: PositionIdx; + riskId: number; + riskLimitValue: string; + size: string; + avgPrice: string; + liqPrice: string; + bustPrice: string; + markPrice: string; + positionValue: string; + leverage: string; + autoAddMargin: 0 | 1; + positionStatus: 'Normal' | 'Liq' | 'Adl'; + positionIM: string; + positionMM: string; + takeProfit: string; + stopLoss: string; + trailingStop: string; + unrealisedPnl: string; + cumRealisedPnl: string; + createdTime: string; + updatedTime: string; +} + export interface ExecutionV5 { symbol: string; orderId: string;