question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

okex set-leverage not working

See original GitHub issue
  • OS: linux
  • Programming Language version: python
  • CCXT version: 3.73.3

Trying to set leverage raises an exception

based on the v5 documentation - i think the parameters should be correct.

Is this a okx bug, or am i providing the wrong parameters somehow?

import ccxt
exchange = ccxt.okx({**keys, **{'options': {'defaultType': 'swap'}}})
mark = ct.loadMarkets()
ct.verbose = True
ct.set_leverage(leverage=5,
                symbol=pair,
                params={
                    "mgnMode": 'isolated',
                    "posSide": "short" , # also tested with long
                })


fetch Request: okx POST [https://www.okx.com/api/v5/account/set-leverage]() RequestHeaders: {'OK-ACCESS-KEY': 'something', 'OK-ACCESS-PASSPHRASE': 'something', 'OK-ACCESS-TIMESTAMP': '2022-02-15T16:08:22.027Z', 'Content-Type': 'application[/json]()', 'OK-ACCESS-SIGN': 'something', 'User-Agent': 'python-requests[/2.27.1]()', 'Accept-Encoding': 'gzip, deflate'} RequestBody: {"lever":5,"mgnMode":"isolated","instId":"DOT-USDT-SWAP","posSide":"short"}

fetch Response: okx POST [https://www.okx.com/api/v5/account/set-leverage]() 400 ResponseHeaders: {'Date': 'Tue, 15 Feb 2022 16:08:22 GMT', 'Content-Type': 'application[/json]()', 'Content-Length': '59', 'Connection': 'keep-alive', 'Set-Cookie': 'locale=en-US; Max-Age=604800; Expires=Tue, 22-Feb-2022 16:08:22 GMT; Path=, locale=en-US; Max-Age=604800; Expires=Tue, 22-Feb-2022 16:08:22 GMT; Path=', 'X-BrokerID': '0', 'CF-Cache-Status': 'DYNAMIC', 'Expect-CT': 'max-age=604800, report-uri="[https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct]()"', 'Server': 'cloudflare', 'CF-RAY': '6ddfcb61bc146d8c-MUC'} ResponseBody: {"code":"51000","data":[],"msg":"Parameter posSide  error"}
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/ccxt/base/exchange.py in fetch(self, url, method, headers, body)
    657             self.logger.debug("%s %s, Response: %s %s %s", method, url, http_status_code, headers, http_response)
--> 658             response.raise_for_status()
    659 

~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/requests/models.py in raise_for_status(self)
    959         if http_error_msg:
--> 960             raise HTTPError(http_error_msg, response=self)
    961 

HTTPError: 400 Client Error:  for url: https://www.okx.com/api/v5/account/set-leverage

During handling of the above exception, another exception occurred:

BadRequest                                Traceback (most recent call last)
/tmp/ipykernel_1520147/1210820506.py in <module>
      1 ct.verbose = True
----> 2 ct.set_leverage(leverage=5,
      3                 symbol=pair,
      4                 params={
      5                     "mgnMode": 'isolated',

~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/ccxt/okx.py in set_leverage(self, leverage, symbol, params)
   3635             'instId': market['id'],
   3636         }
-> 3637         response = self.privatePostAccountSetLeverage(self.extend(request, params))
   3638         #
   3639         #     {

~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/ccxt/base/exchange.py in inner(_self, params, context)
    498                 if context is not None:
    499                     inner_kwargs['context'] = params
--> 500                 return entry(_self, **inner_kwargs)
    501             return inner
    502         to_bind = partialer()

~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/ccxt/base/exchange.py in request(self, path, api, method, params, headers, body, config, context)
    549     def request(self, path, api='public', method='GET', params={}, headers=None, body=None, config={}, context={}):
    550         """Exchange.request is the entry point for all generated methods"""
--> 551         return self.fetch2(path, api, method, params, headers, body, config, context)
    552 
    553     @staticmethod

~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/ccxt/base/exchange.py in fetch2(self, path, api, method, params, headers, body, config, context)
    545         self.lastRestRequestTimestamp = self.milliseconds()
    546         request = self.sign(path, api, method, params, headers, body)
--> 547         return self.fetch(request['url'], request['method'], request['headers'], request['body'])
    548 
    549     def request(self, path, api='public', method='GET', params={}, headers=None, body=None, config={}, context={}):

~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/ccxt/base/exchange.py in fetch(self, url, method, headers, body)
    672         except HTTPError as e:
    673             details = ' '.join([self.id, method, url])
--> 674             skip_further_error_handling = self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body)
    675             if not skip_further_error_handling:
    676                 self.handle_http_status_code(http_status_code, http_status_text, url, method, http_response)

~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/ccxt/okx.py in handle_errors(self, httpCode, reason, url, method, headers, body, response, requestHeaders, requestBody)
   3900                 self.throw_exactly_matched_exception(self.exceptions['exact'], errorCode, feedback)
   3901                 self.throw_broadly_matched_exception(self.exceptions['broad'], message, feedback)
-> 3902             self.throw_exactly_matched_exception(self.exceptions['exact'], code, feedback)
   3903             raise ExchangeError(feedback)  # unknown message

~/.pyenv/versions/3.9.7/envs/trade_397/lib/python3.9/site-packages/ccxt/base/exchange.py in throw_exactly_matched_exception(self, exact, string, message)
    563     def throw_exactly_matched_exception(self, exact, string, message):
    564         if string in exact:
--> 565             raise exact[string](message)
    566 
    567     def throw_broadly_matched_exception(self, broad, string, message):

BadRequest: okx {"code":"51000","data":[],"msg":"Parameter posSide  error"}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kroitorcommented, Feb 15, 2022

We have raised it to the OKX tech team, will post the answer here as soon as we hear back from them.

1reaction
xmatthiascommented, Feb 15, 2022

you’re right - but it’s absolutely unclear from the OKEX documentation, which says

Required in long/short mode and when margin mode is isolated, only long or short can be passed.

which i read as “i’m setting isolated -> i must pass long/short”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overview – OKX API guide | OKX technical support
Welcome to the OKX API guide, OKEx technical support. OKX is a leading BTC exchange with highly secure SSL encryption and cold storage...
Read more >
Unified Margin/Position/Leverage Trading via CCXT #4834
Can I margin trade on OKEX/Gate.io/Bibox through ccxt please? The operations involved are: find pair - e.g. EOS/USDT transfer coins into the ...
Read more >
Exchanges — ccxt 2.4.71 documentation
The set of markets differs from exchange to exchange opening possibilities for cross-exchange ... The recommended way of working with exchanges is not...
Read more >
How to Long Using Leverage on OKX Exchange - YouTube
I am not liable for any loss caused, whether due to negligence or otherwise arising ... There are no guarantees or certainties in...
Read more >
How to Trade Bitcoin Futures & Options? Derivatives Exchange
1. Next, click open now to activate your trading account. 2. you want to trade. 3. And select hedge mode and set leverage...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found