在独立的保留比特币交易所玩限制订单的python代码是什么?

时间:2017-07-11 06:37:07

标签: python api bitcoin

在独立储备比特币交易所下限价单的python代码是什么?

这是我自己编写的代码,我不断收到{'消息':'无效凭据'}错误。有人能帮忙吗?

官方参考指南在这里:https://www.independentreserve.com/API#PlaceLimitOrder

import time,hmac,hashlib,requests,json

key = 'test123'
secret = 'test321'

primaryCurrencyCode="Xbt"
secondaryCurrencyCode="Aud"
orderType="LimitBid"
volume=0.88
price=100

url = 'https://api.independentreserve.com/Private/PlaceLimitOrder'
nonce=int(time.time()) 
parameters = [url,'apiKey='+key,'nonce='+str(nonce),"primaryCurrencyCode="+primaryCurrencyCode,"secondaryCurrencyCode="+secondaryCurrencyCode,"orderType="+orderType,"price="+str(price),"volume="+str(volume)]
message = ','.join(parameters) 
signature=hmac.new(secret.encode('utf-8'),msg=message.encode('utf-8'),digestmod=hashlib.sha256).hexdigest().upper() 
data = {"apiKey":key,"nonce": str(nonce),"signature": str(signature),"primaryCurrencyCode":primaryCurrencyCode,
        "secondaryCurrencyCode":secondaryCurrencyCode,"orderType":orderType,"price":price,"volume":volume}
headers={'Content-Type':'application/json'} 
r = requests.post(url, data=json.dumps(data,sort_keys=True), headers=headers).content
response_body = json.loads(r.decode())
print(response_body)  

1 个答案:

答案 0 :(得分:0)

实际上,我发现了问题。需要对参数进行排序..

其他人的工作代码参考:

import time,hmac,hashlib,requests,json

key = 'test123'
secret = 'test321'

primaryCurrencyCode="Xbt"
secondaryCurrencyCode="Aud"
orderType="LimitBid"
volume=0.88
price=100

url = 'https://api.independentreserve.com/Private/PlaceLimitOrder'
nonce=int(time.time()) 
parameters = [url] + sorted(['apiKey='+key,'nonce='+str(nonce),"primaryCurrencyCode="+primaryCurrencyCode,"secondaryCurrencyCode="+secondaryCurrencyCode,"orderType="+orderType,"price="+str(price),"volume="+str(volume)])
message = ','.join(parameters) 
signature=hmac.new(secret.encode('utf-8'),msg=message.encode('utf-8'),digestmod=hashlib.sha256).hexdigest().upper() 
data = {"apiKey":key,"nonce": str(nonce),"signature": str(signature),"primaryCurrencyCode":primaryCurrencyCode,
        "secondaryCurrencyCode":secondaryCurrencyCode,"orderType":orderType,"price":price,"volume":volume}
headers={'Content-Type':'application/json'} 
r = requests.post(url, data=json.dumps(data,sort_keys=True), headers=headers).content
response_body = json.loads(r.decode())
print(response_body)