chore(config): 添加环境变量示例及.gitignore更新

- 新增 .env.example,包含 Tushare API、钉钉机器人和PostgreSQL数据库配置模板
- 更新.gitignore,忽略本地配置文件如 .env.local 和 config_local.py
- 添加对报表文件命名规则的支持,保留示例文件不忽略
- 删除废弃的 chart.py 及相关图表模块代码
- 新增 config/settings.py,实现从环境变量读取配置的统一接口
- 设置数据目录及缓存目录,确保目录存在,提高配置管理规范性
This commit is contained in:
2026-03-18 23:33:40 +08:00
parent 7c93be4b41
commit 988c2335fb
39 changed files with 2983 additions and 1011 deletions

0
core/__init__.py Normal file
View File

0
core/common/__init__.py Normal file
View File

96
core/common/db.py Normal file
View File

@@ -0,0 +1,96 @@
"""
数据库配置和连接工具
"""
import psycopg2
from psycopg2.extras import RealDictCursor
from sqlalchemy import create_engine
import pandas as pd
from loguru import logger
from typing import Optional
from config.settings import get_db_config
class DatabaseManager:
"""数据库管理类"""
def __init__(self, config: dict = None):
self.config = config or get_db_config()
self.engine = None
def get_engine(self):
"""获取SQLAlchemy引擎"""
if self.engine is None:
conn_str = (
f"postgresql://{self.config['username']}:{self.config['password']}"
f"@{self.config['host']}:{self.config['port']}/{self.config['database']}"
)
self.engine = create_engine(
conn_str,
pool_pre_ping=True,
pool_recycle=300,
echo=False,
)
return self.engine
def get_connection(self):
"""获取psycopg2连接"""
return psycopg2.connect(
host=self.config["host"],
port=self.config["port"],
database=self.config["database"],
user=self.config["username"],
password=self.config["password"],
)
def test_connection(self) -> bool:
"""测试数据库连接"""
try:
with self.get_connection() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT 1")
result = cursor.fetchone()
logger.info("数据库连接测试成功")
return True
except Exception as e:
logger.error(f"数据库连接测试失败: {e}")
return False
def execute_query(self, query: str, params: tuple = None) -> Optional[list]:
"""执行查询并返回结果"""
try:
with self.get_connection() as conn:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute(query, params)
result = cursor.fetchall()
return [dict(row) for row in result]
except Exception as e:
logger.error(f"执行查询失败: {e}")
return None
def insert_dataframe(
self, df: pd.DataFrame, table_name: str, if_exists: str = "append"
) -> bool:
"""将DataFrame插入到数据库表"""
try:
engine = self.get_engine()
df.to_sql(
table_name,
engine,
if_exists=if_exists,
index=False,
method="multi",
chunksize=1000,
)
logger.info(f"成功插入 {len(df)} 条记录到表 {table_name}")
return True
except Exception as e:
logger.error(f"插入数据到表 {table_name} 失败: {e}")
return False
def close(self):
"""关闭连接"""
if self.engine:
self.engine.dispose()
self.engine = None

210
core/common/notify.py Normal file
View File

