Skip to content

Commit

Permalink
feat(bybit): add ws crud (#22313)
Browse files Browse the repository at this point in the history
* feat(bybit): add ws crud

* fix static tests

* remove safeValue

---------

Co-authored-by: carlosmiei <[email protected]>
  • Loading branch information
pcriadoperez and carlosmiei committed Apr 28, 2024
1 parent b4ec5df commit dcd92ad
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 75 deletions.
136 changes: 74 additions & 62 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 = {}) {
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 @@ -4238,7 +4222,7 @@ export default class bybit extends Exchange {
};
if (market['spot']) {
// only works for spot market
const isStop = this.safeValue2 (params, 'stop', 'trigger', false);
const isStop = this.safeBool2 (params, 'stop', 'trigger', false);
params = this.omit (params, [ 'stop', 'trigger' ]);
request['orderFilter'] = isStop ? 'StopOrder' : 'Order';
}
Expand All @@ -4254,7 +4238,35 @@ export default class bybit extends Exchange {
} else if (market['option']) {
request['category'] = 'option';
}
const response = await this.privatePostV5OrderCancel (this.extend (request, params));
return this.extend (request, params);
}

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 requestExtended = this.cancelOrderRequest (id, symbol, params);
const response = await this.privatePostV5OrderCancel (requestExtended);
//
// {
// "retCode": 0,
Expand Down

0 comments on commit dcd92ad

Please sign in to comment.