Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bybit): add ws crud #22313

Merged
merged 3 commits into from Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
132 changes: 72 additions & 60 deletions ts/src/bybit.ts
Expand Up @@ -4037,44 +4037,8 @@ export default class bybit extends Exchange {
return this.parseOrder (result, market);
}

async editOrder (id: string, symbol: string, type:OrderType, side: OrderSide, amount: Num = undefined, price: Num = undefined, params = {}) {
/**
* @method
* @name bybit#editOrder
* @description edit a trade order
* @see https://bybit-exchange.github.io/docs/v5/order/amend-order
* @see https://bybit-exchange.github.io/docs/derivatives/unified/replace-order
* @see https://bybit-exchange.github.io/docs/api-explorer/derivatives/trade/contract/replace-order
* @param {string} id cancel order id
* @param {string} symbol unified symbol of the market to create an order in
* @param {string} type 'market' or 'limit'
* @param {string} side 'buy' or 'sell'
* @param {float} amount how much of currency you want to trade in units of base currency
* @param {float} price the price at which the order is to be fullfilled, in units of the base currency, ignored in market orders
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @param {float} [params.triggerPrice] The price that a trigger order is triggered at
* @param {float} [params.stopLossPrice] The price that a stop loss order is triggered at
* @param {float} [params.takeProfitPrice] The price that a take profit order is triggered at
* @param {object} [params.takeProfit] *takeProfit object in params* containing the triggerPrice that the attached take profit order will be triggered
* @param {float} [params.takeProfit.triggerPrice] take profit trigger price
* @param {object} [params.stopLoss] *stopLoss object in params* containing the triggerPrice that the attached stop loss order will be triggered
* @param {float} [params.stopLoss.triggerPrice] stop loss trigger price
* @param {string} [params.triggerBy] 'IndexPrice', 'MarkPrice' or 'LastPrice', default is 'LastPrice', required if no initial value for triggerPrice
* @param {string} [params.slTriggerBy] 'IndexPrice', 'MarkPrice' or 'LastPrice', default is 'LastPrice', required if no initial value for stopLoss
* @param {string} [params.tpTriggerby] 'IndexPrice', 'MarkPrice' or 'LastPrice', default is 'LastPrice', required if no initial value for takeProfit
* @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
*/
if (symbol === undefined) {
throw new ArgumentsRequired (this.id + ' editOrder() requires a symbol argument');
}
await this.loadMarkets ();
editOrderRequest (id: string, symbol: string, type:OrderType, side: OrderSide, amount: Num = undefined, price: Num = undefined, params = {}) {
const market = this.market (symbol);
const [ enableUnifiedMargin, enableUnifiedAccount ] = await this.isUnifiedEnabled ();
const isUnifiedAccount = (enableUnifiedMargin || enableUnifiedAccount);
const isUsdcSettled = market['settle'] === 'USDC';
if (isUsdcSettled && !isUnifiedAccount) {
return await this.editUsdcOrder (id, symbol, type, side, amount, price, params);
}
const request = {
'symbol': market['id'],
'orderId': id,
Expand Down Expand Up @@ -4145,6 +4109,48 @@ export default class bybit extends Exchange {
request['orderLinkId'] = clientOrderId;
}
params = this.omit (params, [ 'stopPrice', 'stopLossPrice', 'takeProfitPrice', 'triggerPrice', 'clientOrderId', 'stopLoss', 'takeProfit' ]);
return request;
}

async editOrder (id: string, symbol: string, type:OrderType, side: OrderSide, amount: Num = undefined, price: Num = undefined, params = {}) {
/**
* @method
* @name bybit#editOrder
* @description edit a trade order
* @see https://bybit-exchange.github.io/docs/v5/order/amend-order
* @see https://bybit-exchange.github.io/docs/derivatives/unified/replace-order
* @see https://bybit-exchange.github.io/docs/api-explorer/derivatives/trade/contract/replace-order
* @param {string} id cancel order id
* @param {string} symbol unified symbol of the market to create an order in
* @param {string} type 'market' or 'limit'
* @param {string} side 'buy' or 'sell'
* @param {float} amount how much of currency you want to trade in units of base currency
* @param {float} price the price at which the order is to be fullfilled, in units of the base currency, ignored in market orders
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @param {float} [params.triggerPrice] The price that a trigger order is triggered at
* @param {float} [params.stopLossPrice] The price that a stop loss order is triggered at
* @param {float} [params.takeProfitPrice] The price that a take profit order is triggered at
* @param {object} [params.takeProfit] *takeProfit object in params* containing the triggerPrice that the attached take profit order will be triggered
* @param {float} [params.takeProfit.triggerPrice] take profit trigger price
* @param {object} [params.stopLoss] *stopLoss object in params* containing the triggerPrice that the attached stop loss order will be triggered
* @param {float} [params.stopLoss.triggerPrice] stop loss trigger price
* @param {string} [params.triggerBy] 'IndexPrice', 'MarkPrice' or 'LastPrice', default is 'LastPrice', required if no initial value for triggerPrice
* @param {string} [params.slTriggerBy] 'IndexPrice', 'MarkPrice' or 'LastPrice', default is 'LastPrice', required if no initial value for stopLoss
* @param {string} [params.tpTriggerby] 'IndexPrice', 'MarkPrice' or 'LastPrice', default is 'LastPrice', required if no initial value for takeProfit
* @returns {object} an [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
*/
await this.loadMarkets ();
if (symbol === undefined) {
throw new ArgumentsRequired (this.id + ' editOrder() requires a symbol argument');
}
const market = this.market (symbol);
const [ enableUnifiedMargin, enableUnifiedAccount ] = await this.isUnifiedEnabled ();
const isUnifiedAccount = (enableUnifiedMargin || enableUnifiedAccount);
const isUsdcSettled = market['settle'] === 'USDC';
if (isUsdcSettled && !isUnifiedAccount) {
return await this.editUsdcOrder (id, symbol, type, side, amount, price, params);
}
const request = this.editOrderRequest (id, symbol, type, side, amount, price, params);
const response = await this.privatePostV5OrderAmend (this.extend (request, params));
//
// {
Expand Down Expand Up @@ -4205,30 +4211,8 @@ export default class bybit extends Exchange {
return this.parseOrder (result, market);
}

async cancelOrder (id: string, symbol: Str = undefined, params = {}) {
/**
* @method
* @name bybit#cancelOrder
* @description cancels an open order
* @see https://bybit-exchange.github.io/docs/v5/order/cancel-order
* @param {string} id order id
* @param {string} symbol unified symbol of the market the order was made in
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @param {boolean} [params.stop] *spot only* whether the order is a stop order
* @param {string} [params.orderFilter] *spot only* 'Order' or 'StopOrder' or 'tpslOrder'
* @returns {object} An [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
*/
if (symbol === undefined) {
throw new ArgumentsRequired (this.id + ' cancelOrder() requires a symbol argument');
}
await this.loadMarkets ();
cancelOrderRequest (id: string, symbol: Str = undefined, params = {}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're missing extending params before returning

const market = this.market (symbol);
const [ enableUnifiedMargin, enableUnifiedAccount ] = await this.isUnifiedEnabled ();
const isUnifiedAccount = (enableUnifiedMargin || enableUnifiedAccount);
const isUsdcSettled = market['settle'] === 'USDC';
if (isUsdcSettled && !isUnifiedAccount) {
return await this.cancelUsdcOrder (id, symbol, params);
}
const request = {
'symbol': market['id'],
// 'orderLinkId': 'string',
Expand All @@ -4254,6 +4238,34 @@ export default class bybit extends Exchange {
} else if (market['option']) {
request['category'] = 'option';
}
return request;
}

async cancelOrder (id: string, symbol: Str = undefined, params = {}) {
/**
* @method
* @name bybit#cancelOrder
* @description cancels an open order
* @see https://bybit-exchange.github.io/docs/v5/order/cancel-order
* @param {string} id order id
* @param {string} symbol unified symbol of the market the order was made in
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @param {boolean} [params.stop] *spot only* whether the order is a stop order
* @param {string} [params.orderFilter] *spot only* 'Order' or 'StopOrder' or 'tpslOrder'
* @returns {object} An [order structure]{@link https://docs.ccxt.com/#/?id=order-structure}
*/
if (symbol === undefined) {
throw new ArgumentsRequired (this.id + ' cancelOrder() requires a symbol argument');
}
await this.loadMarkets ();
const market = this.market (symbol);
const [ enableUnifiedMargin, enableUnifiedAccount ] = await this.isUnifiedEnabled ();
const isUnifiedAccount = (enableUnifiedMargin || enableUnifiedAccount);
const isUsdcSettled = market['settle'] === 'USDC';
if (isUsdcSettled && !isUnifiedAccount) {
return await this.cancelUsdcOrder (id, symbol, params);
}
const request = this.cancelOrderRequest (id, symbol, params);
const response = await this.privatePostV5OrderCancel (this.extend (request, params));
//
// {
Expand Down