[ccxt] fix unsupported fiat failures (#620)

* prepare to support FIAT/Crypto trading

* Don't fail fiat-convert for unsupported stake currencies

* remove commented code

* Add BNB to cryptomap

* Fix test-failure

* related to random execution as fee was not properly mocked if this is
one of the first tests
This commit is contained in:
Matthias 2018-04-21 23:20:12 +02:00 committed by Michael Egger
parent 23e989d31f
commit acb1b50924
3 changed files with 10 additions and 6 deletions

View File

@ -76,7 +76,8 @@ class CryptoToFiatConverter(object):
CRYPTOMAP = {
'BTC': 'bitcoin',
'ETH': 'ethereum',
'USDT': 'thether'
'USDT': 'thether',
'BNB': 'binance-coin'
}
def __new__(cls):
@ -99,6 +100,8 @@ class CryptoToFiatConverter(object):
:param fiat_symbol: fiat to convert to
:return: float, value in fiat of the crypto-currency amount
"""
if crypto_symbol == fiat_symbol:
return crypto_amount
price = self.get_price(crypto_symbol=crypto_symbol, fiat_symbol=fiat_symbol)
return float(crypto_amount) * float(price)
@ -180,8 +183,9 @@ class CryptoToFiatConverter(object):
raise ValueError('The fiat {} is not supported.'.format(fiat_symbol))
if crypto_symbol not in self.CRYPTOMAP:
raise ValueError(
'The crypto symbol {} is not supported.'.format(crypto_symbol))
# return 0 for unsupported stake currencies (fiat-convert should not break the bot)
logger.warning("unsupported crypto-symbol %s - returning 0.0", crypto_symbol)
return 0.0
try:
return float(
self._coinmarketcap.ticker(

View File

@ -287,11 +287,12 @@ def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> Non
)
def test_start(mocker, init_backtesting, default_conf, caplog) -> None:
def test_start(mocker, init_backtesting, fee, default_conf, caplog) -> None:
"""
Test start() function
"""
start_mock = MagicMock()
mocker.patch('freqtrade.exchange.get_fee', fee)
mocker.patch('freqtrade.optimize.backtesting.Backtesting.start', start_mock)
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)

View File

@ -77,8 +77,7 @@ 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')
with pytest.raises(ValueError, match=r'The crypto symbol XRP is not supported.'):
fiat_convert.get_price(crypto_symbol='XRP', fiat_symbol='USD')
assert fiat_convert.get_price(crypto_symbol='XRP', fiat_symbol='USD') == 0.0
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