v2 BacktestEngine: support add_strategy(instance) alongside add_strategy_from_config #3807
wayneadams
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Context
In the v1 (Cython) BacktestEngine, there are two ways to register strategies:
engine.add_strategy(my_strategy), where you pass a constructed Python object directlyImportableStrategyConfigwithBacktestNode, where you pass a dotted import path and a config dictThe v2 (Rust/PyO3) BacktestEngine currently exposes only
add_strategy_from_configandadd_actor_from_config. The instance-basedadd_strategy(instance)andadd_actor(instance)are not available.I'd like to ask whether adding instance-based registration to the v2 BacktestEngine is something you'd consider, and whether it fits the v2 design direction.
Use case: optimization and walk-forward loops
My project generates randomized strategy configurations (entry signal trees, exit parameters, reversal bar settings), runs single-day backtests in a loop, and archives trade-level results. A simplified version of the loop looks like:
The strategy configs contain nested Python objects (condition trees, indicator instances shared between signals) that are awkward to serialize through a
dictto JSON to reimport round-trip on every iteration. In this kind of single-workstation optimization pattern, the strategies are constructed in the same Python process that's running the engine, so there's no need to serialize across a process or network boundary.What I think is involved technically
Looking at the v2 implementation in
crates/backtest/src/python/engine.rs,add_strategy_from_configdoes three things:PyStrategywrapper, including setting the strategy ID, logging flags, and storing the Python instance referenceSteps 2 and 3 don't depend on how the Python object was created. They just need a Python object that inherits from
PyStrategy. Anadd_strategymethod could skip step 1 and go straight to step 2 with a caller-provided object.The same applies to
add_actor_from_configandPyDataActor.Questions
Thanks for the incredible work on the Rust port. The v2 build and type stubs are already a huge improvement!
Beta Was this translation helpful? Give feedback.
All reactions