ClawSkills logoClawSkills

multi-factor-strategy

指导用户创建多因子选股策略并生成独立的 YAML 配置文件

介绍

{"homepage":"https://gitcode.com/datavoid/quantcli","user-invocable":true}

# Multi-Factor Strategy Assistant

引导您创建多因子选股策略并生成独立的 YAML 配置文件。

## 安装 quantcli

```bash # Install from PyPI (recommended) pip install quantcli

# Or install from source git clone https://gitcode.com/datavoid/quantcli.git cd quantcli pip install -e . ```

验证安装: ```bash quantcli --help ```

## 快速开始

一个完整的多因子选股策略 YAML 示例:

```yaml name: Value-Growth Hybrid Strategy version: 1.0.0 description: ROE + Momentum factor stock selection

screening: fundamental_conditions: # Stage 1: Financial condition screening - "roe > 0.10" # ROE > 10% - "pe_ttm < 30" # P/E < 30 - "pe_ttm > 0" # Exclude losses daily_conditions: # Stage 2: Price condition screening - "close > ma10" # Above 10-day MA limit: 100 # Keep at most 100 stocks

# Factor configuration (supports two methods, factors at top level) factors: # Method 1: Inline factor definition - name: ma10_deviation expr: "(close - ma(close, 10)) / ma(close, 10)" direction: negative description: "10-day MA deviation"

# Method 2: External reference (reference factor files in factors/ directory, include .yaml suffix) - factors/alpha_001.yaml - factors/alpha_008.yaml

ranking: weights: # Weight fusion ma10_deviation: 0.20 # Inline factor factors/alpha_001.yaml: 0.40 # External reference factor factors/alpha_008.yaml: 0.40 normalize: zscore # Normalization method

output: limit: 30 # Output top 30 stocks columns: [symbol, name, score, roe, pe_ttm, close, ma10_deviation] ```

### 因子配置方式

**因子配置支持两种方式(可混合使用):**

| 方式 | 类型 | 示例 | 描述 | |--------|------|---------|-------------| | **内联 (Inline)** | `dict` | `{name: xxx, expr: "..."}` | 在 YAML 中直接定义表达式 | | **外部 (External)** | `str` | `factors/alpha_001.yaml` | 从 `factors/` 目录加载因子文件 |

**示例:混合使用**

```yaml factors: # Inline: Custom factor - name: custom_momentum expr: "close / delay(close, 20) - 1" direction: positive

# External: Alpha101 factor library (include .yaml suffix) - factors/alpha_001.yaml - factors/alpha_005.yaml - factors/alpha_009.yaml

ranking: weights: custom_momentum: 0.3 factors/alpha_001.yaml: 0.3 factors/alpha_005.yaml: 0.2 factors/alpha_009.yaml: 0.2 ```

运行策略: ```bash quantcli filter run -f your_strategy.yaml ```

## 调用

``` /multi-factor-strategy ```

## 可用表达式函数

### 数据处理函数 | 函数 | 用法 | 描述 | |----------|-------|-------------| | delay | `delay(x, n)` | 滞后 n 个周期 | | ma | `ma(x, n)` | 简单移动平均 | | ema | `ema(x, n)` | 指数移动平均 | | rolling_sum | `rolling_sum(x, n)` | 滚动求和 | | rolling_std | `rolling_std(x, n)` | 滚动标准差 |

### 技术指标函数 | 函数 | 用法 | 描述 | |----------|-------|-------------| | rsi | `rsi(x, n=14)` | 相对强弱指标 | | correlation | `correlation(x, y, n)` | 相关系数 | | cross_up | `cross_up(a, b)` | 金叉(a 上穿 b) | | cross_down | `cross_down(a, b)` | 死叉(a 下穿 b) |

### 排名与归一化函数 | 函数 | 用法 | 描述 | |----------|-------|-------------| | rank | `rank(x)` | 截面排名 (0-1) | | zscore | `zscore(x)` | 标准化 | | sign | `sign(x)` | 符号函数 | | clamp | `clamp(x, min, max)` | 裁剪函数 |

### 条件函数 | 函数 | 用法 | 描述 | |----------|-------|-------------| | where | `where(cond, t, f)` | 条件选择 | | if | `if(cond, t, f)` | 条件选择(别名) |

### 基础字段 | 字段 | 描述 | |-------|-------------| | open, high, low, close | OHLC 价格 | | volume | 成交量 | | pe, pb | 市盈率、市净率 | | roe | 净资产收益率 | | netprofitmargin | 销售净利率 |

## 引导式工作流

### 第一步:策略目标定位

