'How to close a position in Kucoin using its Python SDK?

I am using kucoin-futures-python-sdk. I open a position by this command:

        x["open_order_id"]  = client.create_limit_order(my_symbol, open_side, my_leverage, x["trading_size"], x["entrance_price"])

When I go to close the position using this command:

        x["close_order_id"] = client.create_limit_order(my_symbol, "","","", x["tp3"], closeOrder = True, stopPrice = x["sl1"], stop = c_s_dir, stopPriceType = "TP", type = "limit")

Kucoin places a new stop order. I need a stop order which closes the previous position. According to Kucoin API Documentation, I have set closeOrder = True, correctly, but it is not working. How can I do that and close the position?



Solution 1:[1]

based on the documentation, you should only pass "clientOid","closeOrder": True and type of the closure. For example Passing these params worked for me:

params = {
    "clientOid": "5c52e11203aa677f33e493fb",
    "closeOrder": True,
    "symbol": "XBTUSDTM",
    "type": "market",
 }

Solution 2:[2]

If You want to close the order on market price, use create_market_order instead of create_limit_order.

If you want to set stop loss to close the order on certain price, you should make a stop order:

def setSl(exchange, symbol: str, side: str, size: int, sl: float):
    params = dict(
        stop='down',
        stopPriceType='TP',
        stopPrice=sl,
        reduceOnly=True,
    )
    side = 'sell' if side=='buy' else 'buy'
    try:
        res = exchange.createOrder(symbol, 'market', side, size, params=params)
    except Exception as e:
        print(e)
        return False
    return res

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahad Yekta
Solution 2 A.Dadkhah