ccxt / ccxt

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading API with support for more than 100 bitcoin/altcoin exchanges
https://docs.ccxt.com
MIT License
32.4k stars 7.45k forks source link

Okex stream order/position/balance #23071

Open duggar opened 1 month ago

duggar commented 1 month ago

Operating System

Linux Debian

Programming Languages

Python

CCXT Version

4.3.59

Description

Order/Balance/Position Streamer giving issues after the trade is closed that is there is no position

[I] [2024-07-12 10:05:44,940] [-] [.stream_position_update] TypeError TypeError('can only concatenate str (not "NoneType") to str') 
[I] [2024-07-12 10:05:44,941] [-] [.stream_position_update] Traceback (most recent call last):\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/eigenquant/crypto/okx/exchange.py", line 98, in stream_position_update\n    pos = await self.exch.watch_positions(symbols=[symbol], params={"extraParams": \'{"updateInterval": 0}\'})\n          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1212, in watch_positions\n    newPositions = await self.subscribe_multiple(\'private\', channel, symbols, self.extend(request, params))\n                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 149, in subscribe_multiple\n    return await self.watch(url, messageHash, request, messageHash)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/fast_client.py", line 26, in handler\n    self.handle_message(message)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 34, in handle_message\n    self.handle_text_or_binary_message(message.data)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 29, in handle_text_or_binary_message\n    self.on_message_callback(self, decoded)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1851, in handle_message\n    method(client, message)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1295, in handle_positions\n    cache.append(position)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/cache.py", line 197, in append\n    self._index.append(item[\'symbol\'] + item[\'side\'])\n                       ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~\nTypeError: can only concatenate str (not "NoneType") to str\n 
[I] [2024-07-12 10:05:44,941] [-] [.stream_order_update] TypeError TypeError('can only concatenate str (not "NoneType") to str') 
[I] [2024-07-12 10:05:44,942] [-] [.stream_order_update] Traceback (most recent call last):\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/eigenquant/crypto/okx/exchange.py", line 54, in stream_order_update\n    orderdata = await self.exch.watch_orders(symbol)\n                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1344, in watch_orders\n    orders = await self.subscribe(\'private\', channel, channel, symbol, self.extend(request, params))\n             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 167, in subscribe\n    return await self.watch(url, messageHash, request, messageHash)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/fast_client.py", line 26, in handler\n    self.handle_message(message)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 34, in handle_message\n    self.handle_text_or_binary_message(message.data)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 29, in handle_text_or_binary_message\n    self.on_message_callback(self, decoded)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1851, in handle_message\n    method(client, message)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1295, in handle_positions\n    cache.append(position)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/cache.py", line 197, in append\n    self._index.append(item[\'symbol\'] + item[\'side\'])\n                       ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~\nTypeError: can only concatenate str (not "NoneType") to str\n 
[I] [2024-07-12 10:05:44,942] [-] [.stream_balance_update] TypeError TypeError('can only concatenate str (not "NoneType") to str') 
[I] [2024-07-12 10:05:44,942] [-] [.stream_balance_update] Traceback (most recent call last):\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/eigenquant/crypto/okx/exchange.py", line 70, in stream_balance_update\n    bal = await self.exch.watch_balance(params={"extraParams": \'{"updateInterval": 0}\'})\n          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1057, in watch_balance\n    return await self.subscribe(\'private\', \'account\', \'account\', None, params)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 167, in subscribe\n    return await self.watch(url, messageHash, request, messageHash)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/fast_client.py", line 26, in handler\n    self.handle_message(message)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 34, in handle_message\n    self.handle_text_or_binary_message(message.data)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 29, in handle_text_or_binary_message\n    self.on_message_callback(self, decoded)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1851, in handle_message\n    method(client, message)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/pro/okx.py", line 1295, in handle_positions\n    cache.append(position)\n  File "/home/gov/miniconda3/envs/ccxtokx/lib/python3.11/site-packages/ccxt/async_support/base/ws/cache.py", line 197, in append\n    self._index.append(item[\'symbol\'] + item[\'side\'])\n                       ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~\nTypeError: can only concatenate str (not "NoneType") to str\n 
samgermain commented 1 month ago

Would you be able to share your code that leads to the error please?

nkasimova commented 2 weeks ago

@samgermain , I got the same error

watch_positions() failed: can only concatenate str (not "NoneType") to str [ERROR]: can only concatenate str (not "NoneType") to str File "/Users/nadezhda/Documents/Finance/CCXTConnector/min_work_example.py", line 48, in watch_balance balance = await exchange.watch_balance(params=params) File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/pro/okx.py", line 1069, in watch_balance return await self.subscribe('private', 'account', 'account', None, params) File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/pro/okx.py", line 168, in subscribe return await self.watch(url, messageHash, request, messageHash) File "/Users/nadezhda/Documents/Finance/CCXTConnector/min_work_example.py", line 58, in watch_positions positions = await exchange.watch_positions() File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/pro/okx.py", line 1222, in watch_positions newPositions = await self.watch(url, channel, nonSymbolRequest, channel) File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/async_support/base/ws/fast_client.py", line 27, in handler self.handle_message(message) File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 34, in handle_message self.handle_text_or_binary_message(message.data) File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 29, in handle_text_or_binary_message self.on_message_callback(self, decoded) File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/pro/okx.py", line 1869, in handle_message method(client, message) File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/pro/okx.py", line 1307, in handle_positions cache.append(position) File "/Users/nadezhda/miniforge3/envs/forced_python/lib/python3.9/site-packages/ccxt/async_support/base/ws/cache.py", line 197, in append self._index.append(item['symbol'] + item['side'])

How to reproduce:

  1. run the code below
  2. open postion (btc/usdt:usdt swap)
  3. close position
  4. see the output for watch_balance, watch_positions
  import asyncio
  import sys

  import ccxt.pro as ccxt

  api_key = <>
  api_secret=<>
  password=<>
  SYMBOL='BTC/USDT:USDT'

  def print_error_info(error, message=''):
      print(f"[ERROR]: {message, type(error).__name__, error.args}")
      print(f"[ERROR]:{sys.exc_info()[1]}")

  exchange = ccxt.okx({
                  'apiKey': api_key,
                  'secret': api_secret,
                  'password': password,
                  'options': {
                      'defaultType': 'swap',
                      'newUpdates': False,
                      'watchOrderBook': {
                          'depth': 'bbo-tbt',  # tick-by-tick best bidask
                      },
                  },
                  'verbose': False,
              })

  async def watch_balance():
      params = {'symbol': SYMBOL,
                  'type': 'swap'}
      while True:
          try:
              balance = await exchange.watch_balance(params=params)
              print(balance['USDT']['total'])
          except Exception as e:
              print_error_info(e)
              print(f'watch_balance() failed: {e}')

  async def watch_positions():
      while True:
          try:
              positions = await exchange.watch_positions()
              print(f"watch_positions: {positions}")

          except Exception as e:
              print_error_info(e)
              print(f'watch_positions() failed: {e}')

  async def run_watchers():

      watchers = [watch_balance(),
                  watch_positions(),
                  ]
      await asyncio.gather(*watchers)
      await exchange.close()

  t = asyncio.ensure_future(run_watchers())
  asyncio.get_event_loop().run_until_complete(t)