title: I have a problem in Trading with Kucoin API (Python)
body: <p>I tried to do API trading in Kucoin. I developed a bot that is finding the trading opportunities well, while I encountered problems in making new orders. Pls check the code and help me to make it functional.</p>
  <p>The code was edited in accordance with the comment of <a href="https://stackoverflow.com/users/1258041/lev-levitsky">Lev Levitsky
  </a>  is as follows:</p>
  <pre class="lang-py prettyprint-override"><code>import json
  import urllib
  import requests
  
  import base64
  import hmac
  import hashlib
  
  api_key        = 'api_key'
  api_secret     = 'api_secret'
  api_passphrase = 'api_passphrase'
  base_uri       = 'https://api-futures.kucoin.com'
  endpoint       = '/api/v1/orders?symbol=MATICUSDTM'
  method         = 'POST'
  
  x= {}
  x[&quot;symbol&quot;]         = &quot;MATICUSDTM&quot;
  x[&quot;signal_type&quot;]    = &quot;SHORT&quot;
  x[&quot;leverage&quot;]       = 5
  x[&quot;exchange&quot;]       = &quot;Kucoin&quot;
  x[&quot;entrance_price&quot;] = 2.1000
  x[&quot;trading_size&quot;]   = 150
  x[&quot;tp1&quot;]            = 2.08
  x[&quot;sl1&quot;]            = 2.12
  
  all_futures_signals = list()
  all_futures_signals.append(x)
  
  def get_headers(method, endpoint, api_key, api_passphrase,body):
      api_secret     = ''
      now = int(time.time() * 1000)
      str_to_sign = str(now) + method + endpoint + str(body)
  
      signature  = base64.b64encode(hmac.new(api_secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest())
      passphrase = base64.b64encode(hmac.new(api_secret.encode('utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest())
  
      return {'KC-API-KEY': api_key,
              'KC-API-KEY-VERSION': '2',
              'KC-API-PASSPHRASE': passphrase,
              'KC-API-SIGN': signature,
              'KC-API-TIMESTAMP': str(now)}
  
  
  body = {
      &quot;clientOid&quot;    : &quot;&quot;,
      &quot;reduceOnly&quot;   : False,   # A mark to reduce the position size only
      &quot;closeOrder&quot;   : False,   # If closeOrder is set to TRUE, the system will close the position and the position size will become 0. Side, Size and Leverage fields can be left empty and the system will determine the side and size automatically.
      &quot;forceHold&quot;    : False,   # The system will forcely freeze certain amount of funds for this order, including orders whose direction is opposite to the current positions. This feature is to ensure that the order won’t be canceled by the matching engine in such a circumstance that not enough funds are frozen for the order.
      &quot;hidden&quot;       : False,   # A hidden order will enter but not display on the orderbook.
      &quot;iceberg&quot;      : False,   # When placing an iceberg order, you need to set the visible size. The minimum visible size is 1/20 of the order size. The minimum visible size shall be greater than the minimum order size, or an error will occur.
      &quot;visibleSize&quot;  : 0,       # When placing an iceberg order, you need to set the visible size. The minimum visible size is 1/20 of the order size. The minimum visible size shall be greater than the minimum order size, or an error will occur.
      &quot;leverage&quot;     : x[&quot;leverage&quot;],
      &quot;postOnly&quot;     : False,   # The post-only flag ensures that the trader always pays the maker fee and provides liquidity to the order book.
      &quot;price&quot;        : 2.1000, # The price specified must be a multiple number of the contract tickSize,
      &quot;remark&quot;       : &quot;remark&quot;,
      &quot;side&quot;         : &quot;buy&quot;,# sell/buy
      &quot;size&quot;         : x[&quot;trading_size&quot;],      # The size must be no less than the lotSize for the contract and no larger than the maxOrderQty.
      &quot;stop&quot;         : &quot;&quot;,      # down/up
      &quot;stopPrice&quot;    : &quot;&quot;,
      &quot;stopPriceType&quot;: &quot;&quot;,      # TP/MP/IP: TP for trade price, MP for mark price, and IP for index price
      &quot;symbol&quot;       : x[&quot;symbol&quot;],
      &quot;timeInForce&quot;  : &quot;&quot;,      # GTC/IOC: Good Till Canceled GTC and Immediate Or Cancel IOC.
      &quot;type&quot;         : &quot;limit&quot;, # limit/market
  }
  
  headers = get_headers(method, endpoint, api_key, api_passphrase, body)
  x[&quot;opening_response&quot;] = requests.post( base_uri + endpoint, body, headers=headers).json()
  print(x[&quot;opening_response&quot;])
  </code></pre>
  <p>I receive this error: {'code': '400005', 'msg': 'Invalid KC-API-SIGN'}</p>
  <p>All inputs are correct. I think there is a problem with the code.</p>
  <p>Best Regards
  Javad</p>