feat(): expand v5 client with trade endpoints

This commit is contained in:
tiagosiebler
2023-02-15 11:12:27 +00:00
parent 5b44e31207
commit 145f17d4f1
7 changed files with 400 additions and 6 deletions

View File

@@ -0,0 +1,30 @@
import { ContractClient } from '../src/index';
// or
// import { ContractClient } from 'bybit-api';
const key = process.env.API_KEY_COM;
const secret = process.env.API_SECRET_COM;
const client = new ContractClient({
key,
secret,
strict_param_validation: true,
});
(async () => {
try {
/**
* You can make raw HTTP requests without the per-endpoint abstraction,
* e.g. if an endpoint is missing and you're blocked (but please raise an issue if you're missing an endpoint)
*/
const rawCall = await client.getPrivate('/v5/order/realtime', {
category: 'linear',
symbol: 'BTCUSDT',
});
console.log('rawCall:', rawCall);
} catch (e) {
console.error('request failed: ', e);
}
})();

View File

@@ -1,7 +1,8 @@
import { import {
APIResponseV3WithTime, APIResponseV3WithTime,
APIResponseWithTime, CategoryListV5,
CategoryV5, CategoryV5,
FundingRateHistoryResponseV5,
GetFundingRateHistoryParamsV5, GetFundingRateHistoryParamsV5,
GetHistoricalVolatilityParamsV5, GetHistoricalVolatilityParamsV5,
GetIndexPriceKlineParamsV5, GetIndexPriceKlineParamsV5,
@@ -16,19 +17,17 @@ import {
GetPublicTradingHistoryParamsV5, GetPublicTradingHistoryParamsV5,
GetRiskLimitParamsV5, GetRiskLimitParamsV5,
GetTickersParamsV5, GetTickersParamsV5,
} from './types';
import {
CategoryListV5,
FundingRateHistoryResponseV5,
HistoricalVolatilityV5, HistoricalVolatilityV5,
IndexPriceKlineResponseV5, IndexPriceKlineResponseV5,
InstrumentInfoV5, InstrumentInfoV5,
InsuranceResponseV5, InsuranceResponseV5,
KlineResponseV5, KlineResponseV5,
MarkPriceKlineResponseV5, MarkPriceKlineResponseV5,
AccountOrdersResultV5,
OpenInterestResponseV5, OpenInterestResponseV5,
OptionDeliveryPriceResponseV5, OptionDeliveryPriceResponseV5,
OrderbookResponseV5, OrderbookResponseV5,
OrderParamsV5,
PaginatedListV5, PaginatedListV5,
PremiumIndexPriceKlineResponse, PremiumIndexPriceKlineResponse,
PublicTradeV5, PublicTradeV5,
@@ -36,7 +35,22 @@ import {
TickersLinearInverseResponseV5, TickersLinearInverseResponseV5,
TickersOptionResponseV5, TickersOptionResponseV5,
TickersSpotResponseV5, TickersSpotResponseV5,
} from './types/response/v5-market'; AmendOrderParamsV5,
CancelOrderParamsV5,
GetAccountOrdersParams,
OrderResultV5,
CancelAllOrdersParamsV5,
BatchOrderParamsV5,
BatchAmendOrderParamsV5,
BatchOrderResult,
BatchOrdersResult,
BatchAmendOrderResult,
BatchCancelOrderParamsV5,
BatchCancelOrderResult,
OrderSideV5,
SpotBorrowCheckResult,
APIResponseV3,
} 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';
@@ -219,4 +233,139 @@ export class RestClientV5 extends BaseRestClient {
): Promise<APIResponseV3WithTime<OptionDeliveryPriceResponseV5>> { ): Promise<APIResponseV3WithTime<OptionDeliveryPriceResponseV5>> {
return this.get(`/v5/market/delivery-price`, params); return this.get(`/v5/market/delivery-price`, params);
} }
/**
*
* Trade APIs
*
*/
submitOrder(
params: OrderParamsV5
): Promise<APIResponseV3WithTime<OrderResultV5>> {
return this.postPrivate(`/v5/order/create`, params);
}
amendOrder(
params: AmendOrderParamsV5
): Promise<APIResponseV3WithTime<OrderResultV5>> {
return this.postPrivate('/v5/order/amend', params);
}
cancelOrder(
params: CancelOrderParamsV5
): Promise<APIResponseV3WithTime<OrderResultV5>> {
return this.postPrivate('/v5/order/cancel', params);
}
/**
* Query unfilled or partially filled orders in real-time. To query older order records, please use the order history interface.
*/
getActiveOrders(
params: GetAccountOrdersParams
): Promise<APIResponseV3WithTime<AccountOrdersResultV5>> {
return this.getPrivate('/v5/order/realtime', params);
}
cancelAllOrders(
params: CancelAllOrdersParamsV5
): Promise<APIResponseV3WithTime<{ list: OrderResultV5[] }>> {
return this.postPrivate('/v5/order/cancel-all', params);
}
/**
* 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<APIResponseV3WithTime<AccountOrdersResultV5>> {
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(
category: 'option',
orders: BatchOrderParamsV5[]
): Promise<APIResponseV3WithTime<BatchOrdersResult<BatchOrderResult[]>>> {
return this.postPrivate('/v5/order/create-batch', {
category,
request: orders,
});
}
/**
* 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(
category: 'option',
orders: BatchAmendOrderParamsV5[]
): Promise<
APIResponseV3WithTime<BatchOrdersResult<BatchAmendOrderResult[]>>
> {
return this.postPrivate('/v5/order/amend-batch', {
category,
request: orders,
});
}
/**
* 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(
category: 'option',
orders: BatchCancelOrderParamsV5[]
): Promise<
APIResponseV3WithTime<BatchOrdersResult<BatchCancelOrderResult[]>>
> {
return this.postPrivate('/v5/order/cancel-batch', {
category,
request: orders,
});
}
/**
* Query the qty and amount of borrowable coins in spot account.
* Covers: Spot (Unified Account)
*/
getSpotBorrowCheck(
symbol: string,
side: OrderSideV5
): Promise<APIResponseV3WithTime<SpotBorrowCheckResult>> {
return this.getPrivate('/v5/order/spot-borrow-check', {
category: 'spot',
symbol,
side,
});
}
/**
* 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(
category: 'option',
timeWindow: number
): Promise<APIResponseV3<undefined>> {
return this.postPrivate('/v5/order/disconnected-cancel-all', {
category,
timeWindow,
});
}
//
//
//
//
//
//
//
} }

View File

@@ -9,3 +9,4 @@ export * from './usdc-shared';
export * from './unified-margin'; export * from './unified-margin';
export * from './v5-market'; export * from './v5-market';
export * from './contract'; export * from './contract';
export * from './v5-trade';

View File

@@ -0,0 +1,107 @@
import {
CategoryV5,
OrderFilterV5,
OrderSideV5,
OrderTimeInForceV5,
OrderTriggerByV5,
OrderTypeV5,
PositionIdx,
} from '../v5-shared';
export interface OrderParamsV5 {
category: CategoryV5;
symbol: string;
isLeverage?: 0 | 1;
side: OrderSideV5;
orderType: OrderTypeV5;
qty: string;
price?: string;
triggerDirection?: 1 | 2;
orderFilter?: 'Order' | 'tpslOrder';
triggerPrice?: string;
triggerBy?: OrderTriggerByV5;
orderIv?: string;
timeInForce?: OrderTimeInForceV5;
positionIdx?: PositionIdx;
orderLinkId?: string;
takeProfit?: string;
stopLoss?: string;
tpTriggerBy?: OrderTriggerByV5;
slTriggerBy?: OrderTriggerByV5;
reduceOnly?: boolean;
closeOnTrigger?: boolean;
mmp?: boolean;
}
export interface AmendOrderParamsV5 {
category: CategoryV5;
symbol: string;
orderId?: string;
orderLinkId?: string;
orderIv?: string;
triggerPrice?: string;
qty?: string;
price?: string;
takeProfit?: string;
stopLoss?: string;
tpTriggerBy?: OrderTriggerByV5;
slTriggerBy?: OrderTriggerByV5;
triggerBy?: OrderTriggerByV5;
}
export interface CancelOrderParamsV5 {
category: CategoryV5;
symbol: string;
orderId?: string;
orderLinkId?: string;
orderFilter?: OrderFilterV5;
}
export interface GetAccountOrdersParams {
category: CategoryV5;
symbol?: string;
baseCoin?: string;
settleCoin?: string;
orderId?: string;
orderLinkId?: string;
openOnly?: 0 | 1 | 2;
orderFilter?: OrderFilterV5;
limit?: number;
cursor?: string;
}
export interface CancelAllOrdersParamsV5 {
category: CategoryV5;
symbol?: string;
baseCoin?: string;
settleCoin?: string;
orderFilter?: OrderFilterV5;
}
export interface BatchOrderParamsV5 {
symbol: string;
side: OrderSideV5;
orderType: OrderTypeV5;
qty: string;
price?: string;
orderIv?: string;
timeInForce?: OrderTimeInForceV5;
orderLinkId: string;
reduceOnly?: boolean;
mmp?: boolean;
}
export interface BatchAmendOrderParamsV5 {
symbol: string;
orderId?: string;
orderLinkId?: string;
qty?: string;
price?: string;
orderIv?: string;
}
export interface BatchCancelOrderParamsV5 {
symbol: string;
orderId?: string;
orderLinkId?: string;
}

View File

@@ -4,3 +4,5 @@ export * from './shared';
export * from './spot'; export * from './spot';
export * from './usdt-perp'; export * from './usdt-perp';
export * from './unified-margin'; export * from './unified-margin';
export * from './v5-market';
export * from './v5-trade';

View File

@@ -0,0 +1,98 @@
import {
CategoryV5,
OrderSideV5,
OrderTimeInForceV5,
OrderTriggerByV5,
OrderTypeV5,
PositionIdx,
} from '../v5-shared';
export interface OrderResultV5 {
orderId: string;
orderLinkId: string;
}
export interface AccountOrderV5 {
orderId: string;
orderLinkId?: string;
blockTradeId?: string;
symbol: string;
price: string;
qty: string;
side: OrderSideV5;
isLeverage?: string;
positionIdx?: PositionIdx;
orderStatus: string;
cancelType?: string;
rejectReason?: string;
avgPrice: string;
leavesQty: string;
leavesValue: string;
cumExecQty: string;
cumExecValue: string;
cumExecFee: string;
timeInForce?: OrderTimeInForceV5;
orderType?: OrderTypeV5;
stopOrderType?: string;
orderIv?: string;
triggerPrice?: string;
takeProfit?: string;
stopLoss?: string;
tpTriggerBy?: OrderTriggerByV5;
slTriggerBy?: OrderTriggerByV5;
triggerDirection?: number;
triggerBy?: OrderTriggerByV5;
lastPriceOnCreated?: string;
reduceOnly?: boolean;
closeOnTrigger?: boolean;
createdTime: string;
updatedTime: string;
}
export interface AccountOrdersResultV5 {
category: CategoryV5;
nextPageCursor?: string;
list: AccountOrderV5[];
}
export interface BatchOrderResult {
category: CategoryV5;
symbol: string;
orderId: string;
orderLinkId: string;
createAt: string;
}
export interface BatchOrdersResult<T extends unknown[]> {
result: {
list: T;
};
retExtInfo: {
list: {
code: number;
msg: string;
}[];
};
}
export interface BatchAmendOrderResult {
category: CategoryV5;
symbol: string;
orderId: string;
orderLinkId: string;
}
export interface BatchCancelOrderResult {
category: CategoryV5;
symbol: string;
orderId: string;
orderLinkId: string;
}
export interface SpotBorrowCheckResult {
symbol: string;
side: OrderSideV5;
maxTradeQty: string;
maxTradeAmount: string;
borrowCoin: string;
}

View File

@@ -1 +1,8 @@
export type CategoryV5 = 'spot' | 'linear' | 'inverse' | 'option'; export type CategoryV5 = 'spot' | 'linear' | 'inverse' | 'option';
export type OrderFilterV5 = 'Order' | 'tpslOrder';
export type OrderSideV5 = 'Buy' | 'Sell';
export type OrderTimeInForceV5 = 'GTC' | 'IOC' | 'FOK' | 'PostOnly';
export type OrderTriggerByV5 = 'LastPrice' | 'IndexPrice' | 'MarkPrice';
export type OrderTypeV5 = 'Market' | 'Limit';
export type PositionIdx = 0 | 1 | 2;