繁体   English   中英

我在与 Kucoin API (Python) 交易时遇到问题

[英]I have a problem in Trading with Kucoin API (Python)

我尝试在 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

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

更简单的方法是使用 Kucoin API 的客户端。 它就在这里

你考虑过使用ccxt吗? 它使处理像这样的较低级别的 API 的东西更容易一些。

鉴于它将签名单选为无效,但没有说缺少任何标头,这可能意味着 str_to_sign 变量的签名是错误的?

让我们看一下:

api_secret     = ''
now = int(time.time() * 1000)
str_to_sign = str(now) + method + endpoint + str(body)

从外观上看,您的 api_secret 只是一个空字符串,因此生成的签名将不正确。

因此,当您进行签名时,请向下几行:

    signature  = base64.b64encode(hmac.new(api_secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest())

即使 api_secret 在较高的 scope 中被赋予了一个值,它也会被这个空字符串的局部变量覆盖。 如果您的 api_secret 实际上是一个空字符串,那么您的代码将产生正确的签名,但事实并非如此

因此,如果您将其作为参数提供给您的 get_headers function 即。

def get_headers(method, endpoint, api_key, api_passphrase, api_secret, body):

并删除第一行api_secret = ''

那么也许它会起作用?

如果它不起作用,那么这是一个不同的问题。 (例如,如果您实际上已将 API 密钥放在那里并在发布前对其进行了编辑)。 我不知道,因为我没有尝试运行你的代码。

PS:致雪绒花:Grüzi aus Berner Oberland!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM