diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index b26013a11..b6b7c113f 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -1400,7 +1400,10 @@ class Backtesting: ) while current_time <= end_date: is_first = True - for pair in pairs: + # Pairs that have open trades should be processed first + new_pairlist = list(dict.fromkeys([t.pair for t in LocalTrade.bt_trades_open] + pairs)) + + for pair in new_pairlist: yield current_time, pair, is_first is_first = False diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index c2c6ce954..9567a2cac 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -1514,7 +1514,26 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir) all_orients = [x for _, x in calls_per_candle.items()] - assert all(x == ["ADA/BTC", "DASH/BTC", "ETH/BTC", "LTC/BTC", "NXT/BTC"] for x in all_orients) + distinct_calls = [list(x) for x in set(tuple(x) for x in all_orients)] + + # All calls must be made for the full pairlist + assert all(len(x) == 5 for x in distinct_calls) + + # order varied - and is not always identical + assert not all( + x == ["ADA/BTC", "DASH/BTC", "ETH/BTC", "LTC/BTC", "NXT/BTC"] for x in distinct_calls + ) + # But some calls should've kept the original ordering + assert any( + x == ["ADA/BTC", "DASH/BTC", "ETH/BTC", "LTC/BTC", "NXT/BTC"] for x in distinct_calls + ) + assert ( + # Ordering can be different, but should be one of the following + any(x == ["ETH/BTC", "ADA/BTC", "DASH/BTC", "LTC/BTC", "NXT/BTC"] for x in distinct_calls) + or any( + x == ["ETH/BTC", "LTC/BTC", "ADA/BTC", "DASH/BTC", "NXT/BTC"] for x in distinct_calls + ) + ) # Make sure we have parallel trades assert len(evaluate_result_multi(results["results"], "5m", 2)) > 0