@@ -0,0 +1,210 @@
"""
通知模块 - 支持钉钉、日志等多种通知方式
"""
import requests
import time
import hmac
import hashlib
import base64
import urllib.parse
from loguru import logger
from typing import Optional
from config.settings import get_dingtalk_config
class DingTalkBot:
"""钉钉机器人类"""
def __init__(self, webhook: str = None, secret: str = None):
"""
初始化钉钉机器人
Args:
webhook: 钉钉自定义机器人webhook地址
secret: 加签密钥(可选)
"""
config = get_dingtalk_config()
self.webhook = webhook or config.get("webhook", "")
self.secret = secret or config.get("secret", "")
if not self.webhook:
logger.warning("钉钉webhook未配置消息将不会被发送")
def _gen_signed_url(self) -> str:
"""生成带签名的URL"""
if not self.secret:
return self.webhook
timestamp = str(round(time.time() * 1000))
secret_enc = self.secret.encode("utf-8")
string_to_sign = f"{timestamp}\n{self.secret}"
string_to_sign_enc = string_to_sign.encode("utf-8")
hmac_code = hmac.new(
secret_enc, string_to_sign_enc, digestmod=hashlib.sha256
).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return f"{self.webhook}&timestamp={timestamp}&sign={sign}"
def send_text(
self, content: str, at_mobiles: list = None, is_at_all: bool = False
) -> bool:
"""
发送文本消息
Args:
content: 消息内容
at_mobiles: 需要@的手机号列表
is_at_all: 是否@所有人
Returns:
bool: 是否发送成功
"""
if not self.webhook:
logger.warning(f"[钉钉消息未发送] {content[:100]}...")
return False
at_mobiles = at_mobiles or []
data = {
"msgtype": "text",
"text": {"content": content},
"at": {"atMobiles": at_mobiles, "isAtAll": is_at_all},
}
url = self._gen_signed_url()
try:
response = requests.post(url, json=data, timeout=5)
response.raise_for_status()
result = response.json()
if result.get("errcode", -1) != 0:
logger.error(f"钉钉消息发送失败: {result}")
return False
logger.info("钉钉消息发送成功")
return True
except Exception as e:
logger.error(f"钉钉消息发送异常: {e}")
return False
def send_markdown(
self,
title: str,
text: str,
at_mobiles: list = None,
is_at_all: bool = False,
) -> bool:
"""
发送markdown消息
Args:
title: 消息标题
text: markdown格式的消息内容
at_mobiles: 需要@的手机号列表
is_at_all: 是否@所有人
Returns:
bool: 是否发送成功
"""
if not self.webhook:
logger.warning(f"[钉钉Markdown未发送] {title}")
return False
at_mobiles = at_mobiles or []
data = {
"msgtype": "markdown",
"markdown": {"title": title, "text": text},
"at": {"atMobiles": at_mobiles, "isAtAll": is_at_all},
}
url = self._gen_signed_url()
try:
response = requests.post(url, json=data, timeout=5)
response.raise_for_status()
result = response.json()
if result.get("errcode", -1) != 0:
logger.error(f"钉钉markdown消息发送失败: {result}")
return False
logger.info("钉钉markdown消息发送成功")
return True
except Exception as e:
logger.error(f"钉钉markdown消息发送异常: {e}")
return False
class NotificationManager:
"""通知管理器 - 统一管理多种通知渠道"""
def __init__(self):
self.dingtalk = DingTalkBot()
def notify(self, message: str, title: str = "系统通知", use_markdown: bool = False):
"""
发送通知(优先使用钉钉,失败则记录日志)
Args:
message: 消息内容
title: 消息标题markdown模式使用
use_markdown: 是否使用markdown格式
"""
if use_markdown:
success = self.dingtalk.send_markdown(title, message)
else:
success = self.dingtalk.send_text(message)
if not success:
# 钉钉发送失败,记录到日志
logger.info(f"[通知] {title}: {message}")
def notify_error(self, error_msg: str):
"""发送错误通知"""
markdown = f"""## 错误告警
**时间**: {time.strftime('%Y-%m-%d %H:%M:%S')}
**错误信息**:
```
{error_msg}
```
"""
self.notify(markdown, title="系统错误", use_markdown=True)
def notify_signal(self, signals: list, signal_type: str = "CCI超卖"):
"""
发送交易信号通知
Args:
signals: 信号列表每项为dict包含code, name等指标
signal_type: 信号类型名称
"""
if not signals:
logger.info(f"[{signal_type}] 无信号")
return
# 构建markdown表格
if signals:
headers = signals[0].keys()
header_line = " | ".join(headers)
separator = " | ".join(["---"] * len(headers))
rows = []
for s in signals:
row = " | ".join(str(v) for v in s.values())
rows.append(row)
table = f"{header_line}\n{separator}\n" + "\n".join(rows)
else:
table = ""
markdown = f"""## {signal_type}信号
**时间**: {time.strftime('%Y-%m-%d %H:%M:%S')}
**筛选结果**:
{table}
{len(signals)} 个标的符合筛选条件。
"""
self.notify(markdown, title=f"{signal_type}信号", use_markdown=True)

190
core/common/utils.py Normal file
View File

