Zurück zum Ranking

ICT-FinD-Lab/alphagen

Python

Generating sets of formulaic alpha (predictive) stock factors via reinforcement learning.

quantitative-tradingreinforcement-learningsymbolic-regression
Sterne-Wachstum
Sterne
1.2k
Forks
314
Wochenwachstum
Issues
23
5001k
Mai 2023Mai 2024Juni 2025Juli 2026
ArtefaktePyPIpip install alphagen
README

AlphaGen

Automatic formulaic alpha generation with reinforcement learning.

This repository contains the code for our paper Generating Synergistic Formulaic Alpha Collections via Reinforcement Learning accepted by KDD 2023, Applied Data Science (ADS) track, publically available on ACM DL. Some extensions upon this work are also included in this repo.

Repository Structure

  • /alphagen contains the basic data structures and the essential modules for starting an alpha mining pipeline;
  • /alphagen_qlib contains the qlib-specific APIs for data preparation;
  • /alphagen_generic contains data structures and utils designed for our baselines, which basically follow gplearn APIs, but with modifications for quant pipeline;
  • /alphagen_llm contains LLM client abstractions and a set of prompts useful for LLM-based alpha generation, and also provides some LLM-based automatic iterative alpha-generation routines.
  • /gplearn and /dso contains modified versions of our baselines;
  • /scripts contains several scripts for running the experiments.

Result Reproduction

Note that you can either use our builtin alpha calculation pipeline (see Choice 1), or implement an adapter to your own pipeline (see Choice 2).

Choice 1: Stock data preparation

Builtin pipeline requires Qlib library and local-storaged stock data.

  • READ THIS! We need some of the metadata (but not the actual stock price/volume data) given by Qlib, so follow the data preparing process in Qlib first.
  • The actual stock data we use are retrieved from baostock, due to concerns on the timeliness and truthfulness of the data source used by Qlib.
  • The data can be downloaded by running the script data_collection/fetch_baostock_data.py. The newly downloaded data is saved into ~/.qlib/qlib_data/cn_data_baostock_fwdadj by default. This path can be customized to fit your specific needs, but make sure to use the correct path when loading the data (In alphagen_qlib/stock_data.py, function StockData._init_qlib, the path should be passed to qlib with qlib.init(provider_uri=path)).

Choice 2: Adapt to external pipelines

Maybe you have better implements of alpha calculation, you can implement an adapter of alphagen.data.calculator.AlphaCalculator. The interface is defined as follows:

class AlphaCalculator(metaclass=ABCMeta):
    @abstractmethod
    def calc_single_IC_ret(self, expr: Expression) -> float:
        'Calculate IC between a single alpha and a predefined target.'

    @abstractmethod
    def calc_single_rIC_ret(self, expr: Expression) -> float:
        'Calculate Rank IC between a single alpha and a predefined target.'

    @abstractmethod
    def calc_single_all_ret(self, expr: Expression) -> Tuple[float, float]:
        'Calculate both IC and Rank IC between a single alpha and a predefined target.'

    @abstractmethod
    def calc_mutual_IC(self, expr1: Expression, expr2: Expression) -> float:
        'Calculate IC between two alphas.'

    @abstractmethod
    def calc_pool_IC_ret(self, exprs: List[Expression], weights: List[float]) -> float:
        'First combine the alphas linearly,'
        'then Calculate IC between the linear combination and a predefined target.'

    @abstractmethod
    def calc_pool_rIC_ret(self, exprs: List[Expression], weights: List[float]) -> float:
        'First combine the alphas linearly,'
        'then Calculate Rank IC between the linear combination and a predefined target.'

    @abstractmethod
    def calc_pool_all_ret(self, exprs: List[Expression], weights: List[float]) -> Tuple[float, float]:
        'First combine the alphas linearly,'
        'then Calculate both IC and Rank IC between the linear combination and a predefined target.'

Reminder: the values evaluated from different alphas may have drastically different scales, we recommend that you should normalize them before combination.

Before running

All principle components of our expriment are located in train_maskable_ppo.py.

These parameters may help you build an AlphaCalculator:

  • instruments (Set of instruments)
  • start_time & end_time (Data range for each dataset)
  • target (Target stock trend, e.g., 20d return rate)

These parameters will define a RL run:

  • batch_size (PPO batch size)
  • features_extractor_kwargs (Arguments for LSTM shared net)
  • device (PyTorch device)
  • save_path (Path for checkpoints)
  • tensorboard_log (Path for TensorBoard)

Run the experiments

Please run the individual scripts at the root directory of this project as modules, i.e. python -m scripts.NAME ARGS.... Use python -m scripts.NAME -h for information on the arguments.

  • scripts/rl.py: Main experiments of AlphaGen/HARLA
  • scripts/llm_only.py: Alpha generator based solely on iterative interactions with an LLM.
  • scripts/llm_test_validity.py: Tests on how the system prompt affects the valid alpha rate of an LLM.

After running

  • Model checkpoints and alpha pools are located in save_path;
    • The model is compatiable with stable-baselines3
    • Alpha pools are formatted in human-readable JSON.
  • Tensorboard logs are located in tensorboard_log.

Baselines

GP-based methods

gplearn implements Genetic Programming, a commonly used method for symbolic regression. We maintained a modified version of gplearn to make it compatiable with our task. The corresponding experiment scipt is gp.py

Deep Symbolic Regression

