ccxt: decimal.InvalidOperation with okex.create_order()

  • OS: Linux
  • Programming Language version: Python 3.6
  • CCXT version:ccxt==1.27.54

Hello, I’m facing an error with OKEx when trying to create a new order. Here is my code and the error message, I’m very curious to know what could be the reason since I did many tries and I can’t find a solution.

Thank you,

import ccxt
ok = ccxt.okex()
ok.secret = ''
ok.apiKey = ''
ok.password = ''
args = {'amount': 18, 'side': 'sell', 'symbol': 'ETH-USDT-SWAP', 'type': 'market'}
ok.options['defaultType'] = 'swap'
ok.create_order(**args)
---------------------------------------------------------------------------
InvalidOperation                          Traceback (most recent call last)
<ipython-input-42-58965134a2db> in <module>
      1 args = {'amount': 18, 'side': 'sell', 'symbol': 'ETH-USDT-SWAP', 'type': 'market'}
      2 ok.options['defaultType'] = 'swap'
----> 3 ok.create_order(**args)

~/python/envdev/lib/python3.6/site-packages/ccxt/okex.py in create_order(self, symbol, type, side, amount, price, params)
   1766                 'type': type,  # 1:open long 2:open short 3:close long 4:close short for futures
   1767                 'size': size,
-> 1768                 'price': self.price_to_precision(symbol, price),
   1769                 # 'match_price': '0',  # Order at best counter party price?(0:no 1:yes). The default is 0. If it is set as 1, the price parameter will be ignored. When posting orders at best bid price, order_type can only be 0(regular order).
   1770             })

~/python/envdev/lib/python3.6/site-packages/ccxt/base/exchange.py in price_to_precision(self, symbol, price)
   1219 
   1220     def price_to_precision(self, symbol, price):
-> 1221         return self.decimal_to_precision(price, ROUND, self.markets[symbol]['precision']['price'], self.precisionMode)
   1222 
   1223     def amount_to_precision(self, symbol, amount):

~/python/envdev/lib/python3.6/site-packages/ccxt/base/decimal_to_precision.py in decimal_to_precision(n, rounding_mode, precision, counting_mode, padding_mode)
     53     context.rounding = decimal.ROUND_HALF_UP  # rounds 0.5 away from zero
     54 
---> 55     dec = decimal.Decimal(str(n))
     56     precision_dec = decimal.Decimal(str(precision))
     57     string = '{:f}'.format(dec)  # convert to string using .format to avoid engineering notation

InvalidOperation: [<class 'decimal.ConversionSyntax'>]

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

@Kinzowa yes, this aspect is still subject to change, we’re working on a unification schema for it.

@Kinzowa

This should work:

ok.options['defaultType'] = 'swap'
ok.load_markets()
ok.verbose = True
amount = 18
symbol = 'ETH-USDT-SWAP'
side = 'buy'
type = '1'  #  1:open long 2:open short 3:close long 4:close short
params = { 'order_type': '4' }  # order_type = 4 means a market order with okex swap / futures
price = None
ok.create_order(symbol, type, side, amount, price, params)

Let me know if you still have difficulties with it.

This code sample you just provided still generates a ConversionSyntax and no verbose output is displayed. Please find bellow the code sample that generates Invalid order type error.

ok.options['defaultType'] = 'swap'
ok.load_markets()
ok.verbose = True
amount = 18
symbol = 'ETH-USDT-SWAP'
side = 'buy'
typeo = '1'
ok.create_order(symbol, typeo, side, amount)

---------------------------------------------------------------------------
InvalidOperation                          Traceback (most recent call last)
<ipython-input-9-e31bb2390fae> in <module>
      6 side = 'buy'
      7 typeo = '1'
----> 8 ok.create_order(symbol, typeo, side, amount)

~/python/envdev/lib/python3.6/site-packages/ccxt/okex.py in create_order(self, symbol, type, side, amount, price, params)
   1788                 request['match_price'] = '1'
   1789             else:
