Search code examples
python-2.7python-requestshttp-postbitcoinrest

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


I need help to create orders using the bittrex version 3 REST API. I have the code below and I can't understand what is missing to work. I can make other GET calls, but I cannot make this POST request. I don't know how to deal with the passing of parameters.

Official documentation at 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)

And my error message:

{u'code': u'BAD_REQUEST', u'data': {u'invalidRequestParameter': u'direction'}, u'detail': u'Refer to the data field for specific field validation failures.'}

Solution

  • It seems from the documentation that this body is expected by the api as json data:

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

    and you are setting these values as url params that's the issue.

    you need to do this:

    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())
    
    

    If you still have issues let us know. If it works then please mark answer as accepted. Hope this helps.