在python中创建一个order kucoin API


import requests, json, time

url = 'https://api.kucoin.com/api/v1/orders'
headers = {
"KC-API-KEY": '',
"KC-API-PASSPHRASE": '',
"clientOid": "AAA",
"side": "sell",
"symbol": "BTC-USDT",
"type": "market",
"size": "0.001",

}
response = requests.post(url, headers=headers)
print(response.status_code)
print(response.json())

我正试着下订单,但它不工作。我是不是漏掉了一些参数?

错误:

{'code': '400001', 'msg': 'Please check the header of your request for KC-API-KEY, KC-API-SIGN, KC-API-TIMESTAMP, KC-API-PASSPHRASE'}

根据官方文档,所有私人请求必须包含以下头:

  • KC-API-KEY
  • KC-API-SIGN
  • KC-API-TIMESTAMP
  • KC-API-PASSPHRASE
  • KC-API-VERSION

下面是一个放置订单限制的端点示例:

import base64, hmac, hashlib, json
# constants
API_KEY = "YOUR_API_KEY"
API_SECRET = "YOUR_API_SECRET"
API_PASSPHRASE = "YOUR_API_PASSPHRASE"
url = "https://api.kucoin.com/api/v1/orders"
now = int(time.time() * 1000)
data = {"clientOid": "AAA", "side": "sell", "symbol": "BTC-USDT", "type": "market", "size": "0.001"}
data_json = json.dumps(data)
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",
"Content-Type": "application/json"
}
try:
res = requests.post(
url, headers=headers, data=data_json).json()
print(res)
except Exception as err:
print(err)

希望对你有帮助。

你考虑过使用像Python-kucoin这样的包装库吗?https://python-kucoin.readthedocs.io/en/stable/index.html

它真的很棒,一定会对你有帮助。查看一下文档

from kucoin.client import Client
api_key = '<api_key>'
api_secret = '<api_secret>'
api_passphrase = '<api_passphrase>'
client = Client(api_key, api_secret, api_passphrase)
# place a market buy order
order = client.create_market_order('BTC-USDT', Client.SIDE_BUY, size=0.001)

尝试删除:data = {"clientOid";AAA" side";sell";symbol";BTC-USDT";type";market";size";0.001"}

相关内容

  • 没有找到相关文章

最新更新