BLD: conditionally fetching single or multi tickers for performance reasons

This commit is contained in:
Frederic Fortier
2018-01-25 17:40:45 -05:00
parent 0aa8c91577
commit 7ae23e2340
3 changed files with 31 additions and 11 deletions
+29 -9
View File
@@ -997,16 +997,36 @@ class CCXT(Exchange):
list[dict[str, float]
"""
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
if len(assets) == 1:
symbol = self.get_symbol(assets[0])
try:
log.debug('fetching single ticker: {}'.format(symbol))
results = dict()
results[symbol] = self.api.fetch_ticker(symbol=symbol)
except (ExchangeError, NetworkError) as e:
log.warn(
'unable to fetch ticker {} / {}: {}'.format(
self.name, symbol, e
)
)
)
raise ExchangeRequestError(error=e)
raise ExchangeRequestError(error=e)
elif len(assets) > 1:
symbols = self.get_symbols(assets)
try:
log.debug('fetching multiple tickers: {}'.format(symbols))
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)
else:
raise ValueError('Cannot request tickers with not assets.')
tickers = dict()
for asset in assets:
+1 -1
View File
@@ -701,7 +701,7 @@ class Exchange:
)
positions_value = 0.0
if positions is not None:
if positions:
assets = set([position.asset for position in positions])
tickers = self.tickers(assets)
+1 -1
View File
@@ -76,7 +76,7 @@ class TestCCXT(BaseExchangeTestCase):
self.exchange.get_asset('zrx_eth'),
]
tickers = self.exchange.tickers(assets)
assert len(tickers) == 1
assert len(tickers) == 2
pass
def test_my_trades(self):