feat: 量化引擎加固 — 新增测试 + 数据/因子/回测层优化
- 新增 finance/tests/ 6 个测试套件(agents/backtest/dao_upsert/factors/features/fundamental_lookahead) - 数据层: data_manager / dao 优化,新增 upsert 逻辑 - 因子层: 基本面因子抽象定位 _mapping、ROE/PE/PB 重构 - 回测层: vectorbt/engine 大改动(251 行),report 增强 - ML 层: features/backtest_integration 特征工程与回测优化 - CLI: agent_cli 重构 - config/settings 扩充配置项
This commit is contained in:
@@ -8,6 +8,9 @@ import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from agents.base import BaseAgent
|
||||
from config.settings import (
|
||||
RISK_THRESHOLDS, RISK_EXPOSURE, RISK_MAX_SINGLE, RISK_STOP_LOSS,
|
||||
)
|
||||
|
||||
|
||||
class RiskAgent(BaseAgent):
|
||||
@@ -16,11 +19,8 @@ class RiskAgent(BaseAgent):
|
||||
name = "Risk"
|
||||
description = "仓位控制与风险预警"
|
||||
|
||||
# 风险等级阈值
|
||||
THRESHOLDS = {
|
||||
"high": {"vol": 35, "dd": -15},
|
||||
"medium": {"vol": 25, "dd": -8},
|
||||
}
|
||||
# 风险等级阈值(来自配置中心)
|
||||
THRESHOLDS = RISK_THRESHOLDS
|
||||
|
||||
def execute(
|
||||
self,
|
||||
@@ -55,24 +55,22 @@ class RiskAgent(BaseAgent):
|
||||
peak = close.expanding().max()
|
||||
current_dd = float((close.iloc[-1] / peak.iloc[-1] - 1) * 100)
|
||||
|
||||
# 风险等级
|
||||
# 风险等级 & 仓位(参数来自配置中心)
|
||||
if market_vol > self.THRESHOLDS["high"]["vol"] or current_dd < self.THRESHOLDS["high"]["dd"]:
|
||||
risk_level = "high"
|
||||
target_exposure = 0.30
|
||||
elif market_vol > self.THRESHOLDS["medium"]["vol"] or current_dd < self.THRESHOLDS["medium"]["dd"]:
|
||||
risk_level = "medium"
|
||||
target_exposure = 0.60
|
||||
else:
|
||||
risk_level = "low"
|
||||
target_exposure = 0.85
|
||||
target_exposure = RISK_EXPOSURE[risk_level]
|
||||
|
||||
# 最近 N 日涨跌
|
||||
ret_5d = float(close.pct_change(5).iloc[-1] * 100) if len(close) >= 6 else 0
|
||||
ret_20d = float(close.pct_change(20).iloc[-1] * 100) if len(close) >= 21 else 0
|
||||
|
||||
# 单票上限(风险越高越集中)
|
||||
max_single = 0.15 if risk_level == "low" else (0.10 if risk_level == "medium" else 0.05)
|
||||
stop_loss = -0.05 if risk_level == "low" else (-0.08 if risk_level == "medium" else -0.12)
|
||||
# 单票上限(风险越高越集中)与止损线
|
||||
max_single = RISK_MAX_SINGLE[risk_level]
|
||||
stop_loss = RISK_STOP_LOSS[risk_level]
|
||||
|
||||
# 持仓预警
|
||||
alerts = []
|
||||
|
||||
Reference in New Issue
Block a user