feat: 完善 QMT 交易模块文档和配置展示功能
- 优化前端仪表盘界面 - 添加配置文件可视化展示 - 编写 QMT 模块配置文档 - 完善项目规则体系(KiloCode)
This commit is contained in:
@@ -78,88 +78,6 @@ from .momentum_factors import (
|
||||
VolatilityFactor,
|
||||
MomentumFactor,
|
||||
MomentumAcceleration,
|
||||
TrendEfficiency
|
||||
TrendEfficiency,
|
||||
SimpleVolatilityFactor
|
||||
)
|
||||
|
||||
|
||||
# 导入统一因子计算模块
|
||||
from .all_factors import calculate_all_factors, compute_factors
|
||||
|
||||
# 导入算子框架
|
||||
from .operator_framework import StockWiseFactor, DateWiseFactor, FactorGraph
|
||||
|
||||
# 定义所有因子类的列表,便于统一管理
|
||||
ALL_STOCK_FACTORS = [
|
||||
SMAFactor,
|
||||
EMAFactor,
|
||||
ATRFactor,
|
||||
OBVFactor,
|
||||
MACDFactor,
|
||||
RSI_Factor,
|
||||
LGFlowFactor,
|
||||
FlowIntensityFactor,
|
||||
FlowDivergenceFactor,
|
||||
FlowStructureFactor,
|
||||
FlowAccelerationFactor,
|
||||
ChipConcentrationFactor,
|
||||
ChipSkewnessFactor,
|
||||
FloatingChipFactor,
|
||||
CostSupportFactor,
|
||||
WinnerPriceZoneFactor,
|
||||
SentimentPanicGreedFactor,
|
||||
SentimentBreadthFactor,
|
||||
SentimentReversalFactor,
|
||||
PriceDeductionFactor,
|
||||
PriceDeductionRatioFactor,
|
||||
CashflowToEVFactor,
|
||||
BookToPriceFactor,
|
||||
DebtToEquityFactor,
|
||||
ProfitMarginFactor,
|
||||
LimitFactor,
|
||||
VolumeRatioFactor,
|
||||
BBI_RATIO_FACTOR,
|
||||
VolatilitySlopeFactor,
|
||||
PriceVolumeTrendFactor
|
||||
]
|
||||
|
||||
ALL_DATE_FACTORS = [
|
||||
CrossSectionalRankFactor,
|
||||
IndustryMomentumFactor,
|
||||
MarketBreadthFactor,
|
||||
SectorRotationFactor
|
||||
]
|
||||
|
||||
__all__ = [
|
||||
# 技术指标因子
|
||||
'SMAFactor', 'EMAFactor', 'ATRFactor', 'OBVFactor', 'MACDFactor', 'RSI_Factor',
|
||||
|
||||
# 资金流因子
|
||||
'LGFlowFactor', 'FlowIntensityFactor', 'FlowDivergenceFactor',
|
||||
'FlowStructureFactor', 'FlowAccelerationFactor',
|
||||
|
||||
# 筹码分布因子
|
||||
'ChipConcentrationFactor', 'ChipSkewnessFactor', 'FloatingChipFactor',
|
||||
'CostSupportFactor', 'WinnerPriceZoneFactor',
|
||||
|
||||
# 市场情绪因子
|
||||
'SentimentPanicGreedFactor', 'SentimentBreadthFactor', 'SentimentReversalFactor',
|
||||
'PriceDeductionFactor', 'PriceDeductionRatioFactor',
|
||||
|
||||
# 行业/横截面因子
|
||||
'CrossSectionalRankFactor', 'IndustryMomentumFactor', 'MarketBreadthFactor',
|
||||
'SectorRotationFactor',
|
||||
|
||||
# 财务因子
|
||||
'CashflowToEVFactor', 'BookToPriceFactor', 'ROEFactor', 'DebtToEquityFactor',
|
||||
'ProfitMarginFactor',
|
||||
|
||||
# 特殊因子
|
||||
'LimitFactor', 'VolumeRatioFactor', 'BBI_RATIO_FACTOR',
|
||||
'VolatilitySlopeFactor', 'PriceVolumeTrendFactor',
|
||||
|
||||
# 统一因子计算
|
||||
'calculate_all_factors', 'compute_factors', 'get_available_stock_factors', 'get_available_date_factors',
|
||||
|
||||
# # 算子框架
|
||||
# 'StockWiseFactor', 'DateWiseFactor'
|
||||
]
|
||||
@@ -65,6 +65,7 @@ from main.factor import (
|
||||
BBI_RATIO_FACTOR,
|
||||
VolatilitySlopeFactor,
|
||||
PriceVolumeTrendFactor,
|
||||
SimpleVolatilityFactor
|
||||
)
|
||||
|
||||
|
||||
@@ -168,6 +169,10 @@ def calculate_all_factors(
|
||||
"class": CrossSectionalRankFactor,
|
||||
"params": {"column": "circ_mv", "name": "size_rank"},
|
||||
},
|
||||
{
|
||||
"class": SimpleVolatilityFactor,
|
||||
"params": {"high", "low", "vol"}
|
||||
}
|
||||
]
|
||||
|
||||
if date_factor_configs is None:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -12,7 +12,6 @@ import pandas as pd
|
||||
|
||||
def prepare_data(
|
||||
polars_df: pl.DataFrame,
|
||||
label_horizon: int = 5,
|
||||
open_col: str = "open",
|
||||
date_col: str = "trade_date",
|
||||
code_col: str = "ts_code",
|
||||
@@ -24,13 +23,13 @@ def prepare_data(
|
||||
|
||||
df = polars_df.sort([code_col, date_col])
|
||||
|
||||
# 获取 T+1 日的开盘价(作为买入价)
|
||||
df = df.with_columns([
|
||||
pl.col(open_col).shift(-1).over(code_col).alias("__buy_price"),
|
||||
pl.col(open_col).shift(-(1 + label_horizon)).over(code_col).alias("__sell_price"),
|
||||
]).with_columns([
|
||||
(pl.col("__sell_price") / pl.col("__buy_price") - 1).alias("label")
|
||||
]).drop(["__buy_price", "__sell_price"])
|
||||
# # 获取 T+1 日的开盘价(作为买入价)
|
||||
# df = df.with_columns([
|
||||
# pl.col(open_col).shift(-1).over(code_col).alias("__buy_price"),
|
||||
# pl.col(open_col).shift(-(1 + label_horizon)).over(code_col).alias("__sell_price"),
|
||||
# ]).with_columns([
|
||||
# (pl.col("__sell_price") / pl.col("__buy_price") - 1).alias("label")
|
||||
# ]).drop(["__buy_price", "__sell_price"])
|
||||
|
||||
# 转 pandas
|
||||
df = df.to_pandas()
|
||||
|
||||
Reference in New Issue
Block a user