2

我尝试在 Kucoin 中进行 API 交易。我开发了一个机器人,它很好地找到了交易机会,而我在下新订单时遇到了问题。请检查代码并帮助我使其正常运行。

代码根据Lev Levitsky 的注释编辑 如下:

import json
import urllib
import requests

import base64
import hmac
import hashlib

api_key        = 'api_key'
api_secret     = 'api_secret'
api_passphrase = 'api_passphrase'
base_uri       = 'https://api-futures.kucoin.com'
endpoint       = '/api/v1/orders?symbol=MATICUSDTM'
method         = 'POST'

x= {}
x["symbol"]         = "MATICUSDTM"
x["signal_type"]    = "SHORT"
x["leverage"]       = 5
x["exchange"]       = "Kucoin"
x["entrance_price"] = 2.1000
x["trading_size"]   = 150
x["tp1"]            = 2.08
x["sl1"]            = 2.12

all_futures_signals = list()
all_futures_signals.append(x)

def get_headers(method, endpoint, api_key, api_passphrase,body):
    api_secret     = ''
    now = int(time.time() * 1000)
    str_to_sign = str(now) + method + endpoint + str(body)

    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())

    return {'KC-API-KEY': api_key,
            'KC-API-KEY-VERSION': '2',
            'KC-API-PASSPHRASE': passphrase,
            'KC-API-SIGN': signature,
            'KC-API-TIMESTAMP': str(now)}


body = {
    "clientOid"    : "",
    "reduceOnly"   : False,   # A mark to reduce the position size only
    "closeOrder"   : False,   # If closeOrder is set to TRUE, the system will close the position and the position size will become 0. Side, Size and Leverage fields can be left empty and the system will determine the side and size automatically.
    "forceHold"    : False,   # The system will forcely freeze certain amount of funds for this order, including orders whose direction is opposite to the current positions. This feature is to ensure that the order won’t be canceled by the matching engine in such a circumstance that not enough funds are frozen for the order.
    "hidden"       : False,   # A hidden order will enter but not display on the orderbook.
    "iceberg"      : False,   # When placing an iceberg order, you need to set the visible size. The minimum visible size is 1/20 of the order size. The minimum visible size shall be greater than the minimum order size, or an error will occur.
    "visibleSize"  : 0,       # When placing an iceberg order, you need to set the visible size. The minimum visible size is 1/20 of the order size. The minimum visible size shall be greater than the minimum order size, or an error will occur.
    "leverage"     : x["leverage"],
    "postOnly"     : False,   # The post-only flag ensures that the trader always pays the maker fee and provides liquidity to the order book.
    "price"        : 2.1000, # The price specified must be a multiple number of the contract tickSize,
    "remark"       : "remark",
    "side"         : "buy",# sell/buy
    "size"         : x["trading_size"],      # The size must be no less than the lotSize for the contract and no larger than the maxOrderQty.
    "stop"         : "",      # down/up
    "stopPrice"    : "",
    "stopPriceType": "",      # TP/MP/IP: TP for trade price, MP for mark price, and IP for index price
    "symbol"       : x["symbol"],
    "timeInForce"  : "",      # GTC/IOC: Good Till Canceled GTC and Immediate Or Cancel IOC.
    "type"         : "limit", # limit/market
}

headers = get_headers(method, endpoint, api_key, api_passphrase, body)
x["opening_response"] = requests.post( base_uri + endpoint, body, headers=headers).json()
print(x["opening_response"])

我收到此错误:{'code': '400005', 'msg': 'Invalid KC-API-SIGN'}

所有输入都是正确的。我认为代码有问题。

最好的问候 Javad

4

2 回答 2

0

我认为问题是你的endpoint变量。当您尝试添加新订单时,我相信您不应该将符号添加到端点。将其从端点中移除并在 body 对象中传递符号。另一件事是我认为您不需要将空字符串传递给body对象中的可选字段。我不确定这一点,但我认为你应该将它们从body对象中删除。这是一个签名问题,因此您必须检查 4 个变量:timestampmethod和。我希望这行得通。endpointbody

于 2022-01-17T11:07:03.057 回答
-1

根据 edelweiss 的评论更简单的方法是使用 Kucoin API 的客户端。它在这里

首先,我使用此代码安装了客户端。

!pip install kucoin-futures-python

然后,我通过以下代码开仓:

from kucoin_futures.client import Trade

client = Trade(key='api_key', secret='api_secret', passphrase='api_passphrase', is_sandbox=False, url='')
order_id = client.create_limit_order(symbol, side, lever, size, price, clientOid='', **kwargs)
于 2022-02-03T11:12:47.663 回答