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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
mellertsoncommented, Sep 20, 2021

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.

1reaction
tjappocommented, Jul 13, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exchanges — ccxt 2.4.71 documentation
Each class implements the public and private API for a particular crypto exchange. All exchanges are derived from the base Exchange class and...
Read more >
ccxt-dev/ccxt - Gitter
Anyone here have much experience with Binance's "Signature is not valid" error? I'm currently watching it but it seems like it might be...
Read more >
How To Handle CCXT Binance Intermittent Network Error
1 Answer 1. Sorted by: Reset to default. Highest score (default) ...
Read more >
ccxt - Bountysource
ccxt. A JavaScript / Python / PHP library for cryptocurrency trading and ... new ccxt['binance']; while (true) { try { // How to...
Read more >
Python Scripts for CCXT Crypto Candlestick (OHLCV ...
CCXT Example. Jumping right into the first example, we will take the LTC/BTC trading pair and collect the OHLCV candles from Binance. Once...
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