DSO is a mature deep learning framework for symbolic optimization tasks. We maintained a minimal version of DSO to make it compatiable with our task. The corresponding experiment scipt is dso.py

Trading (Experimental)

We implemented some trading strategies based on Qlib. See backtest.py and trade_decision.py for demos.

Citing our work

@inproceedings{alphagen,
    author = {Yu, Shuo and Xue, Hongyan and Ao, Xiang and Pan, Feiyang and He, Jia and Tu, Dandan and He, Qing},
    title = {Generating Synergistic Formulaic Alpha Collections via Reinforcement Learning},
    year = {2023},
    doi = {10.1145/3580305.3599831},
    booktitle = {Proceedings of the 29th ACM SIGKDD Conference on Knowledge Discovery and Data Mining},
}
@article{HARLA,
  author = {Yu, Shuo and Xue, Hongyan and Ao, Xiang and He, Qing},
  title = {A hybrid approach to formulaic alpha discovery with large language model assistance},
  journal = {Frontiers of Computer Science},
  volume = {20},
  number = {2},
  pages = {2002316},
  year = {2026},
  doi = {10.1007/S11704-025-41061-5},
}

Contributing

Feel free to submit Issues or Pull requests.

Contributors

This work is maintained by the MLDM research group, IIP, ICT, CAS.

Maintainers include:

Thanks to the following contributors:

Thanks to the following in-depth research on our project:

  • 因子选股系列之九十五: DFQ强化学习因子组合挖掘系统
Ähnliche Repositories
ZhuLinsen/daily_stock_analysis

LLM 驱动的多市场股票智能分析系统:多源行情、实时新闻、决策看板与自动推送,支持零成本定时运行。 LLM-powered multi-market stock analysis system with multi-source market data, real-time news, decision dashboard, automated notifications, and cost-free scheduled runs.

PythonPyPIMIT Licenseaigcllm
dsa.zhulinsen.tech
58.2k50k
microsoft/qlib

Qlib is an AI-oriented Quant investment platform that aims to use AI tech to empower Quant Research, from exploring ideas to implementing productions. Qlib supports diverse ML modeling paradigms, including supervised learning, market dynamics modeling, and RL, and is now equipped with https://github.com/microsoft/RD-Agent to automate R&D process.

PythonPyPIMIT Licensequantitative-financemachine-learning
qlib.readthedocs.io/en/latest/
46.5k7.4k
wilsonfreitas/awesome-quant

A curated list of insanely awesome libraries, packages and resources for Quants (Quantitative Finance)

HTMLfinancefinancial-data
wilsonfreitas.github.io/awesome-quant/
28.1k3.7k
UFund-Me/Qbot

[🔥updating ...] AI 自动量化交易机器人(完全本地部署) AI-powered Quantitative Investment Research Platform. 📃 online docs: https://ufund-me.github.io/Qbot ✨ :news: qbot-mini: https://github.com/Charmve/iQuant

Jupyter NotebookMIT Licensefundsmachine-learning
github.com/Charmve
18.1k2.6k
bbfamily/abu

阿布量化交易系统(股票,期权,期货,比特币,机器学习) 基于python的开源量化交易,量化投资架构

PythonPyPIGNU General Public License v3.0quanttrade
abuquant.com
17.9k4.6k
je-suis-tm/quant-trading

Python quantitative trading strategies including VIX Calculator, Pattern Recognition, Commodity Trading Advisor, Monte Carlo, Options Straddle, Shooting Star, London Breakout, Heikin-Ashi, Pair Trading, RSI, Bollinger Bands, Parabolic SAR, Dual Thrust, Awesome, MACD

PythonPyPIApache License 2.0trading-strategiesquantitative-trading
je-suis-tm.github.io/quant-trading
10.4k1.8k
paperswithbacktest/awesome-systematic-trading

A curated list of awesome libraries, packages, strategies, books, blogs, tutorials for systematic trading.

PythonPyPIfinanceawesome
paperswithbacktest.com
8.6k1.2k
jesse-ai/jesse

An advanced crypto trading bot written in Python

JavaScriptnpmMIT Licensecryptocurrencytrading-bot
jesse.trade
8.2k1.2k
ranaroussi/quantstats

Portfolio analytics for quants, written in Python

PythonPyPIApache License 2.0quantalgotrading
7.5k1.2k
Superalgos/Superalgos

Free, open-source crypto trading bot, automated bitcoin / cryptocurrency trading software, algorithmic trading bots. Visually design your crypto trading bot, leveraging an integrated charting system, data-mining, backtesting, paper trading, and multi-server crypto bot deployments.

JavaScriptnpmApache License 2.0open-sourcecrypto-trading-bots
superalgos.org
5.6k6k
wangzhe3224/awesome-systematic-trading

A curated list of insanely awesome libraries, packages and resources for systematic trading. Crypto, Stock, Futures, Options, CFDs, FX, and more | 量化交易 | 量化投资

HTMLMIT Licensesystematic-tradingquantitative-trading
wangzhe3224.github.io/awesome-systematic-trading/
4.5k587
nkaz001/hftbacktest

Free, open source, a high frequency trading and market making backtesting and trading bot, which accounts for limit orders, queue positions, and latencies, utilizing full tick data for trades and order books(Level-2 and Level-3), with real-world crypto trading examples for Binance and Bybit

Rustcrates.ioMIT Licensebacktestinghigh-frequency-trading
4.3k838