From bf4b2dc05eaa5fd48231170671a7692f13c29638 Mon Sep 17 00:00:00 2001 From: Gerald Lonlas Date: Sat, 6 Jan 2018 20:07:40 -0800 Subject: [PATCH] Fix _coinmarketcap that fails backtesting and Hyperopt when no network --- freqtrade/fiat_convert.py | 22 ++++++++++++++-------- freqtrade/tests/rpc/test_rpc_telegram.py | 2 ++ freqtrade/tests/test_fiat_convert.py | 13 +++++++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/freqtrade/fiat_convert.py b/freqtrade/fiat_convert.py index 567835eed..3f0b6330b 100644 --- a/freqtrade/fiat_convert.py +++ b/freqtrade/fiat_convert.py @@ -57,7 +57,11 @@ class CryptoToFiatConverter(): ] def __init__(self) -> None: - self._coinmarketcap = Pymarketcap() + try: + self._coinmarketcap = Pymarketcap() + except BaseException: + self._coinmarketcap = None + self._pairs = [] def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float: @@ -147,10 +151,12 @@ class CryptoToFiatConverter(): # Check if the fiat convertion you want is supported if not self._is_supported_fiat(fiat=fiat_symbol): raise ValueError('The fiat {} is not supported.'.format(fiat_symbol)) - - return float( - self._coinmarketcap.ticker( - currency=crypto_symbol, - convert=fiat_symbol - )['price_' + fiat_symbol.lower()] - ) + try: + return float( + self._coinmarketcap.ticker( + currency=crypto_symbol, + convert=fiat_symbol + )['price_' + fiat_symbol.lower()] + ) + except BaseException: + return 0.0 diff --git a/freqtrade/tests/rpc/test_rpc_telegram.py b/freqtrade/tests/rpc/test_rpc_telegram.py index eca65bd5a..428c544c1 100644 --- a/freqtrade/tests/rpc/test_rpc_telegram.py +++ b/freqtrade/tests/rpc/test_rpc_telegram.py @@ -167,6 +167,7 @@ def test_profit_handle( mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap', ticker=MagicMock(return_value={'price_usd': 15000.0}), _cache_symbols=MagicMock(return_value={'BTC': 1})) + mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0) init(default_conf, create_engine('sqlite://')) _profit(bot=MagicMock(), update=update) @@ -422,6 +423,7 @@ def test_daily_handle( mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap', ticker=MagicMock(return_value={'price_usd': 15000.0}), _cache_symbols=MagicMock(return_value={'BTC': 1})) + mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0) init(default_conf, create_engine('sqlite://')) # Create some test data diff --git a/freqtrade/tests/test_fiat_convert.py b/freqtrade/tests/test_fiat_convert.py index 13597f3a3..7ad316042 100644 --- a/freqtrade/tests/test_fiat_convert.py +++ b/freqtrade/tests/test_fiat_convert.py @@ -72,8 +72,11 @@ def test_fiat_convert_find_price(mocker): with pytest.raises(ValueError, match=r'The fiat ABC is not supported.'): fiat_convert._find_price(crypto_symbol='BTC', fiat_symbol='ABC') + mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=12345.0) assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='USD') == 12345.0 assert fiat_convert.get_price(crypto_symbol='btc', fiat_symbol='usd') == 12345.0 + + mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=13000.2) assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='EUR') == 13000.2 @@ -83,6 +86,7 @@ def test_fiat_convert_get_price(mocker): 'price_eur': 15000.0 }) mocker.patch('freqtrade.fiat_convert.Pymarketcap.ticker', api_mock) + mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=28000.0) fiat_convert = CryptoToFiatConverter() @@ -109,3 +113,12 @@ def test_fiat_convert_get_price(mocker): fiat_convert._pairs[0]._expiration = expiration assert fiat_convert.get_price(crypto_symbol='BTC', fiat_symbol='USD') == 28000.0 assert fiat_convert._pairs[0]._expiration is not expiration + + +def test_fiat_convert_without_network(mocker): + Pymarketcap = MagicMock(side_effect=ImportError('Oh boy, you have no network!')) + mocker.patch('freqtrade.fiat_convert.Pymarketcap', Pymarketcap) + + fiat_convert = CryptoToFiatConverter() + assert fiat_convert._coinmarketcap is None + assert fiat_convert._find_price(crypto_symbol='BTC', fiat_symbol='USD') == 0.0