CCXT okex5 swap contracts - Parameter posSide error
Asked Answered
E

3

7

I am trying to create a perpetual limit order using ccxt on okex exchange version 5. The API is successfully placing orders(both limit and market) on spot exchange but not on perpetual exchange. Here is what I am trying -

exchange_swap = ccxt.okex5({
'apiKey': credentials['okex']['apikey'],
'secret': credentials['okex']['secretkey'],
'password': credentials['okex']['password'],
'options': {
    'defaultType': 'swap', 
      }
})
params =  {
   "test":True
}
order = exchange_swap.createLimitBuyOrder('XRP-USDT-SWAP',100,0.7)

Error message-

ccxt.base.errors.BadRequest: okex5 {"code":"1","data":[{"clOrdId":"","ordId":"","sCode":"51000","sMsg":"Parameter posSide  error ","tag":""}],"msg":""}

After searching about this error I found this in their documentation-

Error message           Http status code     Error code
Parameter {0} error.    400                  51000

Documentation link - Okex v5

P.S - I am able to place this via trading dashboard on okex but not through the API. I have enough balance to create this order.

Entire error message is this-

Traceback (most recent call last):
File "C:\Users\ishaa\Desktop\trading\Crypto Vibhor\Testing Files\ccxt_test.py", line 70, in 
 <module>
  order = exchange_swap.createLimitBuyOrder('XRP-USDT-SWAP',100,0.7)
  File "C:\Users\ishaa\Desktop\trading\tradingenv\lib\site-packages\ccxt\base\exchange.py", line 1983, in create_limit_buy_order
return self.create_order(symbol, 'limit', 'buy', amount, price, params)
 File "C:\Users\ishaa\Desktop\trading\tradingenv\lib\site-packages\ccxt\okex5.py", line 1287, in create_order
response = self.privatePostTradeOrder(self.extend(request, params))
File "C:\Users\ishaa\Desktop\trading\tradingenv\lib\site-packages\ccxt\base\exchange.py", line 461, in inner
return entry(_self, **inner_kwargs)
File "C:\Users\ishaa\Desktop\trading\tradingenv\lib\site-packages\ccxt\base\exchange.py", line 
 486, in request
  return self.fetch2(path, api, method, params, headers, body)
  File "C:\Users\ishaa\Desktop\trading\tradingenv\lib\site-packages\ccxt\base\exchange.py", line 482, in fetch2
  return self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "C:\Users\ishaa\Desktop\trading\tradingenv\lib\site-packages\ccxt\base\exchange.py", line 634, in fetch
  self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body)
  File "C:\Users\ishaa\Desktop\trading\tradingenv\lib\site-packages\ccxt\okex5.py", line 2230, in handle_errors
  self.throw_exactly_matched_exception(self.exceptions['exact'], errorCode, feedback)
  File "C:\Users\ishaa\Desktop\trading\tradingenv\lib\site-packages\ccxt\base\exchange.py", line 500, in throw_exactly_matched_exception
   raise exact[string](message)
   ccxt.base.errors.BadRequest: okex5 {"code":"1","data": 
   [{"clOrdId":"","ordId":"","sCode":"51000","sMsg":"Parameter posSide  error 
    ","tag":""}],"msg":""}
Energetic answered 7/6, 2021 at 9:42 Comment(0)
F
4

This could happen if you have the wrong Order Placement Mode in your OKX account settings. Go to your OKX account > trade > perpetual swap > setting > order placement mode and set it to Long/Short mode.

Floydflss answered 18/9, 2022 at 9:24 Comment(0)
U
2

The error message says Parameter posSide error which means you have an error in the posSide paramater you are sending in your request.

As far as I can see from your code you have only provided the ticker, size and price but are missing the posSide parameter.

Contrary to other exchanges, on Okex you can buy or sell a long or short for FUTURES and SWAP products so you need to add the posSide parameter.

It's counter intuitive and took me some time to figure out as well. Effectively you can both buy a long and buy a short at the same time. You can try it out in the Okex gui to see exactly how it works.

The docs tell us that posSide parameter is:

enter image description here

Another option is to disable the long/short mode (which is set by default) and use net mode instead. Using net mode means you won't need to add the posSide parameter to your request string.

Here is the link to the docs which explain how to set the position mode.

And here is a working example which includes two requeired parameters for a limit order, namely setting tdMode and posSide:

import ccxt

exchange = ccxt.okex5({
    'apiKey': '...',
    'secret': '...',
    'password': '...',
})

exchange.createLimitBuyOrder('XRP-USDT-SWAP', 100, 0.4,
                             {"tdMode": "cross", "posSide": "long"})

You can read more about setting Custom Order Params in the ccxt doc here.

Urrutia answered 15/5, 2022 at 8:4 Comment(0)
C
0

For anyone facing similar issue in 2024, it's called Position Mode and it's value should be One-way mode in Perp Settings as pointed out in @x-6-2-5's answer.

The same error can be seen when using the ATAS Platform for OKX Crypto.

Cheeseburger answered 25/2 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.