ccxt: Watching ohlcv on Binance results in ccxt.base.errors.NetworkError: 1000

I am getting this error (ccxt.base.errors.NetworkError: 1000) for the Binance exchange after a couple of hours. This also occurs for BNB/USDT. However, strangely I don’t get this error for btc/usdt.

  • OS: Linux (Ubuntu)
  • Programming Language version: Python 3.7
  • CCXT version: latest

Function giving the error (ccxt.base.errors.NetworkError: 1000):

exchange = getattr(ccxtpro, 'binance')({'enableRateLimit': True})
if not exchange.has['fetchOHLCV']:
        raise KeyError('Exchange does not provide the ability to fetch OHLCV candles.')
if not exchange.has['watchOHLCV']:
        raise KeyError('Exchange does not provide the ability to watch OHLCV candles.')
ohlcvs = await exchange.watch_ohlcv(symbol="ETH/USDT", timeframe="1m", limit=2)
if len(ohlcvs) < 2:
      return False
# Copies the ohlcv candles, since ccxt cannot handle in-reference edits
candles = [ohlcvs[0][:], ohlcvs[1][:]]
candles[0][0] = from_unix(candles[0][0])  # Official formed candle, with previous timestamp
candles[1][0] = from_unix(candles[1][0])  # Unofficial candle, representing current timestamp
if not self.last_candles or candles[1][0] > self.last_candles[1][0]:
      self.last_candles = candles
      return True
return False

Other function using the data

last_candle = DataFrame([self.last_candles[0]], columns=TOHLCV.get_values()).set_index(TOHLCV.TIMESTAMP.value)
self.historical_minute = self.historical_minute.append(last_candle).tail(min_candles)
File "/src/mia/blotter/blotter/blotter.py", line 140, in __get_last_candle
     ohlcvs = await self.exchange.watch_ohlcv(symbol=self.strategy.ccxt_pair, timeframe=timeframe, limit=2)
   File "/venv/lib/python3.8/site-packages/ccxtpro/binance.py", line 394, in watch_ohlcv
     return await self.after(future, self.filter_by_since_limit, since, limit, 0, True)
   File "/venv/lib/python3.8/site-packages/ccxtpro/base/exchange.py", line 81, in after
     return method(await future, *args)
   File "/venv/lib/python3.8/site-packages/ccxtpro/binance.py", line 481, in watch_public
     return await self.watch(url, messageHash, self.extend(request, query), messageHash, subscribe)
 ccxt.base.errors.NetworkError: 1000

About this issue

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

Commits related to this issue

Most upvoted comments

Just to provide additional context, I too have experienced this issue. And I can confirm it was due to an underlying network connection issue unrelated to CCXT and the crypto-currency exchange. After adding an execption handler to restart things it is now a non-issue. I have provided my code below, which handles the exception by restarting.

class ExchangeRateCacher(object):

	...
    
	def main(self):
		""" This main method is called in loop by self.run() """
		self.log.info(f'started')
		while self.is_running:
			try:
				loop = asyncio.new_event_loop()
				loop.run_until_complete(self.order_book_wss(loop))
			except Exception as e:
				self.log.error(
					f'{self.name} crashed, attempting restart...',
					exc_info=True)
				cn_sleep(5.0)
			try:
				loop.close()
			except Exception:
				pass
		self.log.info(f'stopped')

	async def order_book_wss(self, loop):
		""" Update the order book in real-time using CCXT Pro """
		exchange_class = getattr(ccxtpro, self.exchange.id, None)
		if exchange_class is None:
			raise RuntimeError(f'{self.exchange} not found in ccxtpro!')
		exchange = exchange_class({'enableRateLimit': True, 'asyncio_loop': loop})
		while self.is_running:
			try:
				self.order_book = await exchange.watch_order_book(self.market.symbol)
				if self.bid_price is not None:
					self.rate_queue.put((self.bid_price, self.bid_amount))
			except NetworkError as e:
				self.debug(f'{type(e)}, error number: {e}, attempting to reconnect...')
			except Exception as e:
				self.log.error(f'crashed, attempting to reconnect...')
				self.log.error(f'---> {e}', exc_info=True)

The service, which my code is a part of, has been running without issue for over 14 months. I hope this helps someone. Cheers.

I ran the code you gave me in a Kubernetes container and it had one restart. I also attached the logs of that container. ccxt version: 1.30.94 ccxt pro version: 0.2.89 ccxt_previous.log

I get the same errors for ETH/USDT as well as BNB/USDT. I will do some logging later and will post the output.