ccxt: watch_order_book error with Binance : Connection closed by remote server, closing code 1008

Hello,

I upgraded both CCXT and CCXT Pro to the latest version and I receive this error when I try to watch Binance order book for one spot symbol :


  File "/home/abc/env/lib/python3.6/site-packages/ccxtpro/binance.py", line 155, in watch_order_book
    orderbook = await self.watch(url, messageHash, message, messageHash, subscription)
ccxt.base.errors.NetworkError: Connection closed by remote server, closing code 1008

Error code1008 is not referenced in the Binance API.

What do you think is the reason for this issue and how can I fix it?

Thank you,

This is how I initiate the loop:


    async def watch_book(account, client, market, i, j):
        while True:
            try:
                ob = await client.watch_order_book(market.symbol)
            except Exception as e:
                break

    async def wallet_loop(account, loop, i, wallet):
        client = getattr(ccxtpro, exchange.exid)({'enableRateLimit': True, 'asyncio_loop': loop, })
        ws_loops = [watch_book(account, client, market, i, j) for j, market in enumerate(markets_monitor)]
        await asyncio.gather(*ws_loops)
        await client.close()

    async def main(account, loop, wallets):
        wallet_loops = [wallet_loop(account, loop, i, wallet) for i, wallet in enumerate(wallets)]
        await asyncio.gather(*wallet_loops)

    loop = asyncio.get_event_loop()
    gp = asyncio.wait([main(account, loop, wallets)])
    loop.run_until_complete(gp)
    loop.close()

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

@YuriyTigiev try adding more pairs to it to see where it breaks. Then add the database connector. Make sure you use an async connector, for example: https://aioinflux.readthedocs.io/en/stable/

Please find below a reproducible example.

When 4 markets are monitored it throws a RuntimeError : This event loop is already running then returns data, but when 5 markets are monitored it throws the RuntimeError with no data. That is the problem

Now, when I replace break with continue my IP address get banned by the remote host (happened yesterday) so I don’t think it’s a workaround.

Thanks for your support, Kinzowa

import ccxtpro
import asyncio

async def watch_book(client, symbol):

    while True:
        try:
            ob = await client.watch_order_book(symbol)
            print(ob['asks'][0][0])
        
            await client.sleep(1000)
            
        except Exception as e:
            print('error {0}'.format(symbol), str(e))
            break
            
async def markets_loop(loop):

    client = getattr(ccxtpro, 'binance')({'enableRateLimit': True, 'asyncio_loop': loop, })
    client.options['defaultType'] = 'spot'

    symbols = ['DOT/BUSD',
              'FTM/BUSD',
              # 'FTM/USDT',    #  <- REMOVE TO MONITOR 5 MARKETS INSTEAD OF 4
              'SOL/USDT',
              'DOT/USDT']

    ws_loops = [watch_book(client, symbol) for symbol in symbols]

    await asyncio.gather(*ws_loops)
    await client.close()
        
loop = asyncio.get_event_loop()
gp = asyncio.wait([markets_loop(loop)])
loop.run_until_complete(gp)
loop.close()

@Kinzowa replace this code

    async def watch_book(account, client, market, i, j):
        while True:
            try:
                ob = await client.watch_order_book(market.symbol)
            except Exception as e:
                break

with

    async def watch_book(account, client, market, i, j):
        while True:
            try:
                ob = await client.watch_order_book(market.symbol)
            except Exception as e:
               print('oh no an error', str(e))
               continue # CHANGE THIS LINE

ccxt will handle the disconnects and reconnect automatically.

Let us know if this does not solve your problem and thanks for using ccxt pro!