Skip to content

Commit

Permalink
[Kucoin] handle cross positions parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Nov 25, 2024
1 parent addc9ae commit 3951803
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Trading/Exchange/kucoin/kucoin_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,21 @@ async def create_order(self, order_type: trading_enums.TraderOrderType, symbol:
side: trading_enums.TradeOrderSide = None, current_price: decimal.Decimal = None,
reduce_only: bool = False, params: dict = None) -> typing.Optional[dict]:
if self.exchange_manager.is_future:
params = params or {}
# on futures exchange expects, quantity in contracts: convert quantity into contracts
quantity = quantity / self.get_contract_size(symbol)
try:
# "marginMode": "ISOLATED" // Added field for margin mode: ISOLATED, CROSS, default: ISOLATED
# from https://www.kucoin.com/docs/rest/futures-trading/orders/place-order
if (
KucoinCCXTAdapter.KUCOIN_MARGIN_MODE not in params and
self.exchange_manager.exchange_personal_data.positions_manager.get_symbol_position_margin_type(
symbol
) is trading_enums.MarginType.CROSS
):
params[KucoinCCXTAdapter.KUCOIN_MARGIN_MODE] = "CROSS"
except ValueError as err:
self.logger.error(f"Impossible to add {KucoinCCXTAdapter.KUCOIN_MARGIN_MODE} to order: {err}")
return await super().create_order(order_type, symbol, quantity,
price=price, stop_price=stop_price,
side=side, current_price=current_price,
Expand Down Expand Up @@ -455,6 +468,7 @@ class KucoinCCXTAdapter(exchanges.CCXTAdapter):

# ORDER
KUCOIN_LEVERAGE = "leverage"
KUCOIN_MARGIN_MODE = "marginMode"

def fix_order(self, raw, symbol=None, **kwargs):
raw_order_info = raw[ccxt_enums.ExchangePositionCCXTColumns.INFO.value]
Expand Down Expand Up @@ -522,13 +536,9 @@ def parse_funding_rate(self, fixed, from_ticker=False, **kwargs):
def parse_position(self, fixed, **kwargs):
raw_position_info = fixed[ccxt_enums.ExchangePositionCCXTColumns.INFO.value]
parsed = super().parse_position(fixed, **kwargs)
parsed[trading_enums.ExchangeConstantsPositionColumns.MARGIN_TYPE.value] = \
trading_enums.MarginType(
fixed.get(ccxt_enums.ExchangePositionCCXTColumns.MARGIN_MODE.value)
)
parsed[trading_enums.ExchangeConstantsPositionColumns.POSITION_MODE.value] = \
trading_enums.PositionMode.HEDGE if raw_position_info[self.KUCOIN_AUTO_DEPOSIT] \
else trading_enums.PositionMode.ONE_WAY
parsed[trading_enums.ExchangeConstantsPositionColumns.AUTO_DEPOSIT_MARGIN.value] = (
raw_position_info.get(self.KUCOIN_AUTO_DEPOSIT, False) # unset for cross positions
)
parsed_leverage = self.safe_decimal(
parsed, trading_enums.ExchangeConstantsPositionColumns.LEVERAGE.value, constants.ZERO
)
Expand Down

0 comments on commit 3951803

Please sign in to comment.