From 5b47bf263643bd32549f43dbdfb65914c40e409f Mon Sep 17 00:00:00 2001
From: Variya <variya@VariyadeMacBook-Air.local>
Date: Tue, 03 Mar 2026 13:03:19 +0800
Subject: [PATCH] 同步当前项目文件到gitee
---
code/strategy/alpha_stock_selector.py | 251 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 250 insertions(+), 1 deletions(-)
diff --git a/code/alpha_stock_selector.py b/code/strategy/alpha_stock_selector.py
similarity index 74%
rename from code/alpha_stock_selector.py
rename to code/strategy/alpha_stock_selector.py
index 8e6987c..eb58a92 100644
--- a/code/alpha_stock_selector.py
+++ b/code/strategy/alpha_stock_selector.py
@@ -83,7 +83,7 @@
),
AlphaFactor(
name='Gross_Margin', # 毛利率
- formula='revenue - cost_of_goods_sold / revenue',
+ formula='(revenue - cost_of_goods_sold) / revenue',
ic_target=0.06,
ir_target=1.2,
description='产品竞争力',
@@ -136,6 +136,103 @@
ir_target=0.5,
description='流动性水平',
direction='positive'
+ ),
+ # ==================== 技术面因子 ====================
+ AlphaFactor(
+ name='Momentum_1M', # 1个月动量
+ formula='close / close_20d - 1',
+ ic_target=0.04,
+ ir_target=0.8,
+ description='短期价格动量(1个月)',
+ direction='positive'
+ ),
+ AlphaFactor(
+ name='Momentum_3M', # 3个月动量
+ formula='close / close_60d - 1',
+ ic_target=0.05,
+ ir_target=1.0,
+ description='中期价格动量(3个月)',
+ direction='positive'
+ ),
+ AlphaFactor(
+ name='Momentum_6M', # 6个月动量
+ formula='close / close_120d - 1',
+ ic_target=0.06,
+ ir_target=1.2,
+ description='长期价格动量(6个月)',
+ direction='positive'
+ ),
+ AlphaFactor(
+ name='Reversal_5D', # 5日反转
+ formula='close / close_5d - 1',
+ ic_target=-0.03,
+ ir_target=0.6,
+ description='短期反转效应(5日)',
+ direction='negative'
+ ),
+ AlphaFactor(
+ name='Reversal_20D', # 20日反转
+ formula='close / close_20d - 1',
+ ic_target=-0.04,
+ ir_target=0.8,
+ description='短期反转效应(20日)',
+ direction='negative'
+ ),
+ AlphaFactor(
+ name='Volatility_20D', # 20日波动率
+ formula='std(close - close_prev, 20) / mean(close, 20)',
+ ic_target=-0.03,
+ ir_target=0.6,
+ description='短期价格波动率',
+ direction='negative'
+ ),
+ AlphaFactor(
+ name='Volatility_60D', # 60日波动率
+ formula='std(close - close_prev, 60) / mean(close, 60)',
+ ic_target=-0.04,
+ ir_target=0.8,
+ description='中期价格波动率',
+ direction='negative'
+ ),
+ AlphaFactor(
+ name='Volume_Trend', # 成交量趋势
+ formula='mean(volume, 5) / mean(volume, 20) - 1',
+ ic_target=0.02,
+ ir_target=0.5,
+ description='成交量相对变化',
+ direction='positive'
+ ),
+ AlphaFactor(
+ name='Price_Volume_Corr', # 量价相关性
+ formula='corr(close, volume, 20)',
+ ic_target=0.03,
+ ir_target=0.6,
+ description='价格与成交量的相关性',
+ direction='positive'
+ ),
+ AlphaFactor(
+ name='RSI_14', # 14日RSI
+ formula='rsi(close, 14)',
+ ic_target=-0.02,
+ ir_target=0.5,
+ description='相对强弱指标',
+ direction='negative'
+ ),
+ AlphaFactor(
+ name='MACD_Signal', # MACD信号
+ formula='macd(close, 12, 26) - macd_signal(close, 12, 26, 9)',
+ ic_target=0.03,
+ ir_target=0.6,
+ description='MACD柱状图',
+ direction='positive'
+ ),
+ AlphaFactor(
+ name='Bollinger_Position', # 布林带位置
+ formula='(close - bollinger_lower) / (bollinger_upper - bollinger_lower)',
+ ic_target=0.02,
+ ir_target=0.5,
+ description='价格在布林带中的位置',
+ direction='negative'
)
]
@@ -348,6 +445,158 @@
return result
+ def market_cap_neutralize(
+ self,
+ data: pd.DataFrame,
+ factor_col: str,
+ market_cap_col: str = 'market_cap',
+ method: str = 'regression'
+ ) -> pd.Series:
+ """
+ 市值中性化处理
+
+ Args:
+ data: 数据 DataFrame
+ factor_col: 因子列名
+ market_cap_col: 市值列名
+ method: 中性化方法 ('regression' 或 'quantile')
+
+ Returns:
+ 市值中性化后的因子 Series
+ """
+ if market_cap_col not in data.columns:
+ return data[factor_col]
+
+ factor_data = data[factor_col].copy()
+ market_cap = data[market_cap_col].copy()
+
+ valid_mask = ~(factor_data.isna() | market_cap.isna() | (market_cap <= 0))
+
+ if method == 'regression':
+ log_mc = np.log(market_cap[valid_mask])
+ factor_valid = factor_data[valid_mask]
+
+ if len(factor_valid) < 10:
+ return factor_data
+
+ X = np.column_stack([np.ones(len(log_mc)), log_mc])
+ try:
+ beta = np.linalg.lstsq(X, factor_valid.values, rcond=None)[0]
+ residual = factor_valid.values - X @ beta
+ factor_data[valid_mask] = residual
+ except:
+ pass
+
+ elif method == 'quantile':
+ market_cap_decile = pd.qcut(market_cap[valid_mask], 10, labels=False, duplicates='drop')
+
+ for decile in market_cap_decile.unique():
+ mask = market_cap_decile == decile
+ decile_mean = factor_data[valid_mask][mask.values].mean()
+ factor_data.loc[valid_mask[mask.values].index] -= decile_mean
+
+ return factor_data
+
+ def double_neutralize(
+ self,
+ data: pd.DataFrame,
+ factor_col: str,
+ industry_col: str = 'industry',
+ market_cap_col: str = 'market_cap',
+ method: str = 'sequential'
+ ) -> pd.Series:
+ """
+ 双重中性化(行业+市值)
+
+ Args:
+ data: 数据 DataFrame
+ factor_col: 因子列名
+ industry_col: 行业列名
+ market_cap_col: 市值列名
+ method: 中性化方法 ('sequential' 或 'orthogonal')
+
+ Returns:
+ 双重中性化后的因子 Series
+ """
+ if method == 'sequential':
+ result = self.industry_neutralize(data, factor_col, industry_col)
+ temp_data = data.copy()
+ temp_data[factor_col] = result
+ result = self.market_cap_neutralize(temp_data, factor_col, market_cap_col)
+
+ elif method == 'orthogonal':
+ result = data[factor_col].copy()
+
+ valid_mask = ~(result.isna())
+ if market_cap_col in data.columns:
+ valid_mask &= ~(data[market_cap_col].isna() | (data[market_cap_col] <= 0))
+
+ if valid_mask.sum() < 20:
+ return result
+
+ X_list = [np.ones(valid_mask.sum())]
+
+ if industry_col in data.columns:
+ industries = pd.get_dummies(data.loc[valid_mask, industry_col], drop_first=True)
+ X_list.append(industries.values)
+
+ if market_cap_col in data.columns:
+ log_mc = np.log(data.loc[valid_mask, market_cap_col])
+ X_list.append(log_mc.values.reshape(-1, 1))
+
+ X = np.column_stack(X_list)
+ y = result[valid_mask].values
+
+ try:
+ beta = np.linalg.lstsq(X, y, rcond=None)[0]
+ residual = y - X @ beta
+ result[valid_mask] = residual
+ except:
+ pass
+
+ return result
+
+ def neutralize_all_factors(
+ self,
+ data: pd.DataFrame,
+ factor_list: List[str] = None,
+ industry_col: str = 'industry',
+ market_cap_col: str = 'market_cap',
+ neutralize_type: str = 'double'
+ ) -> pd.DataFrame:
+ """
+ 对所有因子进行中性化处理
+
+ Args:
+ data: 数据 DataFrame
+ factor_list: 因子列表,None则使用所有定义的因子
+ industry_col: 行业列名
+ market_cap_col: 市值列名
+ neutralize_type: 中性化类型 ('industry', 'market_cap', 'double')
+
+ Returns:
+ 中性化后的数据 DataFrame
+ """
+ if factor_list is None:
+ factor_list = [f.name for f in self.alpha_factors if f.name in data.columns]
+
+ result = data.copy()
+
+ for factor in factor_list:
+ if factor not in data.columns:
+ continue
+
+ if neutralize_type == 'industry':
+ result[factor] = self.industry_neutralize(data, factor, industry_col)
+ elif neutralize_type == 'market_cap':
+ result[factor] = self.market_cap_neutralize(data, factor, market_cap_col)
+ elif neutralize_type == 'double':
+ result[factor] = self.double_neutralize(
+ data, factor, industry_col, market_cap_col
+ )
+
+ return result
+
def calculate_alpha_score(self, stock_data: pd.DataFrame,
factor_list: List[str] = None) -> pd.Series:
"""
--
Gitblit v1.8.0