RL-MLDM / alphagen

Generating sets of formulaic alpha (predictive) stock factors via reinforcement learning.
478 stars 160 forks source link
quantitative-trading reinforcement-learning symbolic-regression

AlphaGen

Automatic formulaic alpha generation with reinforcement learning.

Paper Generating Synergistic Formulaic Alpha Collections via Reinforcement Learning accepted by KDD 2023, Applied Data Science (ADS) track.

Paper available on ACM DL or arXiv.

How to reproduce?

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.

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:

These parameters will define a RL run:

Run!

python train_maskable_ppo.py --seed=SEED --pool=POOL_CAPACITY --code=INSTRUMENTS --step=NUM_STEPS

Where SEED is random seed, e.g., 1 or 1,2, POOL_CAPACITY is the size of combination model and, NUM_STEPS is the limit of RL steps.

After running

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

Repository Structure

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},
}

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: