Files
myquant/CLAUDE-factors.md
simonandClaude Opus 4.7 271a9343a5 Initial commit: cc-cursor 全链路量化研究平台
7 Sprints 全部完成:
  Sprint 0: 基础设施 (DataManager + MariaDB)
  Sprint 1: 因子引擎 (34因子/12分类)
  Sprint 2: VectorBT 回测 (5策略+截面)
  Sprint 3: Optuna 优化 (+Walk-Forward)
  Sprint 4: ML 模型 (LightGBM+CatBoost)
  Sprint 5: Qwen 情绪因子 (三源新闻+日期对齐)
  Sprint 6: Agent 系统 (4Agent+日报.md/.html)

生产加固 (15项): Tushare双源fallback, SSH自动恢复, pool_pre_ping,
  save_daily先删后插, load_dotenv绝对路径, 日报5d/20d修复,
  RiskAgent改上证指数, 昨日对比+数据截止, mac_report utf8mb4,
  CLAUDE-*.md 9条已知Bug, demo全参数化, djapi数据源归一化,
  indexDatas API修正

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 15:59:05 +08:00

56 lines
2.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CLAUDE-factors.md — 因子引擎 + 情绪因子
## FactorEngine (`finance/factors/engine.py`)
```python
from factors.registry import get_factor, list_factors
from factors.engine import FactorEngine
fe = FactorEngine(dm, sentiment_engine=sent)
factor_df = fe.compute("000001.SZ", [get_factor("momentum_20"), get_factor("rsi_14")])
# → DataFrame: index=trade_date, columns=[momentum_20, rsi_14]
cross = fe.compute_universe(factors, date="20250630", ts_codes=[...])
```
因子路由:`factor.category == "sentiment"` → SentimentEngine`isinstance(f, FUNDAMENTAL_FACTOR_TYPES)` → 注入财务数据。
## 因子注册表 (`finance/factors/registry.py`)
34 因子 / 12 分类:动量、RSI、MACD、量价、布林、ATR、均线、波动率、换手率、振幅、基本面、情绪。
```python
list_factors("动量") # → ['momentum_5','momentum_10','momentum_20','momentum_60']
get_factor("rsi_14") # → RSIFactor(period=14)
```
## 技术因子 (`finance/factors/technical/`)
10 类。每个继承 `BaseFactor`,实现 `calculate(df) → pd.Series`。停牌跳过,新股不足 N 日返回 NaN。
## 基本面因子 (`finance/factors/fundamental/`)
- `roe.py` — ROEFactor(同花顺 `stock_financial_abstract_ths`
- `pe_pb.py` — PEFactor/PBFactor/EPFactorclose + 财务 EPS/BVPS map to daily
## 情绪因子 (`finance/factors/sentiment/`)
### SentimentEngine (`sentiment_engine.py`)
```python
sent = SentimentEngine(dm, qwen_client=client, news_source=news_src)
sent_df = sent.compute("000001.SZ", max_news=30)
# → DataFrame: news_sent_5, news_conf_5, sent_delta_5
```
### 新闻源 (`news_source.py`)
三数据源聚合:AkShare `stock_news_em` + DB `xwlb_daily_ext` + MCP `trendradar-news`
xwlb 日期处理:DB `news_date +1day`(晚间播出→次日影响)→ `align_news_to_trading_days`
AkShare 限频 → 自动 fallback。LIMIT 按日期跨度动态计算。
### Qwen 客户端 (`qwen_client.py`)
双后端:DashScope API + 本地 Ollama。金融情绪专家 prompt,返回 `{sentiment_score, confidence, impact_duration, key_topics}`。配置:`.env` 中的 `QWEN_API_KEY``QWEN_LOCAL_BASE_URL`
### 情绪因子 (`sentiment_factor.py`)
- `NewsSentimentFactor(window, decay)` — 时间衰减加权情绪
- `SentimentConfidenceFactor(window)` — score × confidence 加权
- `SentimentMomentumFactor(period)` — 情绪变化方向