用于Python的Bittrex REST API,我想使用API v3 https://api.bittrex.com/v3/orders创建订单

问题描述 投票:0回答:2

我需要使用bittrex版本3 REST API创建订单的帮助。我有下面的代码,我无法理解缺少的工作。我可以进行其他GET调用,但是无法发出此POST请求。我不知道如何处理参数的传递。

https://bittrex.github.io/api/v3#tag-Orders处的官方文档。

def NewOrder(market, amount, price):
#print 'open sell v3', market
market = 'HEDG-BTC'#'BTC-'+market
uri = 'https://api.bittrex.com/v3/orders?'
params = {
    'marketSymbol': 'BTC-HEDG',#'HEDG-BTC', #market
    'direction': 'BUY',
    'type': 'LIMIT',
    'quantity': amount,
    'limit': price,
    'timeInForce': 'POST_ONLY_GOOD_TIL_CANCELLED',
    'useAwards': True
}

timestamp = str(int(time.time()*1000))
Content = ""
contentHash = hashlib.sha512(Content.encode()).hexdigest()

Method = 'POST'
uri2 = buildURI(uri, params)
#uri2 = 'https://api.bittrex.com/v3/orders?direction=BUY&limit=0.00021&marketSymbol=HEDG-BTC&quantity=1.1&timeInForce=POST_ONLY_GOOD_TIL_CANCELLED&type=LIMIT&useAwards=True'
#print uri2
PreSign = timestamp + uri2 + Method + contentHash# + subaccountId
#print PreSign
Signature = hmac.new(apisecret, PreSign.encode(), hashlib.sha512).hexdigest()

headers = {
      'Api-Key' : apikey,
      'Api-Timestamp' : timestamp,
      'Api-Content-Hash': contentHash,
      'Api-Signature' : Signature
}

r = requests.post(uri2, data={}, headers=headers, timeout=11)
return json.loads(r.content)

NewOrder('HEDG',1.1,0.00021)

以及我的错误消息:

{u'code': u'BAD_REQUEST', u'data': {u'invalidRequestParameter': u'direction'}, u'detail': u'Refer to the data field for specific field validation failures.'}
python-2.7 python-requests http-post bitcoin restapi
2个回答
0
投票

[从文档中看来,此主体被api视为json数据:

{
  "marketSymbol": "string",
  "direction": "string",
  "type": "string",
  "quantity": "number (double)",
  "ceiling": "number (double)",
  "limit": "number (double)",
  "timeInForce": "string",
  "clientOrderId": "string (uuid)",
  "useAwards": "boolean"
}

并且您正在将这些值设置为url params

您需要这样做:

uri = 'https://api.bittrex.com/v3/orders'

# NOTE >>>> please check that you provide all the required fields.
payload = {
    'marketSymbol': 'BTC-HEDG',#'HEDG-BTC', #market
    'direction': 'BUY',
    'type': 'LIMIT',
    'quantity': amount,
    'limit': price,
    'timeInForce': 'POST_ONLY_GOOD_TIL_CANCELLED',
    'useAwards': True
}

# do rest of the stuffs as you are doing

# post payload as json data with the url given in doc
r = requests.post(uri, json=payload, headers=headers, timeout=11)
print(r.json())

如果您仍有问题,请告诉我们。如果有效,则将答案标记为已接受。希望这会有所帮助。


0
投票

我对代码进行了以下修改,但开始在“ Content-Hash”中出现错误

我假设某些参数是可选的,因此将其注释。

def NewOrder(market, amount, price):
market = 'BTC-'+market
uri = 'https://api.bittrex.com/v3/orders'

payload = {
    'marketSymbol': market,
    'direction': 'BUY',
    'type': 'LIMIT',
    'quantity': amount,
    #"ceiling": "number (double)",
    'limit': price,
    'timeInForce': 'POST_ONLY_GOOD_TIL_CANCELLED',
    #"clientOrderId": "string (uuid)",
    'useAwards': True
}
#ceiling (optional, must be included for ceiling orders and excluded for non-ceiling orders)
#clientOrderId (optional) client-provided identifier for advanced order tracking

timestamp = str(int(time.time()*1000))
Content = ""
contentHash = hashlib.sha512(Content.encode()).hexdigest()

Method = 'POST'
#uri2 = buildURI(uri, payload)#line not used
print uri
#PreSign = timestamp + uri2 + Method + contentHash# + subaccountId
PreSign = timestamp + uri + Method + contentHash# + subaccountId
print PreSign
Signature = hmac.new(apisecret, PreSign.encode(), hashlib.sha512).hexdigest()

headers = {
      'Api-Key' : apikey,
      'Api-Timestamp' : timestamp,
      'Api-Content-Hash': contentHash,
      'Api-Signature' : Signature
}

r = requests.post(uri, json=payload, headers=headers, timeout=11)
print(r.json())

return json.loads(r.content)

NewOrder('HEDG',1.5,0.00021)

{u'code':u'INVALID_CONTENT_HASH'}

© www.soinside.com 2019 - 2024. All rights reserved.