Heuristic:51bitquant Ai hedge fund crypto Fail Fast Strategy Loading
| Knowledge Sources | |
|---|---|
| Domains | Debugging, Configuration |
| Last Updated | 2026-10-28 09:34 GMT |
Overview
Dynamic strategy class loading via dotted-path import fails immediately at DAG compile time, catching config typos before the first bar is processed.
Description
The import_strategy_class function uses importlib.import_module and getattr to resolve a dotted path string (e.g., src.strategies.macd_strategy.MacdStrategy) into a class object. Because this happens during Workflow.create_workflow — before any market data is fetched or trades are simulated — a typo in the config's strategies list raises ModuleNotFoundError or AttributeError immediately, rather than failing silently partway through a long backtest.
Usage
This heuristic is always active — there is no flag to toggle. It benefits you when adding a new strategy to config.yaml: if the dotted path is wrong, you find out in seconds rather than after the backtest starts. Keep strategy paths in config as fully-qualified dotted module paths.
The Insight (Rule of Thumb)
- Action: List strategies in
config.yamlas dotted paths (e.g.,src.strategies.macd_strategy.MacdStrategy). - Value: N/A — this is a design pattern, not a numeric setting.
- Trade-off: No downside. The cost is a hard crash on misconfiguration, which is desirable.
- Compatibility: Works with any strategy class that inherits from
BaseNode.
Reasoning
Catching configuration errors early is a fundamental reliability principle. A backtest that runs for minutes or hours before failing on a bad import wastes time and compute. By resolving all strategy classes at DAG construction time (in Workflow.create_workflow), the system converts a potential late failure into an immediate, actionable error message. This is the "fail-fast" pattern applied to plugin architecture.
Code Evidence
Dynamic import from src/utils/util_func.py:12-18:
def import_strategy_class(strategies_path: str):
"""
Import a class from a dotted path like 'myproject.module.MyClass'
"""
module_path, class_name = strategies_path.rsplit('.', 1)
module = importlib.import_module(module_path)
return getattr(module, class_name)
Strategy resolution during workflow construction from src/agent/workflow.py — strategies are imported and added to the LangGraph DAG at build time, before any data is fetched.
Config strategy list from config.example.yaml:12:
strategies: ['MacdStrategy']