← All tools · Guides

Qlib: install, China A-share data, and a first Alpha158 + LightGBM run

Hands-on. Install Qlib, load China A-share data, and run a first machine-learning stock-selection model.

Short answer: a first Qlib run is four steps. Make a Python 3.12 environment, pip install pyqlib, download the community A-share files, and run one qrun. On 21 Oct 2026 that sequence was run on Windows 11: about one minute to install, about one minute to download, and about four minutes to train and backtest on a 16-core CPU. Finishing the run means the pipeline works. It does not mean the model makes money.

How to choose: The first successful Qlib run is four steps: a separate Python 3.12 environment, pip install pyqlib, the community China A-share dataset, and one qrun. The official dataset is paused, so older get_data tutorials no longer download it. If the job is to score a basket of stocks every day and buy the top ranks, use Qlib. If the job is when to buy and sell one or two stocks from a moving average or an indicator, use backtrader. Qlib does not send orders to a broker. Paper-trade first, then a broker terminal such as QMT or PTrade. Comparing nine backtesting frameworks is a different article. This page only installs Qlib and runs Alpha158 plus LightGBM.

Who it is for: Someone who already writes a little Python and wants to score a basket of China A-shares with a machine-learning model, then buy the top ranks, on their own computer. The path is Python 3.12, the community A-share files, one qrun, and how to read IC, excess return, and maximum drawdown. A run on 21 Oct 2026 used Windows 11, Python 3.12, and pyqlib 0.9.7.

Who it is not for: Someone who wants to know when to buy and sell a single stock. backtrader is the shorter path for that. Someone who wants Qlib to place live orders in a brokerage account. It does not connect to the account. This page is not the tool-order guide, not the site FAQ, and not the nine-framework comparison. The official dataset is paused, so an old download command will not fetch it.

The same walkthrough in Chinese is Qlib: install, A-share data, and a first model. A single-stock moving-average example is the first backtrader strategy. Comparing nine frameworks is how to choose a backtesting framework. That comparison is not this article.

This site has no commercial relationship with Microsoft, the Qlib project, or the authors of the community data. The commands and numbers below are from the run recorded on the Chinese page on 21 Oct 2026 (Windows 11, a 16-core CPU, Python 3.12.13 in a uv virtual environment, pyqlib 0.9.7, mlflow 3.17.2). This English page did not re-run them. Another machine or another data file will take a different time and print different numbers. Catalog entry: Qlib.

01 · What Qlib is

Qlib is Microsoft's open-source AI quant platform. The licence is MIT. The work it is built for sits in front of the backtest: prepare data, compute factors, train a model, score stocks, and buy the high scores. The backtest is the last step of that pipeline.

It fits this question: each day, inside a basket of stocks, which names are most likely to outperform. If the question is when to buy and sell one stock, backtrader is the shorter path. The same first strategy in Chinese is backtrader (Chinese).

02 · Install: start with Python 3.12

The official README lists Python 3.8 through 3.12, on Linux, Windows, and macOS. Python 3.13 and 3.14 are not on that list. The test machine had only 3.14, so uv created a 3.12 virtual environment and left the system Python alone:

uv venv --python 3.12 qlib-env
qlib-env\Scripts\activate          # macOS / Linux: source qlib-env/bin/activate
uv pip install pyqlib

Without uv: install 3.12 from python.org, then py -3.12 -m venv qlib-env and pip install pyqlib. Same idea.

03 · China A-share data: the official set is paused

This is the step that now stalls most people. The Qlib README says that, because data-security policy is stricter, the official dataset is paused. It recommends the community project investment_data. The get_data download in older tutorials no longer fetches the official files.

investment_data publishes a tarball on GitHub each day. The licence is Apache-2.0. Its notes say the file merges several sources, including Wind and Caihui (those two only through 2019), Tushare, and AKShare, and that prices are adjusted. The 21 Oct 2026 file, after extraction, looked like this:

On Windows, in PowerShell, using the curl.exe and tar that shipped with that Windows 11 machine:

curl.exe -L -o qlib_bin.tar.gz https://github.com/chenditc/investment_data/releases/latest/download/qlib_bin.tar.gz
mkdir $HOME\.qlib\qlib_data\cn_data
tar -zxf qlib_bin.tar.gz -C $HOME\.qlib\qlib_data\cn_data --strip-components=1

On macOS or Linux, the commands from that README:

wget https://github.com/chenditc/investment_data/releases/latest/download/qlib_bin.tar.gz
mkdir -p ~/.qlib/qlib_data/cn_data
tar -zxvf qlib_bin.tar.gz -C ~/.qlib/qlib_data/cn_data --strip-components=1

The archive is more than 500 MB. Unpacked, it is about 700 MB. The 21 Oct 2026 release itself was 543 MB. The project's notes say to check the download first. The same release page has qlib_bin.manifest.json, which records the archive's SHA256. On Windows, Get-FileHash qlib_bin.tar.gz -Algorithm SHA256 should match that file before you extract. The copy used for this walkthrough matched on size and on SHA256.

04 · The first run: one qrun

