refactor(ml): add MLFilter.from_model(), fix validator initial_balance

- MLFilter.from_model() classmethod eliminates brittle __new__() private-attribute
  manipulation in backtester walk-forward model injection
- backtest_validator._check_invariants() now accepts cfg and uses cfg.initial_balance
  instead of a hardcoded 1000.0 for the negative-balance invariant check
- backtester.py walk-forward injection block simplified to use the new factory method

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
21in7
2026-03-21 18:36:30 +09:00
parent a34fc6f996
commit 5bad7dd691
4 changed files with 34 additions and 13 deletions

View File

@@ -317,16 +317,9 @@ class Backtester:
self.ml_filters = {}
for sym in self.cfg.symbols:
if sym in ml_models and ml_models[sym] is not None:
mf = MLFilter.__new__(MLFilter)
mf._disabled = False
mf._onnx_session = None
mf._lgbm_model = ml_models[sym]
mf._threshold = self.cfg.ml_threshold
mf._onnx_path = Path("/dev/null")
mf._lgbm_path = Path("/dev/null")
mf._loaded_onnx_mtime = 0.0
mf._loaded_lgbm_mtime = 0.0
self.ml_filters[sym] = mf
self.ml_filters[sym] = MLFilter.from_model(
ml_models[sym], threshold=self.cfg.ml_threshold
)
else:
self.ml_filters[sym] = None