0

I tried to create a simple trading execution code for one-time spot market buy order in Python (using Pycharm).

The code is provided below. API key, API passphrase and API secret key are correct, as well as a signature encoded in base64. The problem seems to be with the order URL. I use this one (provided by Kucoin API documentation): api/v1/orders

The error I'm getting is:

Traceback (most recent call last):
  File "C:\Users\xxxx\PycharmProjects\pythonProject11\main 2.py", line 57, in <module>
    response = client.request('POST', url, body=body_str, headers=headers)
  File "C:\Users\xxxx\PycharmProjects\pythonProject11\venv\lib\site-packages\ccxt\base\exchange.py", line 2801, in request
    return self.fetch2(path, api, method, params, headers, body, config, context)
  File "C:\Users\xxxx\PycharmProjects\pythonProject11\venv\lib\site-packages\ccxt\base\exchange.py", line 2797, in fetch2
    request = self.sign(path, api, method, params, headers, body)
  File "C:\Users\xxxx\PycharmProjects\pythonProject11\venv\lib\site-packages\ccxt\kucoin.py", line 3259, in sign
    url = self.urls['api'][api]
KeyError: '/api/v1/orders'

Process finished with exit code 1

Code:

import ccxt
import base64
import hashlib
import hmac
import json
from datetime import datetime

# Replace these with your own API key, secret key, and passphrase
api_key = ''
api_secret = ''
api_passphrase = ''

# Set up the Kucoin client
client = ccxt.kucoin()
client.apiKey = api_key
client.secret = api_secret
client.version = 'v1'

# Set the request parameters -- I know there should be a specific token in place of XXXX
symbol = 'XXXX/USDT'
side = 'buy'
type = 'market'
amount = 1.0

# Generate the timestamp in ISO 8601 format
timestamp = datetime.utcnow().isoformat() + 'Z'

# Construct the request body
body = {
  'clientOid': 'YOUR_UNIQUE_ID',
  'size': amount,
  'side': side,
  'symbol': symbol,
  'type': type
}

# Encode the request body as a JSON string
body_str = json.dumps(body)

# Set the request URL
url = '/api/v1/orders'

# Construct the signature
message = timestamp + 'POST' + url + body_str
signature = base64.b64encode(hmac.new(bytes(api_secret, 'latin-1'), bytes(message, 'latin-1'), digestmod=hashlib.sha256).digest())

# Set the request headers
headers = {
  'KC-API-KEY': api_key,
  'KC-API-SIGN': signature,
  'KC-API-TIMESTAMP': timestamp,
  'KC-API-PASSPHRASE': api_passphrase,
  'Content-Type': 'application/json'
}

# Send the POST request to the "POST /api/v1/orders" URL
response = client.request('POST', url, body=body_str, headers=headers)

# Print the response
print(response)

I tried various URLs but it doesn't seem to work. What is causing the problem?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
qas2100
  • 61
  • 1
  • 4
  • That's not a full URL though. The API is being hosted at a specific endpoint; your URL needs to include its domain name or IP address. Where is your API endpoint and why is its address not included in the URL? – Random Davis Dec 21 '22 at 19:36
  • Yeah @RandomDavis but shouldn't the ccxt library abstract the process of making requests to the Kucoin API, so the request URL does not need to include the API endpoint or domain name??? – qas2100 Dec 21 '22 at 19:51
  • I have no idea, I'm just basing this on the [API Documentation](https://docs.kucoin.com/#authentication) which has the domain name in all its urls, like `'https://openapi-sandbox.kucoin.com/api/v1/accounts'`. Can you show where in the documentation it says that you can omit the hostname? How would the API know the hostname if you never provided it anywhere? Some config file maybe? – Random Davis Dec 21 '22 at 19:52
  • @RandomDavis There is this in the [ccxt documentation](https://docs.ccxt.com/en/latest/manual.html#api-methods-endpoints) which says that 'The recommended way of working with exchanges is not using exchange-specific implicit methods but using the unified ccxt methods instead.' At least that's how I understood it. – qas2100 Dec 21 '22 at 20:05
  • They're not talking about a hostname at all, they're talking about method names. They're saying how each method name gets generated, and how to access them - specifically "Each generated method will be accessible in both `camelCase` and `under_score` notations." But you're not doing that at all. It says you get the list of method names via `print(dir(ccxt.kraken()))` but you're not doing that at all. I think you're reading the documentation too fast and assuming a lot of things that aren't true. I think you should slow down and make sure you're understanding each step and all the terminology. – Random Davis Dec 21 '22 at 20:33
  • @RandomDavis Thanks, I'll try to slow down a little bit. Alternatively do you think that scrapping ccxt library could be a workaround if I still get problems? – qas2100 Dec 21 '22 at 20:39
  • I don't know enough to offer advice one way or the other, sorry. – Random Davis Dec 21 '22 at 20:45
  • 1
    @RandomDavis Nevertheless I appreciate for your time and your input. I'm grateful for it. – qas2100 Dec 21 '22 at 20:58

0 Answers0