我将首先了解您的策略需求: - **策略类型**:价值、成长、动量、波动率、混合型 - **选股数量**:集中型(10-30)、中等(50-100)、分散型(200+) - **持有周期**:日内、短期(周)、中期(月)、长期(季)

### 第二步:因子选择

根据您的策略目标,推荐合适的因子组合:

**常见基本面因子**: | 因子 | 表达式 | 方向 | 描述 | |--------|------------|-----------|-------------| | roe | `roe` | 正向 | 净资产收益率 | | pe | `pe` | 负向 | 市盈率越低越好 | | pb | `pb` | 负向 | 市净率 | | netprofitmargin | `netprofitmargin` | 正向 | 销售净利率 | | revenue_growth | `revenue_yoy` | 正向 | 营收增长率 |

**常见技术面因子**: | 因子 | 表达式 | 方向 | 描述 | |--------|------------|-----------|-------------| | momentum | `(close/delay(close,20))-1` | 正向 | N 日动量 | | ma_deviation | `(close-ma(close,10))/ma(close,10)` | 负向 | 均线偏离度 | | ma_slope | `(ma(close,10)-delay(ma(close,10),5))/delay(ma(close,10),5)` | 正向 | 均线斜率 | | volume_ratio | `volume/ma(volume,5)` | 负向 | 量比 |

**Alpha101 内置因子**(可引用 `{baseDir}/alpha101/alpha_XXX`):

QuantCLI 包含 40 个 WorldQuant Alpha101 因子,可直接引用:

| 因子 | 类别 | 描述 | |--------|----------|-------------| | `alpha101/alpha_001` | 反转 | 20 日新高后下跌 | | `alpha101/alpha_002` | 反转 | 下跌量能底部 | | `alpha101/alpha_003` | 波动率 | 低波动稳定性 | | `alpha101/alpha_004` | 资金流 | 净资金流入 | | `alpha101/alpha_005` | 趋势 | 上升趋势 | | `alpha101/alpha_008` | 资金流 | 资金流入 | | `alpha101/alpha_009` | 动量 | 长期动量 | | `alpha101/alpha_010` | 反转 | 均线偏离反转 | | `alpha101/alpha_011` ~ `alpha_020` | 扩展 | 波动、动量、量价因子 | | `alpha101/alpha_021` ~ `alpha_030` | 扩展 | 量价、趋势、强度因子 | | `alpha101/alpha_031` ~ `alpha_040` | 扩展 | 仓位、波动、资金因子 |

**查看所有内置因子:** ```bash quantcli factors list ```

**使用示例:** ```yaml factors: - alpha101/alpha_001 # Reversal factor - alpha101/alpha_008 # Capital inflow - alpha101/alpha_029 # 5-day momentum ranking: weights: alpha101/alpha_001: 0.4 alpha101/alpha_008: 0.3 alpha101/alpha_029: 0.3 ```

**筛选条件示例**: ```yaml screening: conditions: - "roe > 0.10" # ROE > 10% - "netprofitmargin > 0.05" # Net profit margin > 5% ```

### 第三步:权重配置

根据因子重要性分配权重,0 表示仅用于筛选,不参与打分:

```yaml ranking: weights: # Fundamental factors roe: 0.30 pe: 0.20 # Technical factors ma_deviation: 0.30 momentum: 0.20 normalize: zscore ```

### 第四步:生成策略文件

我将为您生成完整的策略 YAML 文件:

```yaml name: Your Strategy Name version: 1.0.0 description: Strategy description

# Stage 1: Fundamental screening screening: conditions: - "roe > 0.10" - "pe < 30" limit: 200

# Stage 2: Technical ranking ranking: weights: roe: 0.30 pe: 0.20 ma_deviation: 0.30 momentum: 0.20 normalize: zscore

output: columns: [symbol, score, rank, roe, pe, momentum] limit: 30 ```

### 第五步:运行与评估

**运行策略**: ```bash quantcli filter run -f your_strategy.yaml --top 30 ```

**评估要点**: 1. **选股数量**:检查筛选条件是否合理 2. **因子分布**:因子得分分布情况 3. **行业分散度**:避免过度集中

## FAQ

**Q:如何分配因子权重?** A:核心因子 0.3-0.4,辅助因子 0.1-0.2,确保权重总和接近 1

**Q:筛选条件太严导致结果为空?** A:逐步放宽条件,先查看满足每个条件的股票数量

**Q:支持哪些表达式语法?** A:支持 40+ 个内置函数:`ma()`, `ema()`, `delay()`, `rolling_sum()`, `rsi()`, `rank()`, `zscore()` 等。

更多产品