Implementation:51bitquant Ai hedge fund crypto import strategy class
| Knowledge Sources | |
|---|---|
| Domains | Extensibility, Plugin_Loading |
| Last Updated | 2026-10-28 14:00 GMT |
Overview
Concrete tool for dynamically importing a strategy class from a dotted module path provided by the `import_strategy_class` function.
Description
import_strategy_class splits a dotted path on the last `.` (`rsplit('.', 1)`), `importlib.import_module`s the module part, and `getattr`s the class name. For example `"src.strategies.MacdStrategy"` → module `src.strategies`, attr `MacdStrategy`. It is used by Workflow.create_workflow to load strategies by name at DAG construction time. A misnamed strategy raises `AttributeError`/`ModuleNotFoundError` immediately (fail-fast).
Usage
Call this when you need to resolve a strategy class from a string path, or use it as a validation check before wiring a custom strategy into the DAG. Also useful in tests to confirm a strategy module is importable.
Code Reference
Source Location
- Repository: ai-hedge-fund-crypto
- File: src/utils/util_func.py
Signature
import importlib
def import_strategy_class(strategies_path: str):
"""
Import a class from a dotted path like 'src.strategies.MacdStrategy'
"""
module_path, class_name = strategies_path.rsplit('.', 1)
module = importlib.import_module(module_path)
return getattr(module, class_name)
Import
from src.utils.util_func import import_strategy_class
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| strategies_path | str | Yes | Dotted path, e.g. `"src.strategies.MacdStrategy"` |
Outputs
| Name | Type | Description |
|---|---|---|
| return | type | The resolved class (e.g. `MacdStrategy`) |
Usage Examples
Resolve and Instantiate
from src.utils.util_func import import_strategy_class
# Resolve the class
StrategyClass = import_strategy_class("src.strategies.MacdStrategy")
strategy = StrategyClass()
# Validate a custom strategy is importable
try:
Cls = import_strategy_class("src.strategies.MyStrategy")
print("OK:", Cls.__name__)
except (ModuleNotFoundError, AttributeError) as e:
print("Failed:", e)
Inside Workflow.create_workflow
for strategy_node_name in strategies:
strategy_class = import_strategy_class(f"src.strategies.{strategy_node_name}")
strategy_instance = strategy_class()
workflow.add_node(strategy_node_name, strategy_instance)