General Introduction
Qlib is an open source platform developed by Microsoft that focuses on using AI technology to help users research quantitative investments. It starts with basic data processing and supports users in exploring investment ideas and turning them into usable strategies. The platform is easy to use and suitable for users who want to enhance their investment research with machine learning. qlib provides data management, model training and backtesting functions, covering the entire process of quantitative investment. Launched by the Microsoft Research team, it incorporates the latest AI technologies to handle complex financial data. Currently dated March 25, 2025, Qlib is continuously updated on GitHub with an active community that attracts many developers.
Function List
- Data Processing: Supports efficient storage and processing of financial data for quick access and analysis.
- Model Training: Provides a variety of machine learning models such as supervised learning and reinforcement learning to help predict market trends.
- Backtesting tool: Built-in backtesting function to simulate the performance of investment strategies and evaluate the effectiveness.
- Investment Strategy Generation: Optimize trading decisions by generating target portfolios from signals.
- Customizable Module: Users can adjust models and strategies to meet individual needs.
- Online and offline mode: support local deployment or shared data services, flexible choice of use.
Using Help
Installation process
Qlib needs to be installed in an environment that supports Python. Here are the detailed steps:
- Preparing the environment
- Make sure Python is installed on your computer (3.7 or 3.8 recommended). This can be done with
<code>python --version</code>
Check the version. - It is recommended that you use the Anaconda management environment. After downloading and installing Anaconda, create a new environment:
conda create -n qlib_env python=3.8
- Activate the environment:
conda activate qlib_env
- Make sure Python is installed on your computer (3.7 or 3.8 recommended). This can be done with
- Installation of dependencies
- Install the base library first:
pip install numpy pip install --upgrade cython
- Then install Qlib from GitHub:
git clone https://github.com/microsoft/qlib.git cd qlib pip install .
- If you need to develop functionality, you can use the
<code>pip install -e .[dev]</code>
The
- Install the base library first:
- Getting data
- Download sample data (for the Chinese market):
python scripts/get_data.py qlib_data --target_dir ~/.qlib/qlib_data/cn_data --region cn
- The data is saved in the user directory for subsequent use.
- Download sample data (for the Chinese market):
How to use the main features
data processing
Qlib's data processing is fast and can organize financial data into a format suitable for analysis. After running the data download command above, the data is automatically stored in the <code>~/.qlib/qlib_data/cn_data</code>
You can load the data with a Python script. You can load the data with a Python script:
import qlib
qlib.init(provider_uri="~/.qlib/qlib_data/cn_data")
from qlib.data import D
instruments = D.instruments(market="csi300")
data = D.features(instruments, ["$close", "$volume"], start_time="2023-01-01", end_time="2025-03-25")
print(data.head())
This code displays the closing price and volume of the specified stock.
model training
Qlib supports a variety of models, such as LightGBM. here are the steps to train a simple model:
- Configure the dataset and model parameters, save as
<code>workflow_config.yaml</code>
::dataset: class: DatasetH module_path: qlib.data.dataset kwargs: handler: class: Alpha158 module_path: qlib.contrib.data.handler segments: train: ["2023-01-01", "2024-01-01"] valid: ["2024-01-02", "2024-06-30"] test: ["2024-07-01", "2025-03-25"] model: class: LGBModel module_path: qlib.contrib.model.gbdt
- Run the training command:
qrun workflow_config.yaml
- When training is complete, the model is saved in the default path
<code>~/.qlib/qlib_data/models</code>
The
Backtesting tools
Backtesting tests the effectiveness of the strategy. Run a backtest with the following code:
from qlib.contrib.strategy import TopkDropoutStrategy
from qlib.backtest import backtest
strategy = TopkDropoutStrategy(topk=10, drop=2)
report = backtest(strategy=strategy, start_time="2024-01-01", end_time="2025-03-25")
print(report)
<code>topk=10</code>
Indicates the top 10 stocks.<code>drop=2</code>
Indicates that the 2 worst performing stocks are discarded each day.
The results will show return and risk indicators.
Featured Function Operation
online mode
Qlib supports online mode, sharing data through Qlib-Server:
- Install Qlib-Server:
git clone https://github.com/microsoft/qlib-server.git cd qlib-server docker-compose -f docker_support/docker-compose.yaml up -d
- Configure the client to connect to the server:
qlib.init(provider_uri="http://<server_ip>:port")
- The data will be fetched from the server to improve efficiency.
Customized Strategies
Want to try your own strategy? Inheritance is possible <code>WeightStrategyBase</code>
Class:
from qlib.contrib.strategy import WeightStrategyBase
class MyStrategy(WeightStrategyBase):
def generate_trade_decision(self, data):
return {stock: 0.1 for stock in data.index[:5]} # 前5只股票平分权重
Then test the results with a backtesting tool.
application scenario
- Individual investment research
Users can use Qlib to analyze historical data, test their investment ideas, and find strategies with higher returns. - Financial Research Team
Teams can utilize Qlib's modeling and backtesting capabilities to quickly validate the effectiveness of academic theories in the marketplace. - Educational learning
Students can use Qlib to learn about quantitative investing, hands-on data processing and model training.
QA
- What operating systems does Qlib support?
Windows, macOS, and Linux are supported, and will run as long as Python and dependent libraries are installed. - Where does the data come from?
The default data is obtained from Yahoo Finance, or users can replace it with their own data. - Need a programming foundation?
Basic Python knowledge is required, but the official documentation and examples are detailed enough for a novice to get started.