Skip to content

Latest commit

 

History

History
207 lines (160 loc) · 11.1 KB

README_en.md

File metadata and controls

207 lines (160 loc) · 11.1 KB

Alpha-R1

arXiv Hugging Face License Python Version

Alpha-R1: Alpha Screening with LLM Reasoning via Reinforcement Learning

Alpha-R1 is a reasoning-enhanced LLM for quantitative alpha selection, trained with GRPO reinforcement learning on top of Qwen3-8B. This repository is the companion implementation of the paper Alpha-R1: Alpha Screening with LLM Reasoning via Reinforcement Learning.

Overview

Alpha-R1 framework overview

Alpha-R1 screens a candidate pool of Alpha101 factors. Instead of treating alphas as bare time series, it reasons over semantic factor descriptions — how each factor works, when it works, and when it fails — and activates the factors that fit current market conditions:

qlib single-factor backtest (P_i) ─┐
                                   ├─→ LLM factor descriptions α_des (OpenRouter)
market memory (M_global) ──────────┘                 │
                                                     ↓
                           Alpha-R1 inference (FinStep/Alpha-R1)
                                                     ↓
                               parsed selections (selections.json)
                                                     ↓
                       end-to-end strategy backtest (NAV, AR/SR/MDD)
  1. Single-factor backtesting (paper §3.1.3): each Alpha101 factor is evaluated on qlib — factor values, IC/RankIC, and a top-k portfolio — and saved as a performance vector P_i.
  2. Factor description generation (§3.1.2/§3.2.1): an LLM (via OpenRouter) iteratively aggregates daily price/news text into a global market memory M_global, then maps M_global + P_i into a structured description α_des per factor.
  3. Alpha-R1 inference (§3.3): descriptions are concatenated into the decision-context prompt; the model outputs the selected factors in <alpha_list>...</alpha_list>.
  4. Output parsing: responses are validated and parsed into selections.json / summary.csv.
  5. Strategy backtest (§3.3, Appendix F): a fixed linear model scores stocks with the selected factors, and the paper's execution protocol (slot rotation, VWAP fills, fees) produces the NAV curve and metrics.

Installation

pip install -e .            # core (transformers inference + generation + parsing)
pip install -e .[vllm]      # optional high-throughput inference backend
pip install -e .[qlib]      # optional backtesting (pyqlib)

API keys (see .env.example):

export OPENROUTER_API_KEY=...   # description generation
export HF_TOKEN=...             # optional (e.g. for gated/private mirrors)

Usage

All steps read their defaults from configs/.

1. Single-factor backtests

python scripts/prepare_qlib_data.py --csv-dir data/stock_data --qlib-dir ~/.qlib/qlib_data/alpha_r1
python scripts/run_factor_backtest.py --alphas all

Writes result/alpha_backtest/alphaNNN.json per factor. See data/README.md for data layout conventions.

2. Factor descriptions

python scripts/build_market_memory.py --start-date 2023-01-01 --end-date 2024-12-31
python scripts/generate_descriptions.py --model "anthropic/claude-3.7-sonnet" --alphas all

The OpenRouter model id is intentionally left blank in configs/generation.yaml — set it there or pass --model. Writes data/market_memory/M_global.txt and result/alpha_des/alphaNNN.txt.

3. Alpha-R1 inference

python scripts/run_inference.py \
    --factor-des-dir result/alpha_des \
    --start-date 2025-01-01 --end-date 2025-12-31

Loads FinStep/Alpha-R1 via from_pretrained (default hf backend; set backend: vllm in configs/inference.yaml to switch to vLLM). Decoding defaults to temperature=0, top_p=0.7 as in the paper. Writes result/alpha_select/result_YYYYMMDD.json. A minimal smoke run without real data works with --factor-des-dir examples/factor_descriptions.

Decision days default to weekdays in the given range (a trading-calendar approximation); pass --market-state-dir to restrict to days that actually have market data.

4. Parse outputs

python scripts/parse_outputs.py --result-dir result/alpha_select

Writes selections.json (date → factor list) and summary.csv, and reports format-invalid days.

5. Strategy backtest (end-to-end)