Download the official example workflow_config_lightgbm_Alpha158.yaml (the v0.9.7 tag, matching the pip install). Line 2, provider_uri, is the data directory. The default is ~/.qlib/qlib_data/cn_data, which is where the previous step extracted the files. If the data sits somewhere else, or the run says it cannot find the data, replace that value with the full path. The test run did that. Then:

$env:MLFLOW_ALLOW_FILE_STORE="true"      # macOS / Linux: export MLFLOW_ALLOW_FILE_STORE=true
qrun workflow_config_lightgbm_Alpha158.yaml

The first line is a pitfall from the actual run. Without it, mlflow in a fresh environment (Qlib uses it to store the experiment; the installed release was 3.17.2) exits with "filesystem tracking backend is in maintenance mode". The error message itself names this environment variable as the bypass. With it set, reading the data, training, and the backtest took about four minutes on a 16-core CPU. The experiment log is the mlruns folder in the directory where you ran the command.

What that example config does, in plain language:

That last line is the example's assumption, not today's real schedule. China's securities transaction stamp tax has been charged at half the previous rate since 28 Aug 2023 (a Ministry of Finance notice). Commission and the transfer fee differ by broker. Use the schedule from the broker where the account is opened. To bring the backtest closer to that bill, change open_cost, close_cost, and min_cost in the config.

05 · What Alpha158 is

Alpha158 is the factor set that ships with Qlib: the clues the model sees. Counted from the installed 0.9.7 source, there are exactly 158. All of them use open, high, low, close, and volume. None of them use financial statements.

They do not guarantee a useful signal. They give the model a ready starting point. To use your own factors, replace the Alpha158 handler in the config, or add to it.

06 · How to read the result

The end of the log prints several numbers. The table is that run, next to the same model on Qlib's benchmarks page (the mean of 20 random seeds):

MetricThis runPublished by QlibWhat it means
IC0.0470.0448How closely the scores move with the next period's return
Rank IC0.0490.0469The same relationship, using ranks only
Annualized excess return (after cost)11.6%9.0%How much the book beat the CSI 300 each year
Maximum drawdown (after cost)-8.2%-10.4%The largest drop in that excess from a peak

07 · Make it yours

08 · Pitfalls

09 · What to read next

Frequently asked questions

How to choose Qlib for a first run?

The first successful Qlib run is four steps: a separate Python 3.12 environment, pip install pyqlib, the community China A-share dataset, and one qrun. The official dataset is paused, so older get_data tutorials no longer download it. If the job is to score a basket of stocks every day and buy the top ranks, use Qlib. If the job is when to buy and sell one or two stocks from a moving average or an indicator, use backtrader. Qlib does not send orders to a broker. Paper-trade first, then a broker terminal such as QMT or PTrade. Comparing nine backtesting frameworks is a different article. This page only installs Qlib and runs Alpha158 plus LightGBM.

Who it is for?

Someone who already writes a little Python and wants to score a basket of China A-shares with a machine-learning model, then buy the top ranks, on their own computer. The path is Python 3.12, the community A-share files, one qrun, and how to read IC, excess return, and maximum drawdown. A run on 21 Oct 2026 used Windows 11, Python 3.12, and pyqlib 0.9.7.

Who it is not for?

Someone who wants to know when to buy and sell a single stock. backtrader is the shorter path for that. Someone who wants Qlib to place live orders in a brokerage account. It does not connect to the account. This page is not the tool-order guide, not the site FAQ, and not the nine-framework comparison. The official dataset is paused, so an old download command will not fetch it.

Is Qlib free, and can I use it commercially?

The code is free. Qlib is Microsoft's open-source project under the MIT licence, so personal and commercial use of the code are allowed. The data is separate. The official dataset is paused. The community dataset investment_data is Apache-2.0. It merges sources including Tushare and AKShare. Confirm each source's terms before commercial use.

Which Python version does Qlib support?

The official README lists Python 3.8 through 3.12, on Linux, Windows, and macOS. Python 3.13 and 3.14 are not on that list. Use a separate 3.12 virtual environment. The 21 Oct 2026 run used Python 3.12.

Where does Qlib's China A-share data come from?

The official README says the official dataset is temporarily disabled because data-security policy is stricter, and it recommends the community project investment_data. That project publishes a tarball on GitHub each day. The 21 Oct 2026 file was 543 MB and covered 6,170 stocks from 4 Jan 2000 through 21 Oct 2026, including delisted names and the Beijing Stock Exchange.

Does Qlib run on Windows?

Yes. On Windows 11, with Python 3.12, pyqlib installed, the built-in tar extracted the data, and qrun finished. The one failure was a fresh mlflow install. Set MLFLOW_ALLOW_FILE_STORE=true before the run.

Can Qlib send live orders?

No. Qlib is a research platform. It handles data, factors, models, and the backtest. It does not connect to a brokerage account and send orders. To trade the selected names, people usually add a broker terminal such as QMT or PTrade, and they should paper-trade for a while first.

How do I choose between Qlib and backtrader?

It depends on the strategy. Scoring a basket of stocks with a model each day and buying the top ranks is Qlib. Watching one or two stocks and trading a moving average or an indicator is more direct in backtrader.

This is not investment advice. A machine-learning result on past data is not a future return. Qlib's features, versions, and licence follow its own notice, and so do the community data files.