Fix tests (and logic) for get_rates

This commit is contained in:
Matthias 2022-05-27 07:20:21 +02:00
parent 2b5530217d
commit 746d046f1a
3 changed files with 16 additions and 11 deletions

View File

@ -1562,16 +1562,14 @@ class Exchange:
entry_pricing = self._config.get('entry_pricing', {})
exit_pricing = self._config.get('exit_pricing', {})
order_book = ticker = None
if entry_pricing.get('use_order_book', False):
if not entry_rate and entry_pricing.get('use_order_book', False):
order_book_top = max(entry_pricing.get('order_book_top', 1),
exit_pricing.get('order_book_top', 1))
order_book = self.fetch_l2_order_book(pair, order_book_top)
if not entry_rate:
entry_rate = self.get_rate(pair, refresh, 'entry', is_short, order_book=order_book)
else:
entry_rate = self.get_rate(pair, refresh, 'entry', is_short, order_book=order_book)
elif not entry_rate:
ticker = self.fetch_ticker(pair)
if not entry_rate:
entry_rate = self.get_rate(pair, refresh, 'entry', is_short, ticker=ticker)
entry_rate = self.get_rate(pair, refresh, 'entry', is_short, ticker=ticker)
if not exit_rate:
exit_rate = self.get_rate(pair, refresh, 'exit',
is_short, order_book=order_book, ticker=ticker)

View File

@ -2525,15 +2525,20 @@ def test_get_rates_testing_buy(mocker, default_conf, caplog, side, ask, bid,
assert exchange.get_rates('ETH/BTC', refresh=True, is_short=False)[0] == expected
assert not log_has("Using cached buy rate for ETH/BTC.", caplog)
api_mock.fetch_l2_order_book.reset_mock()
api_mock.fetch_ticker.reset_mock()
assert exchange.get_rates('ETH/BTC', refresh=False, is_short=False)[0] == expected
assert log_has("Using cached buy rate for ETH/BTC.", caplog)
assert api_mock.fetch_l2_order_book.call_count == 0
assert api_mock.fetch_ticker.call_count == 0
# Running a 2nd time with Refresh on!
caplog.clear()
assert exchange.get_rates('ETH/BTC', refresh=True, is_short=False)[0] == expected
assert not log_has("Using cached buy rate for ETH/BTC.", caplog)
api_mock.fetch_l2_order_book.call_count = int(use_order_book)
api_mock.fetch_ticker.call_count = 1
assert api_mock.fetch_l2_order_book.call_count == int(use_order_book)
assert api_mock.fetch_ticker.call_count == 1
@pytest.mark.parametrize('side,ask,bid,last,last_ab,expected', get_sell_rate_data)
@ -2564,12 +2569,15 @@ def test_get_rates_testing_sell(default_conf, mocker, caplog, side, bid, ask,
assert isinstance(rate, float)
assert rate == expected
# Use caching
api_mock.fetch_l2_order_book.reset_mock()
api_mock.fetch_ticker.reset_mock()
rate = exchange.get_rates(pair, refresh=False, is_short=False)[1]
assert rate == expected
assert log_has("Using cached sell rate for ETH/BTC.", caplog)
api_mock.fetch_l2_order_book.call_count = int(use_order_book)
api_mock.fetch_ticker.call_count = 1
assert api_mock.fetch_l2_order_book.call_count == 0
assert api_mock.fetch_ticker.call_count == 0
@pytest.mark.parametrize("exchange_name", EXCHANGES)

View File

@ -737,7 +737,6 @@ def test_backtest__get_sell_trade_entry(default_conf, fee, mocker) -> None:
res = backtesting._get_exit_trade_entry(trade, row_sell)
assert res is not None
assert res.exit_reason == ExitType.ROI.value
# Sell at minute 3 (not available above!)
assert res.close_date_utc == datetime(2020, 1, 1, 5, 3, tzinfo=timezone.utc)
sell_order = res.select_order('sell', True)