From 0aa8c91577aab693ad94ce0152fd6df04ee9f754 Mon Sep 17 00:00:00 2001 From: Frederic Fortier Date: Wed, 24 Jan 2018 22:43:08 -0500 Subject: [PATCH] BLD: for issue #174, re-implemented the fetch_tickers approach --- catalyst/exchange/ccxt/ccxt_exchange.py | 36 ++++++++++++------------- tests/exchange/test_ccxt.py | 3 ++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/catalyst/exchange/ccxt/ccxt_exchange.py b/catalyst/exchange/ccxt/ccxt_exchange.py index 3227361f..543466aa 100644 --- a/catalyst/exchange/ccxt/ccxt_exchange.py +++ b/catalyst/exchange/ccxt/ccxt_exchange.py @@ -997,31 +997,31 @@ class CCXT(Exchange): list[dict[str, float] """ - tickers = {} + symbols = self.get_symbols(assets) + try: + results = self.api.fetch_tickers(symbols=symbols) + except (ExchangeError, NetworkError) as e: + log.warn( + 'unable to fetch tickers {} / {}: {}'.format( + self.name, symbols, e + ) + ) + raise ExchangeRequestError(error=e) + + tickers = dict() for asset in assets: symbol = self.get_symbol(asset) - - # Test the CCXT throttling further to see if we need this - self.ask_request() - - # TODO: use fetch_tickers() for efficiency - # I tried using fetch_tickers() but noticed some - # inconsistencies, see issue: - # https://github.com/ccxt/ccxt/issues/870 - try: - ticker = self.api.fetch_ticker(symbol=symbol) - except (ExchangeError, NetworkError) as e: - log.warn( - 'unable to fetch ticker {} / {}: {}'.format( - self.name, asset.symbol, e - ) + if symbol not in results: + msg = 'ticker not found {} / {}'.format( + self.name, symbol ) + log.warn(msg) if on_ticker_error == 'warn': continue - else: - raise ExchangeRequestError(error=e) + raise ExchangeRequestError(error=msg) + ticker = results[symbol] ticker['last_traded'] = from_ms_timestamp(ticker['timestamp']) if 'last_price' not in ticker: diff --git a/tests/exchange/test_ccxt.py b/tests/exchange/test_ccxt.py index 4e33ae3e..3683ca4a 100644 --- a/tests/exchange/test_ccxt.py +++ b/tests/exchange/test_ccxt.py @@ -72,7 +72,8 @@ class TestCCXT(BaseExchangeTestCase): def test_tickers(self): log.info('retrieving tickers') assets = [ - self.exchange.get_asset('iot_usd'), + self.exchange.get_asset('ada_eth'), + self.exchange.get_asset('zrx_eth'), ] tickers = self.exchange.tickers(assets) assert len(tickers) == 1