From 59a33d0fa910fd2806f61d9da8fd748d92f15dde Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 25 Jun 2021 20:51:45 +0200 Subject: [PATCH] Add test for ask_orderbook validation --- freqtrade/configuration/config_validation.py | 2 +- freqtrade/exchange/exchange.py | 4 ++-- tests/exchange/test_exchange.py | 3 ++- tests/test_configuration.py | 17 +++++++++++++++++ tests/test_freqtradebot.py | 3 ++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/freqtrade/configuration/config_validation.py b/freqtrade/configuration/config_validation.py index 72c299bed..29c078d0d 100644 --- a/freqtrade/configuration/config_validation.py +++ b/freqtrade/configuration/config_validation.py @@ -193,7 +193,7 @@ def _validate_ask_orderbook(conf: Dict[str, Any]) -> None: ask_strategy = conf.get('ask_strategy', {}) ob_min = ask_strategy.get('order_book_min') ob_max = ask_strategy.get('order_book_max') - if ob_min is not None and ob_max is not None: + if ob_min is not None and ob_max is not None and ask_strategy.get('use_order_book'): if ob_min != ob_max: raise OperationalException( "Using order_book_max != order_book_min in ask_strategy is no longer supported." diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 53a451174..790292a3d 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1071,8 +1071,8 @@ class Exchange: rate = order_book[f"{ask_strategy['price_side']}s"][order_book_top - 1][0] except (IndexError, KeyError) as e: logger.warning( - "Sell Price at location from orderbook could not be determined." - f"Orderbook: {order_book}" + f"Sell Price at location {order_book_top} from orderbook could not be " + f"determined. Orderbook: {order_book}" ) raise PricingError from e else: diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 45db3a5e4..524dc873c 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -1869,7 +1869,8 @@ def test_get_sell_rate_orderbook_exception(default_conf, mocker, caplog): exchange = get_patched_exchange(mocker, default_conf) with pytest.raises(PricingError): exchange.get_sell_rate(pair, True) - assert log_has_re(r"Sell Price at location 1 from orderbook could not be determined\..*", caplog) + assert log_has_re(r"Sell Price at location 1 from orderbook could not be determined\..*", + caplog) def test_get_sell_rate_exception(default_conf, mocker, caplog): diff --git a/tests/test_configuration.py b/tests/test_configuration.py index c5d0cd908..366501d8a 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -935,6 +935,23 @@ def test_validate_protections(default_conf, protconf, expected): validate_config_consistency(conf) +def test_validate_ask_orderbook(default_conf, caplog) -> None: + conf = deepcopy(default_conf) + conf['ask_strategy']['use_order_book'] = True + conf['ask_strategy']['order_book_min'] = 2 + conf['ask_strategy']['order_book_max'] = 2 + + validate_config_consistency(conf) + assert log_has_re(r"DEPRECATED: Please use `order_book_top` instead of.*", caplog) + assert conf['ask_strategy']['order_book_top'] == 2 + + conf['ask_strategy']['order_book_max'] = 5 + + with pytest.raises(OperationalException, + match=r"Using order_book_max != order_book_min in ask_strategy.*"): + validate_config_consistency(conf) + + def test_load_config_test_comments() -> None: """ Load config with comments diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 06f42d5be..4f8a18827 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -4026,7 +4026,8 @@ def test_order_book_ask_strategy(default_conf, limit_buy_order_open, limit_buy_o return_value={'bids': [[]], 'asks': [[]]}) with pytest.raises(PricingError): freqtrade.handle_trade(trade) - assert log_has_re(r'Sell Price at location 1 from orderbook could not be determined\..*', caplog) + assert log_has_re(r'Sell Price at location 1 from orderbook could not be determined\..*', + caplog) def test_startup_state(default_conf, mocker):