Optimize method fails w/o any error info
Expected behavior
I try to optimize a set of args, it fails. But it doesn’t show what the error is. A simple backtest runs, so I know that’s fine. I’ve tried printing to console with try/except in init & super. But nothing. Dunno how to tell what exactly the issue is if I don’t get errors
Code sample
Actual behavior
It just shows a failed bar. Instead of either actually working or showing the error stack
Additional info, steps to reproduce, full crash traceback, screenshots
No response
Software versions
backtesting.__version__: 0.6.4pandas.__version__: 2.3.0numpy.__version__: 2.2.0bokeh.__version__: 3.7.3- OS: Sequoia 15.6.1
Would it be possible to add a debug option to optimize() that exposes the original exception traceback?
When debugging my own scripts, I usually remove the try/except block or add some logging inside it to find where the issue starts. In this case, it seems the exception is hidden by the optimization process, making it difficult to identify the cause.
Maybe something like debug=True or raise_errors=True could help during development?
Can you try with a more recent version of backtesting or even master. I suspect whatever https://github.com/kernc/backtesting.py/commit/6ffa512d058ff3ad68f36d6aeb8dddb036577d70 fixed.
I looked into this on current master (0.6.7.dev1, commit ca2e261) to check whether the silent failure still reproduces. It doesn't — whatever swallowed the exception in 0.6.4 appears to be gone:
Backtest.optimize()'s worker (Backtest._mp_task) has no try/except aroundbt.run(**params), so an exception raised anywhere in a strategy run now propagates out ofpool.imapwith its original type and traceback. This matches @kernc's suspicion above: commit 6ffa512 (reverting the multiprocessing start-method change) is in the history since 0.6.4.
Verified with a minimal example — a strategy whose init() raises:
from backtesting import Backtest, Strategy
from backtesting.test import GOOG
class BoomInit(Strategy):
n = 5
def init(self):
raise RuntimeError('boom from strategy init')
def next(self):
pass
Backtest(GOOG, BoomInit).optimize(n=[5, 10])
On master this raises RuntimeError('boom from strategy init') with the full traceback. The same holds for exceptions raised in the strategy constructor and in next() mid-run, and when only one parameter combination of several fails — the original exception surfaces in every case I tried.
So there doesn't seem to be anything left to fix here on master; if the failure still occurs for someone on the latest release, a traceback would now be visible to attach to a report. I haven't changed any code — flagging the verification result here rather than opening a PR that fixes nothing.