Phase 4: Core Service (port 8010) - Pairing management (symbol-strategy pairs) - Order service with Redis pub/sub - Position and balance tracking Phase 5: Strategy Engine (port 8030) - BaseStrategy abstract class - Strategy registry and executor - Built-in strategies: Grid, MA Cross, Dual MA - Signal manager with Redis integration Phase 6: QMT Adapter (port 8040) - XtQuant SDK wrapper with simulation mode - Order executor listening to Redis channels - Quote pusher and account syncer Phase 7: Testing & Documentation - Pytest fixtures for all services - Unit tests for models and strategies - Windows startup scripts
118 lines
3.5 KiB
Python
118 lines
3.5 KiB
Python
"""
|
|
策略注册中心
|
|
|
|
管理所有已注册的策略
|
|
"""
|
|
|
|
from typing import Any, Optional, Type
|
|
|
|
from src.core.logger import get_logger
|
|
from .base_strategy import BaseStrategy
|
|
|
|
logger = get_logger("strategy_engine.registry")
|
|
|
|
|
|
class StrategyRegistry:
|
|
"""策略注册中心"""
|
|
|
|
_instance: Optional["StrategyRegistry"] = None
|
|
|
|
def __new__(cls) -> "StrategyRegistry":
|
|
if cls._instance is None:
|
|
cls._instance = super().__new__(cls)
|
|
cls._instance._strategies = {}
|
|
cls._instance._initialized = False
|
|
return cls._instance
|
|
|
|
def __init__(self) -> None:
|
|
if self._initialized:
|
|
return
|
|
self._strategies: dict[str, Type[BaseStrategy]] = {}
|
|
self._initialized = True
|
|
# 注册内置策略
|
|
self._register_builtin_strategies()
|
|
|
|
def _register_builtin_strategies(self) -> None:
|
|
"""注册内置策略"""
|
|
from .strategies import (
|
|
GridStrategy,
|
|
MACrossStrategy,
|
|
DualMAStrategy,
|
|
)
|
|
|
|
self.register(GridStrategy)
|
|
self.register(MACrossStrategy)
|
|
self.register(DualMAStrategy)
|
|
|
|
logger.info(f"已注册 {len(self._strategies)} 个内置策略")
|
|
|
|
def register(self, strategy_class: Type[BaseStrategy]) -> None:
|
|
"""
|
|
注册策略
|
|
|
|
Args:
|
|
strategy_class: 策略类
|
|
"""
|
|
strategy_id = strategy_class.strategy_id
|
|
if not strategy_id:
|
|
raise ValueError("策略必须设置 strategy_id")
|
|
|
|
if strategy_id in self._strategies:
|
|
logger.warning(f"策略 {strategy_id} 已存在,将被覆盖")
|
|
|
|
self._strategies[strategy_id] = strategy_class
|
|
logger.info(f"注册策略: {strategy_id} - {strategy_class.strategy_name}")
|
|
|
|
def get_strategy_class(self, strategy_id: str) -> Optional[Type[BaseStrategy]]:
|
|
"""获取策略类"""
|
|
return self._strategies.get(strategy_id)
|
|
|
|
def get_strategy_info(self, strategy_id: str) -> Optional[dict[str, Any]]:
|
|
"""获取策略信息"""
|
|
strategy_class = self._strategies.get(strategy_id)
|
|
if not strategy_class:
|
|
return None
|
|
|
|
return {
|
|
"id": strategy_class.strategy_id,
|
|
"name": strategy_class.strategy_name,
|
|
"description": strategy_class.description,
|
|
"version": strategy_class.version,
|
|
"author": strategy_class.author,
|
|
"params_schema": strategy_class.params_schema,
|
|
}
|
|
|
|
def get_params_schema(self, strategy_id: str) -> Optional[dict]:
|
|
"""获取策略参数定义"""
|
|
strategy_class = self._strategies.get(strategy_id)
|
|
if not strategy_class:
|
|
return None
|
|
return strategy_class.params_schema
|
|
|
|
def list_strategies(self, category: Optional[str] = None) -> list[dict[str, Any]]:
|
|
"""
|
|
列出所有策略
|
|
|
|
Args:
|
|
category: 策略分类(暂未实现)
|
|
"""
|
|
strategies = []
|
|
for strategy_id, strategy_class in self._strategies.items():
|
|
strategies.append(
|
|
{
|
|
"id": strategy_class.strategy_id,
|
|
"name": strategy_class.strategy_name,
|
|
"description": strategy_class.description,
|
|
"version": strategy_class.version,
|
|
}
|
|
)
|
|
return strategies
|
|
|
|
def has_strategy(self, strategy_id: str) -> bool:
|
|
"""检查策略是否存在"""
|
|
return strategy_id in self._strategies
|
|
|
|
|
|
# 单例
|
|
strategy_registry = StrategyRegistry()
|