-> 1790                 request['price'] = self.price_to_precision(symbol, price)
   1791             if market['futures']:
   1792                 request['leverage'] = '10'  # or '20'

~/python/envdev/lib/python3.6/site-packages/ccxt/base/exchange.py in price_to_precision(self, symbol, price)
   1255 
   1256     def price_to_precision(self, symbol, price):
-> 1257         return self.decimal_to_precision(price, ROUND, self.markets[symbol]['precision']['price'], self.precisionMode)
   1258 
   1259     def amount_to_precision(self, symbol, amount):

~/python/envdev/lib/python3.6/site-packages/ccxt/base/decimal_to_precision.py in decimal_to_precision(n, rounding_mode, precision, counting_mode, padding_mode)
     53     context.rounding = decimal.ROUND_HALF_UP  # rounds 0.5 away from zero
     54 
---> 55     dec = decimal.Decimal(str(n))
     56     precision_dec = decimal.Decimal(str(precision))
     57     string = '{:f}'.format(dec)  # convert to string using .format to avoid engineering notation

InvalidOperation: [<class 'decimal.ConversionSyntax'>]

This is how I generate the Invalid order type error :

args = {'amount': 18, 'symbol': 'ETH-USDT-SWAP'}
ok.options['defaultType'] = 'swap'
ok.create_market_buy_order(**args)

Request: POST https://www.okex.com/api/swap/v3/order {'OK-ACCESS-KEY': 'myAPYKEY', 'OK-ACCESS-PASSPHRASE': 'myPASSWORD', 'OK-ACCESS-TIMESTAMP': '2020-08-06T13:10:55.961Z', 'Content-Type': 'application/json', 'OK-ACCESS-SIGN': 'myACCESSSIGN/Q2u7oszWGHrfw=', 'User-Agent': 'python-requests/2.20.1', 'Accept-Encoding': 'gzip, deflate'} {"instrument_id":"ETH-USDT-SWAP","type":"market","size":"18","match_price":"1"}

Response: POST https://www.okex.com/api/swap/v3/order 400 {'Date': 'Thu, 06 Aug 2020 13:10:56 GMT', 'Content-Type': 'application/json', 'Content-Length': '91', 'Connection': 'keep-alive', 'Set-Cookie': '__cfduid=d511db131908862a6cebf6b744857c6a41596719456; expires=Sat, 05-Sep-20 13:10:56 GMT; path=/; domain=.okex.com; HttpOnly; SameSite=Lax, locale=en_US; Max-Age=604800; Expires=Thu, 13-Aug-2020 13:10:56 GMT; Path=/', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'X-Frame-Options': 'DENY', 'X-BrokerID': '0', 'X-Kong-Upstream-Latency': '23', 'X-Kong-Proxy-Latency': '0', 'Via': 'kong/1.4.3', 'CF-Cache-Status': 'DYNAMIC', 'cf-request-id': '04657e77460000cc5a23ac6200000001', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Server': 'cloudflare', 'CF-RAY': '5be900387a8dcc5a-ZRH'} {"error_message":"Invalid order type","result":"true","error_code":"35040","order_id":"-1"}

Thanks it works like a charm now 😃

@Kinzowa ok, please, update to 1.32.56 (it will arrive in 15 minutes), and let me know if this does not work:

import ccxt
exchange = ccxt.okex({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_API_SECRET',
    'password': 'YOUR_API_PASSWORD',
    'enableRateLimit': True,  # required by the Manual: https://github.com/ccxt/ccxt/wiki/Manual#rate-limit
    'options': {
        'defaultType': 'swap',
    },
})
exchange.load_markets()
args = {'amount': 18, 'side': 'sell', 'symbol': 'ETH-USDT-SWAP', 'type': 'market'}
exchange.verbose = True  # ←------------------------- POST YOUR VERBOSE REQUEST+RESPONSE IF IT DOES NOT WORK
exchange.create_order(**args)