feat(#261): add tpsl params missing from position endpoints. add new endpoint for add/reduce margin. add doc examples

This commit is contained in:
tiagosiebler
2023-06-07 16:56:36 +01:00
parent 4f563a65a5
commit 05d63839da
15 changed files with 289 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
# V5 - REST - Position
https://bybit-exchange.github.io/docs/v5/position

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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);
});

View File

@@ -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<APIResponseV3WithTime<AddOrReduceMarginResultV5>> {
return this.postPrivate('/v5/position/add-margin', params);
}
/**
* Query users' execution records, sorted by execTime in descending order
*

View File

@@ -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;

View File

@@ -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;