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

Bittrex REST API for Python, I want to create an order using API v3 https://api.bittrex.com/v3/orders

我需要帮助来使用 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.'}

从文档中可以看出,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())

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

我对代码进行了以下修改,但在 '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 = ''+json.dumps(payload, separators=(',',':'))
print 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'代码': u'INVALID_CONTENT_HASH'}