chore(config): 添加环境变量示例及.gitignore更新
- 新增 .env.example,包含 Tushare API、钉钉机器人和PostgreSQL数据库配置模板 - 更新.gitignore,忽略本地配置文件如 .env.local 和 config_local.py - 添加对报表文件命名规则的支持,保留示例文件不忽略 - 删除废弃的 chart.py 及相关图表模块代码 - 新增 config/settings.py,实现从环境变量读取配置的统一接口 - 设置数据目录及缓存目录,确保目录存在,提高配置管理规范性
This commit is contained in:
0
core/factors/__init__.py
Normal file
0
core/factors/__init__.py
Normal file
137
core/factors/momentum.py
Normal file
137
core/factors/momentum.py
Normal file
@@ -0,0 +1,137 @@
|
||||
"""
|
||||
动量因子计算模块
|
||||
|
||||
支持两种动量因子:
|
||||
1. N日涨幅(简单动量)
|
||||
2. 斜率×R²趋势得分(改进版)
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
|
||||
def calculate_momentum(price_series: pd.Series, n: int) -> pd.Series:
|
||||
"""
|
||||
计算 N 日涨幅(简单动量)
|
||||
|
||||
Args:
|
||||
price_series: 价格序列
|
||||
n: 动量窗口天数
|
||||
|
||||
Returns:
|
||||
Series: N日涨幅
|
||||
"""
|
||||
return price_series / price_series.shift(n + 1) - 1.0
|
||||
|
||||
|
||||
def _slope_r2_score(srs: pd.Series, n: int = 25) -> float:
|
||||
"""
|
||||
单次计算斜率×R²趋势得分
|
||||
|
||||
Args:
|
||||
srs: 价格窗口序列(长度为 n)
|
||||
n: 窗口长度
|
||||
|
||||
Returns:
|
||||
float: 斜率 × R² × 10000
|
||||
"""
|
||||
if srs.shape[0] < n:
|
||||
return np.nan
|
||||
|
||||
x = np.arange(1, n + 1).reshape(-1, 1)
|
||||
y = srs.values / srs.values[0] # 归一化
|
||||
|
||||
lr = LinearRegression().fit(x, y)
|
||||
slope = lr.coef_[0]
|
||||
r_squared = lr.score(x, y)
|
||||
score = 10000 * slope * r_squared
|
||||
|
||||
return score
|
||||
|
||||
|
||||
def calculate_slope_r2(price_series: pd.Series, n: int = 25) -> pd.Series:
|
||||
"""
|
||||
计算斜率×R²趋势得分序列
|
||||
|
||||
Args:
|
||||
price_series: 价格序列
|
||||
n: 滚动窗口天数
|
||||
|
||||
Returns:
|
||||
Series: 趋势得分序列
|
||||
"""
|
||||
return price_series.rolling(n).apply(
|
||||
lambda x: _slope_r2_score(x, n), raw=False
|
||||
)
|
||||
|
||||
|
||||
def calculate_daily_return(price_series: pd.Series) -> pd.Series:
|
||||
"""
|
||||
计算日收益率
|
||||
|
||||
Args:
|
||||
price_series: 价格序列
|
||||
|
||||
Returns:
|
||||
Series: 日收益率
|
||||
"""
|
||||
return price_series / price_series.shift(1) - 1
|
||||
|
||||
|
||||
def compute_factors(
|
||||
etf_data: pd.DataFrame,
|
||||
code_list: list,
|
||||
n: int = 25,
|
||||
factor_type: str = "slope_r2",
|
||||
) -> tuple[pd.DataFrame, list]:
|
||||
"""
|
||||
计算所有指数的因子和日收益率
|
||||
|
||||
Args:
|
||||
etf_data: DataFrame, 宽表格式的收盘价
|
||||
code_list: 指数代码列表
|
||||
n: 动量/趋势窗口
|
||||
factor_type: 'momentum' 或 'slope_r2'
|
||||
|
||||
Returns:
|
||||
tuple: (result_df, valid_codes)
|
||||
"""
|
||||
result = etf_data.copy()
|
||||
|
||||
# 过滤掉缺失值过多的指数
|
||||
total_rows = len(result)
|
||||
valid_codes = []
|
||||
for code in code_list:
|
||||
if code not in result.columns:
|
||||
print(f" ⚠ 跳过 {code}: 不在数据中")
|
||||
continue
|
||||
null_pct = result[code].isnull().sum() / total_rows
|
||||
if null_pct > 0.2:
|
||||
print(f" ⚠ 剔除 {code}: 缺失率 {null_pct:.1%} 过高")
|
||||
result = result.drop(columns=[code])
|
||||
else:
|
||||
valid_codes.append(code)
|
||||
|
||||
# 对有效指数计算因子
|
||||
for code in valid_codes:
|
||||
result[f"日收益率_{code}"] = calculate_daily_return(result[code])
|
||||
|
||||
if factor_type == "momentum":
|
||||
result[f"得分_{code}"] = calculate_momentum(result[code], n)
|
||||
elif factor_type == "slope_r2":
|
||||
result[f"得分_{code}"] = calculate_slope_r2(result[code], n)
|
||||
else:
|
||||
raise ValueError(f"不支持的因子类型: {factor_type}")
|
||||
|
||||
# 按得分列做 dropna
|
||||
score_cols = [f"得分_{code}" for code in valid_codes]
|
||||
result = result.dropna(subset=score_cols)
|
||||
|
||||
print("\n因子计算完成:")
|
||||
print(f" 因子类型: {factor_type}")
|
||||
print(f" 窗口天数: {n}")
|
||||
print(f" 有效指数: {len(valid_codes)}/{len(code_list)}")
|
||||
print(f" 有效数据: {len(result)} 行")
|
||||
|
||||
return result, valid_codes
|
||||
207
core/factors/technical.py
Normal file
207
core/factors/technical.py
Normal file
@@ -0,0 +1,207 @@
|
||||
"""
|
||||
技术指标计算模块
|
||||
|
||||
包含CCI、EMA、MACD等常用技术指标
|
||||
"""
|
||||
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import talib as ta
|
||||
|
||||
|
||||
def calculate_cci(
|
||||
df: pd.DataFrame,
|
||||
period: int = 14,
|
||||
high_col: str = "high",
|
||||
low_col: str = "low",
|
||||
close_col: str = "close",
|
||||
) -> pd.Series:
|
||||
"""
|
||||
计算CCI指标(商品通道指数)
|
||||
|
||||
Args:
|
||||
df: DataFrame with OHLC data
|
||||
period: CCI周期
|
||||
high_col: 最高价列名
|
||||
low_col: 最低价列名
|
||||
close_col: 收盘价列名
|
||||
|
||||
Returns:
|
||||
Series: CCI值
|
||||
"""
|
||||
return ta.CCI(
|
||||
high=df[high_col],
|
||||
low=df[low_col],
|
||||
close=df[close_col],
|
||||
timeperiod=period,
|
||||
)
|
||||
|
||||
|
||||
def calculate_ema(
|
||||
price_series: pd.Series,
|
||||
period: int = 20,
|
||||
) -> pd.Series:
|
||||
"""
|
||||
计算指数移动平均线
|
||||
|
||||
Args:
|
||||
price_series: 价格序列
|
||||
period: EMA周期
|
||||
|
||||
Returns:
|
||||
Series: EMA值
|
||||
"""
|
||||
return ta.EMA(price_series, timeperiod=period)
|
||||
|
||||
|
||||
def calculate_macd(
|
||||
price_series: pd.Series,
|
||||
fastperiod: int = 12,
|
||||
slowperiod: int = 26,
|
||||
signalperiod: int = 9,
|
||||
) -> tuple[pd.Series, pd.Series, pd.Series]:
|
||||
"""
|
||||
计算MACD指标
|
||||
|
||||
Args:
|
||||
price_series: 价格序列
|
||||
fastperiod: 快线周期
|
||||
slowperiod: 慢线周期
|
||||
signalperiod: 信号线周期
|
||||
|
||||
Returns:
|
||||
tuple: (macd, signal, hist)
|
||||
"""
|
||||
macd, signal, hist = ta.MACD(
|
||||
price_series,
|
||||
fastperiod=fastperiod,
|
||||
slowperiod=slowperiod,
|
||||
signalperiod=signalperiod,
|
||||
)
|
||||
return macd, signal, hist
|
||||
|
||||
|
||||
def calculate_td_sequence(close_series: pd.Series) -> pd.Series:
|
||||
"""
|
||||
计算TD序列(Tom DeMark Sequential)
|
||||
|
||||
Args:
|
||||
close_series: 收盘价序列
|
||||
|
||||
Returns:
|
||||
Series: TD序列值(正数为上涨计数,负数为下跌计数)
|
||||
"""
|
||||
close = close_series.to_list()
|
||||
td = [0, 0, 0, 0]
|
||||
up = 0
|
||||
down = 0
|
||||
|
||||
for i in range(4, len(close)):
|
||||
if close[i] > close[i - 4]:
|
||||
up += 1
|
||||
down = 0
|
||||
td.append(up)
|
||||
else:
|
||||
down -= 1
|
||||
up = 0
|
||||
td.append(down)
|
||||
|
||||
return pd.Series(td, index=close_series.index)
|
||||
|
||||
|
||||
def resample_to_weekly(df: pd.DataFrame) -> pd.DataFrame:
|
||||
"""
|
||||
将日线数据重采样为周线数据
|
||||
|
||||
Args:
|
||||
df: DataFrame with columns: date, open, high, low, close, volume
|
||||
|
||||
Returns:
|
||||
DataFrame: 周线数据
|
||||
"""
|
||||
df = df.copy()
|
||||
if "date" in df.columns:
|
||||
df["date"] = pd.to_datetime(df["date"])
|
||||
df.set_index("date", inplace=True)
|
||||
|
||||
weekly = pd.DataFrame(
|
||||
{
|
||||
"code": df["code"].resample("W").first() if "code" in df.columns else None,
|
||||
"open": df["open"].resample("W").first(),
|
||||
"high": df["high"].resample("W").max(),
|
||||
"low": df["low"].resample("W").min(),
|
||||
"close": df["close"].resample("W").last(),
|
||||
"volume": df["volume"].resample("W").sum(),
|
||||
}
|
||||
)
|
||||
|
||||
return weekly.dropna()
|
||||
|
||||
|
||||
class TechnicalScreener:
|
||||
"""技术指标筛选器基类"""
|
||||
|
||||
def __init__(self, name: str):
|
||||
self.name = name
|
||||
|
||||
def screen(self, df: pd.DataFrame) -> bool:
|
||||
"""
|
||||
判断数据是否符合筛选条件
|
||||
|
||||
Args:
|
||||
df: DataFrame with OHLCV data
|
||||
|
||||
Returns:
|
||||
bool: 是否符合条件
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class CCIScreener(TechnicalScreener):
|
||||
"""CCI超卖筛选器"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
day_period: int = 14,
|
||||
week_period: int = 14,
|
||||
threshold: float = -100,
|
||||
use_weekly: bool = True,
|
||||
):
|
||||
super().__init__("CCI超卖筛选")
|
||||
self.day_period = day_period
|
||||
self.week_period = week_period
|
||||
self.threshold = threshold
|
||||
self.use_weekly = use_weekly
|
||||
|
||||
def screen(self, df: pd.DataFrame) -> dict:
|
||||
"""
|
||||
筛选CCI超卖信号
|
||||
|
||||
Returns:
|
||||
dict: {
|
||||
'triggered': bool, # 是否触发信号
|
||||
'day_cci': float, # 日线CCI值
|
||||
'week_cci': float, # 周线CCI值(如启用)
|
||||
}
|
||||
"""
|
||||
# 计算日线CCI
|
||||
day_cci = calculate_cci(df, period=self.day_period).iloc[-1]
|
||||
|
||||
result = {
|
||||
"triggered": day_cci < self.threshold,
|
||||
"day_cci": day_cci,
|
||||
"week_cci": None,
|
||||
}
|
||||
|
||||
# 计算周线CCI(如果启用)
|
||||
if self.use_weekly:
|
||||
weekly_df = resample_to_weekly(df)
|
||||
if len(weekly_df) >= self.week_period:
|
||||
week_cci = calculate_cci(weekly_df, period=self.week_period).iloc[-1]
|
||||
result["week_cci"] = week_cci
|
||||
# 日线或周线任一超卖即触发
|
||||
result["triggered"] = (
|
||||
day_cci < self.threshold or week_cci < self.threshold
|
||||
)
|
||||
|
||||
return result
|
||||
Reference in New Issue
Block a user