feat: 完善 QMT 交易模块文档和配置展示功能
- 优化前端仪表盘界面 - 添加配置文件可视化展示 - 编写 QMT 模块配置文档 - 完善项目规则体系(KiloCode)
This commit is contained in:
@@ -122,53 +122,38 @@ class TrendEfficiency(StockWiseFactor):
|
||||
|
||||
return efficiency_ratio
|
||||
|
||||
# -------------------- 统一计算函数 --------------------
|
||||
def calculate_momentum_factors(df: pl.DataFrame) -> pl.DataFrame:
|
||||
"""
|
||||
统一计算动量因子的函数
|
||||
|
||||
Parameters:
|
||||
df (pl.DataFrame): 输入的股票数据表,必须包含以下列:
|
||||
ts_code, trade_date, close, pct_chg, high, low, vol
|
||||
|
||||
Returns:
|
||||
pl.DataFrame: 包含所有动量因子的DataFrame
|
||||
"""
|
||||
# 初始化结果DataFrame
|
||||
result_df = df.clone()
|
||||
|
||||
# 定义要计算的因子列表
|
||||
# 先计算股票截面因子(时间序列因子)
|
||||
stock_operators = [
|
||||
ReturnFactor(5),
|
||||
ReturnFactor(20),
|
||||
VolatilityFactor(10),
|
||||
VolatilityFactor(30),
|
||||
MomentumFactor(10),
|
||||
MomentumFactor(30),
|
||||
RSI_Factor(14)
|
||||
]
|
||||
|
||||
# 依次应用股票截面因子算子
|
||||
for operator in stock_operators:
|
||||
try:
|
||||
result_df = operator.apply(result_df)
|
||||
except Exception as e:
|
||||
print(f"计算股票截面因子 {operator.factor_id} 时出错: {e}")
|
||||
|
||||
# 再计算日期截面因子(横截面排序因子)
|
||||
date_operators = [
|
||||
CrossSectionalRanking("return_5d"),
|
||||
CrossSectionalRanking("return_20d"),
|
||||
CrossSectionalRanking("volatility_10d"),
|
||||
CrossSectionalRanking("momentum_10d")
|
||||
]
|
||||
|
||||
# 依次应用日期截面因子算子
|
||||
for operator in date_operators:
|
||||
try:
|
||||
result_df = operator.apply(result_df)
|
||||
except Exception as e:
|
||||
print(f"计算日期截面因子 {operator.factor_id} 时出错: {e}")
|
||||
|
||||
return result_df
|
||||
class SimpleVolatilityFactor(StockWiseFactor):
|
||||
factor_id = "simple_volatility"
|
||||
required_factor_ids = ["high", "low", "vol"]
|
||||
|
||||
def __init__(self):
|
||||
super(SimpleVolatilityFactor, self).__init__(
|
||||
name=self.factor_id,
|
||||
parameters={},
|
||||
required_factor_ids=self.required_factor_ids
|
||||
)
|
||||
|
||||
def calc_factor(self, g: pl.DataFrame) -> pl.Series:
|
||||
high = g["high"]
|
||||
low = g["low"]
|
||||
vol = g["vol"]
|
||||
|
||||
# Step 1: 计算 EM_i,t
|
||||
# 注意:shift(1) 得到 t-1 的值
|
||||
em = ((high + low) - (high.shift(1) + low.shift(1))) / 2.0
|
||||
em = em.fill_null(0.0) # 第一天无前值,设为0
|
||||
|
||||
# Step 2: 计算 BR_i,t
|
||||
# 避免除零:若 High == Low,设 BR = 0
|
||||
range_ = high - low
|
||||
br = vol / range_
|
||||
br = br.fill_null(0.0).replace({float('inf'): 0.0, float('-inf'): 0.0})
|
||||
|
||||
# Step 3: 计算 MM_i,t = EM / BR
|
||||
mm = em / br
|
||||
mm = mm.fill_null(0.0).replace({float('inf'): 0.0, float('-inf'): 0.0})
|
||||
|
||||
# Step 4: 计算 239 日简单移动平均
|
||||
emv = mm.rolling_mean(window_size=239, min_periods=1)
|
||||
|
||||
return emv.alias(self.factor_id)
|
||||
|
||||
Reference in New Issue
Block a user