# estimate the fixed linear model on the historical window (paper: 2020-2023)
python scripts/train_linear_model.py --alphas all
# backtest the selections with the paper's execution protocol (slot rotation, VWAP, 10 bps)
python scripts/run_strategy_backtest.py \
    --selections result/alpha_select/parsed/selections.json \
    --betas result/linear_model/betas.csv

Turns parsed selections into a tradable top-10 equal-weight portfolio: capital rotates through holding_days slots (one slot rebalanced per day), fills use daily $vwap (falling back to $close), fees are 10 bps per side, idle cash earns the risk-free rate, and decision day t scores stocks with factor values of t-1. Writes metrics JSON (AR / excess SR / MDD / Sortino / Calmar / IR vs benchmark) and a daily NAV CSV to configs/strategy.yaml: output_dir. Passing a directory of per-round selections files as --selections additionally writes the multi-round average. Limit-lock filtering is available but off by default (daily data carries no limit flags; see configs/strategy.yaml).

Results

Main experiment results (paper Table 1; 12-month out-of-sample testing period 2025-01-01 to 2025-12-31). AR = annualized return, SR = excess Sharpe ratio, MDD = max drawdown.

Backtest NAV comparison on the S&P 500 asset pool Backtest NAV comparison on the CSI 300 asset pool

Type Method S&P 500 CSI 300
AR (%) SR MDD (%) AR (%) SR MDD (%)
Non-LLMBuy & Hold19.340.8018.7522.161.3110.49
PCA7.980.2717.302.930.1714.46
XGBoost3.490.0318.458.990.5016.26
LightGBM-5.42-0.4320.9318.441.0514.92
A2C10.820.4017.7022.961.2014.86
PPO7.680.2514.9714.960.8112.95
DDPG2.53-0.0215.041.970.1216.54
TD35.540.1416.588.660.5210.26
SAC37.601.4415.189.770.5611.68
LLMGemini 2.5 Pro14.230.5517.0116.290.9014.01
Claude 3.7 Sonnet10.920.4018.8810.130.5714.49
DeepSeek‑R121.940.9314.3614.660.8114.60
Qwen3‑8B12.850.4719.5215.440.7914.38
Alpha‑R1 (Ours)47.871.6216.9140.572.236.58

On out-of-domain universes without retraining, Alpha-R1 reaches 80.54% AR (SR 2.46) on Russell 2000 and 73.52% AR (SR 2.80) on CSI 1000 (paper Table 2).

Training

Alpha-R1 is trained with GRPO on Qwen3-8B using verl and a market-feedback reward (paper Section 3.4). training/ contains the training configuration (configs/grpo_alpha_r1.yaml) and a simplified reference implementation of the reward (reward.py), pluggable via verl's custom_reward_function mechanism. See training/README.md for details.

Repository layout

src/alpha_r1/
├── factors/       Alpha101 formula library + description loading/concatenation
├── backtest/      qlib data conversion, Alpha101→qlib expressions, single-factor
│                  backtest, linear model, slot-rotation strategy backtest
├── generation/    OpenRouter client, market memory, description generation
├── inference/     transformers / vLLM backends, prompt builder, selection loop
└── parsing/       <alpha_list> extraction and validation
scripts/           CLI entry points for each pipeline step
configs/           YAML configs for backtest / generation / inference / strategy
training/          GRPO training config + reference reward (verl)
data/              raw data lives here (gitignored, see data/README.md)
examples/          minimal example inputs

Citation

@article{jiang2025alphar1,
  title={Alpha-R1: Alpha Screening with LLM Reasoning via Reinforcement Learning},
  author={Jiang, Zuoyou and Zhao, Li and Sun, Rui and Sun, Ruohan and Li, Zhongjian and Li, Jing and Jiang, Daxin and Bai, Zuo and Hua, Cheng},
  journal={arXiv preprint arXiv:2512.23515},
  year={2025}
}

License

This project is released under the MIT License.

📅 Roadmap & Updates

  • [2025.12] 📄 Paper released on arXiv.
  • [2026.09] 🧩 Code release: qlib single-factor backtests, factor description generation (OpenRouter), Alpha-R1 inference, output parsing, end-to-end strategy backtest, and the GRPO training config + reference reward.
    • ✅ Inference code (Alpha Screening Pipeline)
    • ✅ Model weights (FinStep/Alpha-R1)

Please ⭐ Star this repo to stay updated!