Qlib Quantitative Investment Platform
repository·main·Indexed 13 days ago
https://github.com/microsoft/qlibAn AI-oriented quantitative investment platform providing a full machine learning pipeline for financial research, including data processing, model training, and back-testing. It supports supervised learning, reinforcement learning, and market dynamics modeling, featuring implementations like ALSTM, DoubleEnsemble, GATs, and KRNN. The platform includes specialized datasets such as Alpha158 and Alpha360 and provides comprehensive signal-based and portfolio-based evaluation metrics.
What's inside Qlib
- Qlib is an AI-oriented quantitative investment platform designed to empower research and create value using AI technologies in quantitative investment. It provides a comprehensive framework covering the entire quantitative pipeline, from data management to model training, backtesting, and online serving.
What is Qlib: Quantitative Platform Overview
mainQlib is an AI-oriented quantitative investment platform designed to empower research and create value using AI technologies in quantitative investment. It allows users to test and implement quantitative investment strategies through a modular framework.Overview of Online Serving in Qlib
mainOnline Servingis a suite of modules in Qlib designed to move beyond backtesting by enabling models to make predictions in real market conditions or facilitate real trading. It allows models to utilize the latest available data for live decision-making.Key components include:
Online Manager: Manages the online workflow and tasks.Online Strategy: Defines the logic for how predictions are converted into trading actions.Online Tool: Provides utility functions for online operations.Updater: Handles the updating of models or data to ensure they remain current.
Important Requirements & Limitations:
- Data Freshness: Users must ensure their data sources are continuously updated. Qlib provides scripts (e.g., for Yahoo Finance) to automate daily data collection.
- Prediction vs. Execution: Currently, Qlib supports daily updating predictions for the next trading day. However, it does not support generating orders for the next trading day due to public data limitations.
- Task Management: For managing large numbers of models or complex tasks, users should refer to the
Task Managementdocumentation, which utilizes components likeTrainerRMorCollector.
What is Qlib?
mainQlib is an open-source, AI-oriented quantitative investment platform designed to empower research and create value using AI technologies. It covers the entire quantitative investment chain, including alpha seeking, risk modeling, portfolio optimization, and order execution.
Key capabilities include:
- Full ML Pipeline: Supports data processing, model training, and back-testing.
- Diverse Modeling Paradigms: Supports supervised learning (for mining non-linear patterns), market dynamics modeling (using adaptive concept drift), and reinforcement learning (for continuous investment decisions).
- SOTA Research: Integrates state-of-the-art quantitative research works and models.
Qlib Code Standards and Documentation requirements
mainDevelopers contributing to Qlib must adhere to the following standards:
- Docstrings: Use the
Numpydoc Stylefor all docstrings. - Formatting: Code must align with
blackformatting standards. - Linting: Code must pass
pylintandflake8checks as defined in the CI workflow.
- Docstrings: Use the
Available Data Collection Scripts
mainQlib provides several built-in scripts for collecting different types of financial data:
yahoo: Retrieves US and CN stock data from Yahoo Finance.fund: Retrieves fund data fromhttp://fund.eastmoney.com.cn_index: Retrieves Chinese indices (e.g.,CSI300,CSI100) fromhttp://www.csindex.com.cn.us_index: Retrieves US indices (e.g.,SP500,NASDAQ100,DJIA,SP400) from Wikipedia.contrib: Contains scripts for auxiliary functions.
Overview of Graph Attention Networks (GATs) in Qlib
mainQlib includes a custom implementation of Graph Attention Networks (GATs) built using PyTorch. GATs use masked self-attentional layers on graph-structured data, allowing nodes in stacked layers to attend to their neighborhoods' features. This implementation avoids costly matrix operations (like inversion) and does not require the graph structure to be known upfront.
For theoretical background, refer to the original paper: Graph Attention Networks.
What is a Point-in-Time (PIT) Database?
mainA Point-in-Time (PIT) database is designed to prevent data leakage during historical market analysis and backtesting. In financial datasets, information (like financial reports) is often amended or updated over time. If a backtest uses the latest version of a data point for a historical timestamp, it is using information that would not have been available at that time.
A PIT database ensures that for any historical timestamp, the user retrieves the specific version of the data that was actually available at that moment, maintaining consistency between historical backtesting and online trading performance.
What is Temporally Correlated Task Scheduling (TCTS)?
mainTemporally Correlated Task Scheduling (TCTS) is a framework for sequence learning that utilizes multiple temporally correlated auxiliary tasks to improve the training of a main task. In financial contexts like stock trend forecasting, these auxiliary tasks might involve predicting stock prices for different future time steps (e.g., predicting tomorrow's price vs. the day after tomorrow).
Key components include:
- Learnable Task Scheduler: An adaptive component that selects the best auxiliary task during training based on the current model status and minibatch data.
- Bi-level Optimization: The scheduler and the main model are trained jointly. The scheduler is optimized to maximize validation performance, while the model is optimized to minimize training loss guided by the scheduler's task selection.
What is DoubleEnsemble?
mainDoubleEnsemble is an ensemble framework designed for financial market prediction. It addresses two primary challenges in financial data: low signal-to-noise ratios and the increasing number of features.
It works through two main mechanisms:
- Sample Reweighting: Identifies key samples based on training dynamics (learning trajectories).
- Feature Selection: Identifies key features using shuffling-based ablation impact.
The framework is model-agnostic and can be applied to a wide range of base models to extract complex patterns while mitigating overfitting and instability.
What is State-Frequency-Memory (SFM)?
mainState-Frequency-Memory (SFM) is a recurrent network architecture designed for stock price prediction. It utilizes the Discrete Fourier Transform (DFT) to decompose the hidden states of memory cells. This allows the model to capture multi-frequency trading patterns from historical market data, which are then used to predict future stock prices.
For more details on the underlying theory, refer to the paper: Stock Price Prediction via Discovering Multi-Frequency Trading Patterns.
Understand the Qlib Data Workflow
mainThe Qlib Data Layer is designed for high-performance quantitative investment tasks. A typical workflow follows these stages:
- Data Preparation: Download raw data and convert it into Qlib format (files with the
.binsuffix). At this stage, only basic data like OHLCV (Open, High, Low, Close, Volume) is typically stored. - Feature Engineering (Data Loader): Use Qlib's expression engine to create basic features (e.g.,
Ref($close, 60) / $close). This is handled by theData Loader, which is a component of theData Handler. - Complex Processing (Data Handler): For advanced operations like data normalization that are difficult to express via simple operators, use
Data Handlerwith customized processors. - Dataset Preparation: The
Datasetcomponent takes the processed data from theData Handlerand prepares it into a format specific to your machine learning model.
- Data Preparation: Download raw data and convert it into Qlib format (files with the