Skip to content

Fix MultiBacktest mixed no-trade results - #1366

Merged
kernc merged 3 commits into
kernc:masterfrom
elpolini:fix/multibacktest-no-trade-results
Jul 22, 2026
Merged

Fix MultiBacktest mixed no-trade results#1366
kernc merged 3 commits into
kernc:masterfrom
elpolini:fix/multibacktest-no-trade-results

Conversation

@elpolini

@elpolini elpolini commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1344.
Closes #1358

MultiBacktest._mp_task_run() currently returns None for datasets whose strategy produced zero trades. When a MultiBacktest run mixes datasets with trades and datasets with no trades, MultiBacktest.run() then receives a mixed list of pd.Series and None, which can either raise TypeError: object of type 'NoneType' has no len() or produce nested/odd DataFrame values depending on ordering.

Backtest.run() already returns a complete stats series for zero-trade strategies, so this change keeps those stats instead of replacing them with None.

Changes

  • Always return filtered stats from _mp_task_run(), including zero-trade stats.
  • Add a regression test for mixed trade/no-trade datasets.
  • Assert zero-trade datasets report # Trades == 0 and do not produce nested Series values.

Test plan

python -m unittest backtesting.test._test.TestLib.test_MultiBacktest_handles_mixed_no_trade_results
python -m unittest backtesting.test._test.TestLib.test_MultiBacktest
python -m unittest backtesting.test._test.TestBacktest.test_run backtesting.test._test.TestUtil.test_as_str
python -m compileall -q backtesting
flake8 backtesting setup.py

Comment thread backtesting/test/_test.py Outdated
Fixes CI error:

> AttributeError: Can't get local object 'TestLib.test_MultiBacktest_handles_mixed_no_trade_results.<locals>.SometimesNoTrade'
@kernc

kernc commented Jul 22, 2026

Copy link
Copy Markdown
Owner

This is now green and lgtm! Thanks!

@kernc
kernc merged commit 0d36e37 into kernc:master Jul 22, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MultiBacktest.run errors out when tested strategy sometimes never enters a position

2 participants