feat: ensure uniqueness among fetched orders

closes #11786
This commit is contained in:
Matthias
2025-05-23 07:38:09 +02:00
parent ecdd84efd8
commit f3d03dd26a
3 changed files with 35 additions and 11 deletions

View File

@@ -1793,6 +1793,8 @@ class Exchange:
orders += self._fetch_orders(pair, since)
# Since with 1 minute overlap
since = since + timedelta(minutes=limit - 1)
# Ensure each order is unique based on order id
orders = list({order["id"]: order for order in orders}.values())
return orders
else:

View File

@@ -96,10 +96,27 @@ def test_bybit_fetch_orders(default_conf, mocker, limit_order):
limit_order["sell"],
]
)
api_mock.fetch_open_orders = MagicMock(return_value=[limit_order["buy"]])
api_mock.fetch_closed_orders = MagicMock(return_value=[limit_order["buy"]])
api_mock.fetch_open_orders = MagicMock(
side_effect=[
[{**limit_order["buy"], "id": 1}],
[{**limit_order["buy"], "id": 2}],
[{**limit_order["buy"], "id": 3}],
]
)
api_mock.fetch_closed_orders = MagicMock(
side_effect=[
[{**limit_order["buy"], "id": 5}],
[{**limit_order["buy"], "id": 6}],
[{**limit_order["buy"], "id": 7}],
]
)
mocker.patch(f"{EXMS}.exchange_has", return_value=True)
def exchange_has(value):
if value == "fetchOrders":
return False
return True
mocker.patch(f"{EXMS}.exchange_has", side_effect=exchange_has)
start_time = datetime.now(timezone.utc) - timedelta(days=20)
exchange = get_patched_exchange(mocker, default_conf, api_mock, exchange="bybit")
@@ -111,9 +128,9 @@ def test_bybit_fetch_orders(default_conf, mocker, limit_order):
exchange = get_patched_exchange(mocker, default_conf, api_mock, exchange="bybit")
res = exchange.fetch_orders("mocked", start_time)
# Bybit will call the endpoint 3 times, as it has a limit of 7 days per call
assert api_mock.fetch_orders.call_count == 3
assert api_mock.fetch_open_orders.call_count == 0
assert api_mock.fetch_closed_orders.call_count == 0
assert api_mock.fetch_orders.call_count == 0
assert api_mock.fetch_open_orders.call_count == 3
assert api_mock.fetch_closed_orders.call_count == 3
assert len(res) == 2 * 3

View File

@@ -1726,12 +1726,17 @@ def test_fetch_positions(default_conf, mocker, exchange_name):
@pytest.mark.parametrize("exchange_name", EXCHANGES)
def test_fetch_orders(default_conf, mocker, exchange_name, limit_order):
api_mock = MagicMock()
api_mock.fetch_orders = MagicMock(
return_value=[
limit_order["buy"],
limit_order["sell"],
call_count = 1
def return_value(*args, **kwargs):
nonlocal call_count
call_count += 2
return [
{**limit_order["buy"], "id": call_count},
{**limit_order["sell"], "id": call_count + 1},
]
)
api_mock.fetch_orders = MagicMock(side_effect=return_value)
api_mock.fetch_open_orders = MagicMock(return_value=[limit_order["buy"]])
api_mock.fetch_closed_orders = MagicMock(return_value=[limit_order["buy"]])