Sumtinlazy
Sumtinlazy

Reputation: 347

How to list active orders kucoin

Im new to using API's and have been assigned to try and assemble a trading algorithm using Kucoin API. I am having trouble trying to list the active orders. Any help appreciated

request = Request('https://api.kucoin.com/v1/open/orders-buy')

response_body = urlopen(request).read()
print response_body

Console is returning this error

Traceback (most recent call last):
  File "/Users/thomas/Documents/Github Repo/algo.py", line 5, in  <module>
    response_body = urlopen(request).read()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: 

Upvotes: 3

Views: 1544

Answers (2)

blackraven
blackraven

Reputation: 5627

Here are my working codes in Python 3:

import requests
import json
import hmac
import hashlib
import base64
from urllib.parse import urlencode
import time

api_key = 'xxxxx'
api_secret = 'xx-xxx-xx'
api_passphrase = 'xxx'   #this is NOT trading password
base_uri = 'https://api.kucoin.com'

def get_headers(method, endpoint):
    now = int(time.time() * 1000)
    str_to_sign = str(now) + method + endpoint
    signature = base64.b64encode(hmac.new(api_secret.encode(), str_to_sign.encode(), hashlib.sha256).digest()).decode()
    passphrase = base64.b64encode(hmac.new(api_secret.encode(), api_passphrase.encode(), hashlib.sha256).digest()).decode()
    return {'KC-API-KEY': api_key,
            'KC-API-KEY-VERSION': '2',
            'KC-API-PASSPHRASE': passphrase,
            'KC-API-SIGN': signature,
            'KC-API-TIMESTAMP': str(now)
    }

#List Orders
method = 'GET'
# endpoint = '/api/v1/orders'   #alternative endpoint
endpoint = '/api/v1/orders?status=active'
response = requests.request(method, base_uri+endpoint, headers=get_headers(method,endpoint))
print(response.status_code)
response.json()

Output

200
{'code': '200000', 'data': {'currentPage': 1, 'pageSize': 50, 'totalNum': 0, 'totalPage': 0, 'items': [blah blah blah]}}

Upvotes: 1

Vera
Vera

Reputation: 84

It is missing attributes, as per their documentation.

Upvotes: 2

Related Questions