From 4660909e958eb8da2b79e989435c4e7426f5fbba Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Sun, 14 Jun 2020 01:07:00 +0300 Subject: [PATCH] Validate stoploss_on_exchange_limit_ratio at startup time --- freqtrade/exchange/exchange.py | 7 +++++++ tests/exchange/test_exchange.py | 26 +++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index bd44f56f2..820526b49 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -386,6 +386,13 @@ class Exchange: f'On exchange stoploss is not supported for {self.name}.' ) + # Limit price threshold: As limit price should always be below stop-price + # Used for limit stoplosses on exchange + limit_price_pct = order_types.get('stoploss_on_exchange_limit_ratio', 0.99) + if limit_price_pct >= 1.0 or limit_price_pct <= 0.0: + raise OperationalException( + "stoploss_on_exchange_limit_ratio should be < 1.0 and > 0.0") + def validate_order_time_in_force(self, order_time_in_force: Dict) -> None: """ Checks if order time in force configured in strategy/config are supported diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 48c4956cf..1aaf95379 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -689,13 +689,13 @@ def test_validate_order_types(default_conf, mocker): mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') mocker.patch('freqtrade.exchange.Exchange.name', 'Bittrex') + default_conf['order_types'] = { 'buy': 'limit', 'sell': 'limit', 'stoploss': 'market', 'stoploss_on_exchange': False } - Exchange(default_conf) type(api_mock).has = PropertyMock(return_value={'createMarketOrder': False}) @@ -707,7 +707,6 @@ def test_validate_order_types(default_conf, mocker): 'stoploss': 'market', 'stoploss_on_exchange': False } - with pytest.raises(OperationalException, match=r'Exchange .* does not support market orders.'): Exchange(default_conf) @@ -718,11 +717,32 @@ def test_validate_order_types(default_conf, mocker): 'stoploss': 'limit', 'stoploss_on_exchange': True } - with pytest.raises(OperationalException, match=r'On exchange stoploss is not supported for .*'): Exchange(default_conf) + default_conf['order_types'] = { + 'buy': 'limit', + 'sell': 'limit', + 'stoploss': 'limit', + 'stoploss_on_exchange': False, + 'stoploss_on_exchange_limit_ratio': 1.05 + } + with pytest.raises(OperationalException, + match=r'stoploss_on_exchange_limit_ratio should be < 1.0 and > 0.0'): + Exchange(default_conf) + + default_conf['order_types'] = { + 'buy': 'limit', + 'sell': 'limit', + 'stoploss': 'limit', + 'stoploss_on_exchange': False, + 'stoploss_on_exchange_limit_ratio': -0.1 + } + with pytest.raises(OperationalException, + match=r'stoploss_on_exchange_limit_ratio should be < 1.0 and > 0.0'): + Exchange(default_conf) + def test_validate_order_types_not_in_config(default_conf, mocker): api_mock = MagicMock()