Don't crash dry-run if orderbook side is empty

closes #6018
This commit is contained in:
Matthias 2021-12-02 19:17:00 +01:00
parent 294c98ed5e
commit d3ad4fb52e
2 changed files with 20 additions and 10 deletions

View File

@ -685,16 +685,20 @@ class Exchange:
if not self.exchange_has('fetchL2OrderBook'): if not self.exchange_has('fetchL2OrderBook'):
return True return True
ob = self.fetch_l2_order_book(pair, 1) ob = self.fetch_l2_order_book(pair, 1)
if side == 'buy': try:
price = ob['asks'][0][0] if side == 'buy':
logger.debug(f"{pair} checking dry buy-order: price={price}, limit={limit}") price = ob['asks'][0][0]
if limit >= price: logger.debug(f"{pair} checking dry buy-order: price={price}, limit={limit}")
return True if limit >= price:
else: return True
price = ob['bids'][0][0] else:
logger.debug(f"{pair} checking dry sell-order: price={price}, limit={limit}") price = ob['bids'][0][0]
if limit <= price: logger.debug(f"{pair} checking dry sell-order: price={price}, limit={limit}")
return True if limit <= price:
return True
except IndexError:
# Ignore empty orderbooks when filling - can be filled with the next iteration.
pass
return False return False
def check_dry_limit_order_filled(self, order: Dict[str, Any]) -> Dict[str, Any]: def check_dry_limit_order_filled(self, order: Dict[str, Any]) -> Dict[str, Any]:

View File

@ -1026,6 +1026,12 @@ def test_create_dry_run_order_limit_fill(default_conf, mocker, side, startprice,
assert order_closed['status'] == 'closed' assert order_closed['status'] == 'closed'
assert order['fee'] assert order['fee']
# Empty orderbook test
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book',
return_value={'asks': [], 'bids': []})
exchange._dry_run_open_orders[order['id']]['status'] = 'open'
order_closed = exchange.fetch_dry_run_order(order['id'])
@pytest.mark.parametrize("side,rate,amount,endprice", [ @pytest.mark.parametrize("side,rate,amount,endprice", [
# spread is 25.263-25.266 # spread is 25.263-25.266