From 9d687fc41ac57047f9b13abef4bf21b57f9228ad Mon Sep 17 00:00:00 2001 From: Dan Date: Sat, 13 May 2023 12:02:24 -0600 Subject: [PATCH] feat(kucoinfutures): fetchPosition Added fetchPosition to kucoinfutures: ``` kucoinfutures.fetchPosition (BTC/USDT:USDT) 2023-05-13T17:58:23.847Z iteration 0 passed in 417 ms { info: { id: '63b3599e6c41f50001c47d44', symbol: 'XBTUSDTM', autoDeposit: false, maintMarginReq: 0.004, riskLimit: 25000, realLeverage: 5.02, crossMode: false, delevPercentage: 0.54, openingTimestamp: 1684000025528, currentTimestamp: 1684000705325, currentQty: 1, currentCost: 26.821, currentComm: 0.0160926, unrealisedCost: 26.821, realisedGrossCost: 0, realisedCost: 0.0160926, isOpen: true, markPrice: 26802.8, markValue: 26.8028, posCost: 26.821, posCross: 0, posCrossMargin: 0, posInit: 5.3642, posComm: 0.01931112, posCommCommon: 0.01931112, posLoss: 0, posMargin: 5.38351112, posMaint: 0.12927722, maintMargin: 5.36531112, realisedGrossPnl: 0, realisedPnl: -0.0160926, unrealisedPnl: -0.0182, unrealisedPnlPcnt: -0.0007, unrealisedRoePcnt: -0.0034, avgEntryPrice: 26821, liquidationPrice: 21567, bankruptPrice: 21456, settleCurrency: 'USDT', isInverse: false, maintainMargin: 0.004 }, id: '63b3599e6c41f50001c47d44', symbol: 'BTC/USDT:USDT', timestamp: 1684000705325, datetime: '2023-05-13T17:58:25.325Z', lastUpdateTimestamp: undefined, initialMargin: 5.3642, initialMarginPercentage: 0.2, maintenanceMargin: 0.12927722, maintenanceMarginPercentage: 0.004, entryPrice: 26821, notional: 26.821, leverage: 5.02, unrealizedPnl: -0.0182, contracts: 1, contractSize: 0.001, marginRatio: undefined, liquidationPrice: 21567, markPrice: 26802.8, lastPrice: undefined, collateral: 5.36531112, marginMode: 'isolated', side: 'long', percentage: undefined } ``` --- ts/src/kucoinfutures.ts | 73 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/ts/src/kucoinfutures.ts b/ts/src/kucoinfutures.ts index efc747c10abd..0a8dc027886f 100644 --- a/ts/src/kucoinfutures.ts +++ b/ts/src/kucoinfutures.ts @@ -66,6 +66,7 @@ export default class kucoinfutures extends kucoin { 'fetchOpenOrders': true, 'fetchOrder': true, 'fetchOrderBook': true, + 'fetchPosition': true, 'fetchPositionMode': false, 'fetchPositions': true, 'fetchPremiumIndexOHLCV': false, @@ -859,11 +860,81 @@ export default class kucoinfutures extends kucoin { return fees; } + async fetchPosition (symbol: string, params = {}) { + /** + * @method + * @name kucoinfutures#fetchPosition + * @see https://docs.kucoin.com/futures/#get-position-details + * @description fetch data on an open position + * @param {string} symbol unified market symbol of the market the position is held in + * @param {object} params extra parameters specific to the kucoinfutures api endpoint + * @returns {object} a [position structure]{@link https://docs.ccxt.com/en/latest/manual.html#position-structure} + */ + await this.loadMarkets (); + const market = this.market (symbol); + const request = { + 'symbol': market['id'], + }; + const response = await this.futuresPrivateGetPositions (this.extend (request, params)); + // + // { + // "code": "200000", + // "data": [ + // { + // "id": "63b3599e6c41f50001c47d44", + // "symbol": "XBTUSDTM", + // "autoDeposit": false, + // "maintMarginReq": 0.004, + // "riskLimit": 25000, + // "realLeverage": 5.0, + // "crossMode": false, + // "delevPercentage": 0.57, + // "openingTimestamp": 1684000025528, + // "currentTimestamp": 1684000052160, + // "currentQty": 1, + // "currentCost": 26.821, + // "currentComm": 0.0160926, + // "unrealisedCost": 26.821, + // "realisedGrossCost": 0.0, + // "realisedCost": 0.0160926, + // "isOpen": true, + // "markPrice": 26821.13, + // "markValue": 26.82113, + // "posCost": 26.821, + // "posCross": 0.0, + // "posCrossMargin": 0.0, + // "posInit": 5.3642, + // "posComm": 0.01931112, + // "posCommCommon": 0.01931112, + // "posLoss": 0.0, + // "posMargin": 5.38351112, + // "posMaint": 0.12927722, + // "maintMargin": 5.38364112, + // "realisedGrossPnl": 0.0, + // "realisedPnl": -0.0160926, + // "unrealisedPnl": 1.3E-4, + // "unrealisedPnlPcnt": 0.0, + // "unrealisedRoePcnt": 0.0, + // "avgEntryPrice": 26821.0, + // "liquidationPrice": 21567.0, + // "bankruptPrice": 21456.0, + // "settleCurrency": "USDT", + // "isInverse": false, + // "maintainMargin": 0.004 + // } + // ] + // } + // + const data = this.safeValue (response, 'data', []); + return this.parsePosition (data[0], market); + } + async fetchPositions (symbols: string[] = undefined, params = {}) { /** * @method * @name kucoinfutures#fetchPositions * @description fetch all open positions + * @see https://docs.kucoin.com/futures/#get-position-list * @param {[string]|undefined} symbols list of unified market symbols * @param {object} params extra parameters specific to the kucoinfutures api endpoint * @returns {[object]} a list of [position structure]{@link https://docs.ccxt.com/#/?id=position-structure} @@ -987,7 +1058,7 @@ export default class kucoinfutures extends kucoin { const marginMode = crossMode ? 'cross' : 'isolated'; return this.safePosition ({ 'info': position, - 'id': undefined, + 'id': this.safeString (position, 'id'), 'symbol': this.safeString (market, 'symbol'), 'timestamp': timestamp, 'datetime': this.iso8601 (timestamp),