user2864591
user2864591

Reputation: 86

Issue with KuCoin Futures API to create limit order

I've been working on a python solution to create limit orders on with requests on KuCoin futures API.

I've looked at the library: kucoin-futures-python-sdk, but I had issues with this so am testing with the provided example on the KuCoin Futures API.

I found that the code works on some tokens but not others. In Particular in the code below, it works with the commented 'working code' for token XCN. However the code for 'REEF' (which is not commented) produces error: 'Balance insufficient. The order would cost 6.0396000000.'

If I enter the exact same figures on KuCoin Futures REEF page, The order is accepted.

I believe the Calculation used in the API is not working correctly because the order would actually cost 30 cents on KuCoin, but do you agree? or is there an issue in my code?

*I know these are small amounts of money, but believe the principal would be the same with larger amounts.

import os
import requests
import json
import hmac
import hashlib
import base64
from urllib.parse import urlencode
import time
from urllib.request import urlretrieve
import kf_creds

api_key = kf_creds.api_key()
api_secret = kf_creds.api_secret()
api_passphrase = kf_creds.api_passphrase()

# Example for create deposit addresses in python
url = 'https://api-futures.kucoin.com/api/v1/orders'


# working code:
# token = 'XCN'
# pair = token + 'USDTM'
# clientOid = pair + '_' + str(int(time.time() * 1000))
# data = {
#     'clientOid': clientOid,
#     'side': 'buy',
#     'symbol': pair,
#     'leverage': 5,
#     'price': 0.01,
#     'size': 10
# }


# this code fails. Error : 'Balance insufficient. The order would cost 6.0396000000.'
token = 'REEF'
pair = token + 'USDTM'
clientOid = pair + '_' + str(int(time.time() * 1000))
data = {
    'clientOid': clientOid,
    'side': 'buy',
    'symbol': pair,
    'leverage': 5,
    'price': 0.003,
    'size': 100
}


now = int(time.time() * 1000)
data_json = json.dumps(data, separators=(',', ':'))
str_to_sign = str(now) + 'POST' + '/api/v1/orders' + data_json
signature = base64.b64encode(
    hmac.new(api_secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest())
passphrase = base64.b64encode(hmac.new(api_secret.encode(
    'utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest())
headers = {
    "KC-API-SIGN": signature,
    "KC-API-TIMESTAMP": str(now),
    "KC-API-KEY": api_key,
    "KC-API-PASSPHRASE": passphrase,
    "KC-API-KEY-VERSION": "2",
    # specifying content type or using json=data in request
    "Content-Type": "application/json"
}
response = requests.request('post', url, headers=headers, data=data_json)
print(response.status_code)
print(response.json())

I'm expecting the order to be placed, the same as it is on site, but the API request fails with Balance insufficient error

Upvotes: 0

Views: 888

Answers (1)

user2864591
user2864591

Reputation: 86

For anyone stuck on this - I found the answer!

you might assume that the 'size' order parameter behaves the same as the 'size' field on KuCoin Futures website.. well you'd be wrong.

Through testing I discovered that the 'size' value provided to the API, must be multiplied by the contract multiplier first.

Contract multiplier is available from the '/api/v1/contracts/active' endpoint, as 'multiplier' value.

for instance, the multiplier for REEF is 100, so the 'size' value sent to the API should be '1' to purchase 100 REEF:

data = {
    'clientOid': clientOid,
    'side': 'buy',
    'symbol': pair,
    'leverage': 5,
    'price': 0.003,
    'size': 1
}

Upvotes: 2

Related Questions