@@ -0,0 +1,190 @@
"""
通用工具函数
"""
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from typing import Optional
def format_date(date_str: str, output_format: str = "%Y-%m-%d") -> str:
"""
统一日期格式
Args:
date_str: 输入日期字符串(支持 YYYY-MM-DD 或 YYYYMMDD
output_format: 输出格式
Returns:
str: 格式化后的日期字符串
"""
# 尝试解析多种格式
for fmt in ["%Y-%m-%d", "%Y%m%d", "%Y/%m/%d"]:
try:
dt = datetime.strptime(date_str, fmt)
return dt.strftime(output_format)
except ValueError:
continue
raise ValueError(f"无法解析日期格式: {date_str}")
def get_date_range(
start_date: Optional[str] = None,
end_date: Optional[str] = None,
lookback_days: int = 365,
) -> tuple[str, str]:
"""
获取日期范围
Args:
start_date: 开始日期None则根据lookback_days计算
end_date: 结束日期None则使用今天
lookback_days: 回溯天数
Returns:
tuple: (start_date, end_date) 格式为 YYYY-MM-DD
"""
if end_date is None:
end = datetime.now()
else:
end = datetime.strptime(format_date(end_date), "%Y-%m-%d")
if start_date is None:
start = end - timedelta(days=lookback_days)
else:
start = datetime.strptime(format_date(start_date), "%Y-%m-%d")
return start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")
def calculate_cagr(
nav_series: pd.Series,
method: str = "natural_days",
) -> float:
"""
计算年化收益率CAGR
Args:
nav_series: 净值序列index=日期)
method: 'natural_days''trading_days'
Returns:
float: CAGR值
"""
total_return = nav_series.iloc[-1] / nav_series.iloc[0]
if method == "natural_days":
days = (nav_series.index[-1] - nav_series.index[0]).days
years = days / 365.0
elif method == "trading_days":
years = len(nav_series) / 252.0
else:
raise ValueError(f"不支持的CAGR计算方式: {method}")
if years <= 0:
return 0.0
return total_return ** (1 / years) - 1
def calculate_max_drawdown(nav_series: pd.Series) -> tuple[float, datetime, datetime]:
"""
计算最大回撤
Returns:
tuple: (最大回撤比例, 回撤起始日, 回撤结束日)
"""
cummax = nav_series.cummax()
drawdown = (nav_series - cummax) / cummax
max_dd = drawdown.min()
end_idx = drawdown.idxmin()
start_idx = nav_series[:end_idx].idxmax()
return max_dd, start_idx, end_idx
def calculate_sharpe(
returns: pd.Series,
rf: float = 0.0,
periods: int = 252,
) -> float:
"""
计算年化夏普比率
Args:
returns: 日收益率序列
rf: 无风险利率(年化)
periods: 年化系数
Returns:
float: 夏普比率
"""
excess_returns = returns - rf / periods
if excess_returns.std() == 0:
return 0.0
return excess_returns.mean() / excess_returns.std() * np.sqrt(periods)
def resample_data(
df: pd.DataFrame,
timeframe: str,
time_col: str = "time",
) -> pd.DataFrame:
"""
对数据进行重采样
Args:
df: 原始数据
timeframe: 目标周期 ('1D', '1W', '1M', '1Y')
time_col: 时间列名
Returns:
DataFrame: 重采样后的数据
"""
timeframe_map = {
"1D": "D",
"1W": "W",
"1M": "M",
"3M": "3M",
"1Y": "Y",
}
if timeframe not in timeframe_map:
return df
df = df.copy()
if time_col in df.columns:
df[time_col] = pd.to_datetime(df[time_col])
df.set_index(time_col, inplace=True)
rule = timeframe_map[timeframe]
resampled = (
df.resample(rule)
.agg(
{
"open": "first",
"high": "max",
"low": "min",
"close": "last",
"volume": "sum",
}
)
.dropna()
)
return resampled.reset_index()
def safe_divide(a: float, b: float, default: float = 0.0) -> float:
"""安全除法避免除以0"""
return a / b if b != 0 else default
def truncate_string(s: str, max_length: int = 50, suffix: str = "...") -> str:
"""截断字符串"""
if len(s) <= max_length:
return s
return s[: max_length - len(suffix)] + suffix

0
core/factors/__init__.py Normal file
View File

137
core/factors/momentum.py Normal file
View 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: 斜率 ×× 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
View 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