Merge pull request #331 from gcarq/fix/work_without_network
Fix _coinmarketcap that fails backtesting and Hyperopt when no network
This commit is contained in:
commit
454cd16df4
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user