库币原料药'Invalid KC-API-SIGN'



api_secret等在运行代码中不留空。却把它藏在这里。获取输出:{'code': '400005', 'msg': '无效的KC-API-SIGN'}它适用于其他POST订单,例如。买/卖。

def borrow_margin(currency, amount):
api_key = ****************
api_secret = ****************
api_passphrase = ****************
url = 'https://api.kucoin.com/api/v1/margin/borrow'
order = {'currency':currency,'type':'FOK','size':amount}
order_json = json.dumps(order)
now = int(time.time() * 1000)
str_to_sign = str(now) + 'POST' + '/api/v1/margin/borrow' + order_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"
}
req = requests.request('post', url, headers=headers).json()
print(req)

borrow_margin('USDT', 50.0)

输出:{"代码":"400005","味精":"无效的KC-API-SIGN"}

应为:Req =请求。request('POST', url=url, headers=headers, data=order_json).json()

这个对我来说也是个难题。这是我目前的期货API代码:如何获取账户信息,如何开仓,以及如何平仓。

关于KC_place_order(current, side, amount)的澄清:*参数'curr'指的是你正在使用的交易对:例如一个字符串值,如'FTM-USDT', 'ETH-USDT'等。*'side'是用于多头或空头位置的字符串值:例如:"买"或"卖"*"金额"是您想要交易的货币数量的浮动值。

代码并不完美,我知道,但它在工作。代码将略有不同的现场API顺便说一句:问我,如果有人想让我张贴。

祝你好运!=)

import time
import base64
import hmac
import hashlib
import requests
import json
import uuid
import datetime
from dateutil import parser
import pandas as pd
import numpy as np
import os
def get_equity_account(): 
client0id = uuid.uuid4().hex
client0id = str(client0id)
api_key = "your api_key"
api_secret = "your api_secret"
api_passphrase = "your api_passphrase"
#Get account equity in USDT
url = 'https://api-futures.kucoin.com/api/v1/account-overview?currency=USDT'
now = int(time.time() * 1000)
data = {"clientOid": client0id,'currency':'FTM'}
data_json = json.dumps(data)
str_to_sign = str(now) + 'GET' + '/api/v1/account-overview?currency=USDT' + 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" # specifying content type or using json=data in request
}
response = requests.request('GET', url, headers=headers, data=data_json)
# print(response.status_code)
# pprint(response.json())
# print(response.json()['data']['accountEquity'])
equity = float(response.json()['data']['accountEquity'])
return equity
# server message example: 
#     {'code': '200000',
#  'data': {'accountEquity': 19834.517194515,
#           'availableBalance': 19834.517194515,
#           'currency': 'USDT',
#           'frozenFunds': 0,
#           'marginBalance': 19834.517194515,
#           'orderMargin': 0.0,
#           'positionMargin': 0,
#           'unrealisedPNL': 0}}

def KC_place_order(curr, side, amount):
leverage = '< float with whatever leverage you prefer >'
# curr_URL = curr.replace('-', '') + 'M' - only for futures trading. Original curr string for spot
curr_replace = curr.replace('-','_')
curr_URL = curr.replace('-', '') + 'M'
curr_equity_symbol = curr.replace('-USDT', '')
client0id = uuid.uuid4().hex
client0id = str(client0id)
api_key = "your api_key"
api_secret = "your api_secret"
api_passphrase = "your api_passphrase"
url = 'https://api-futures.kucoin.com/api/v1/orders'
now = int(time.time() * 1000)
data = {"clientOid": client0id, "symbol": curr_URL, "side": side, "type": "market", 'leverage': leverage, "size": amount} #how to set stop loss? Check dev docs!
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" # specifying content type or using json=data in request
}
response = requests.request('POST', url, headers=headers, data=data_json)
print(response.status_code)
print(response.json())
#"closeOrder": True,
#"side": "sell"
#'leverage': 5
#data = {"clientOid": client0id,"symbol": "FTMUSDTM", "side": "buy", "type": "market",'leverage': '5', "size": 10}
#KC_place_order(curr, side, amount, equity_deal)
return response
def KC_close_position(curr):
#get equity of a certain currency
#sell the enirety of that equity
curr_URL = curr.replace('-', '') + 'M'
curr_replace = curr.replace('-','_')
curr_equity_symbol = curr.replace('-USDT', '')
client0id = uuid.uuid4().hex
client0id = str(client0id)
api_key = "your api_key"
api_secret = "your api_secret"
api_passphrase = "your api_passphrase"
url = 'https://api-futures.kucoin.com/api/v1/orders'
now = int(time.time() * 1000)
data = {"clientOid": client0id,"symbol": curr_URL, "type": "market", "closeOrder": True,} 
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" # specifying content type or using json=data in request
}
response = requests.request('POST', url, headers=headers, data=data_json)
print(response.status_code)
print(response.json())

return response

最新更新