Running Qlib Project Demos and Common Errors: A Summary¶
This error
ModuleNotFoundError: No module named 'qlib.data._libs.rolling'
indicates that Qlib's C extension modules were not compiled successfully, so qlib.data._libs.rolling cannot be found.
This is a problem frequently encountered when running Qlib in source-code mode. There are several solutions:
✅ Solutions¶
-
Do not run commands from the source directory
-
You were running from the
/root/qlib/directory - Python prioritizes loading modules from the current directory, which prevents the compiled extensions from being loaded correctly 👉 It is recommended to switch directories before running, for example:
cd ~
python -m qlib.cli.data qlib_data --target_dir ~/.qlib/qlib_data/cn_data --region cn
-
Confirm the C extensions are installed (or compiled)
-
If you got the source code directly via
git clone, you need to compile it first:
cd /root/qlib
pip install -e . --verbose
This compiles the Cython modules under qlib/data/_libs.
Or simply install the PyPI version:
pip install pyqlib
-
Check that dependencies are complete
-
Qlib requires
numpy,pandas,scipy,cython, etc. - It is recommended to install them first:
pip install --upgrade pip setuptools wheel cython
pip install numpy pandas scipy
- Verify the installation succeeded
python -c "import qlib; print(qlib.__version__)"
If there is no error, then run:
python -m qlib.cli.data qlib_data --target_dir ~/.qlib/qlib_data/cn_data --region cn
⚠️ Key points:
- Do not run Qlib commands directly from the source directory, otherwise Python will prioritize importing the uncompiled source code.
- The Cython extensions must be compiled, otherwise
qlib.data._libs.rollingwill never be found. *
When running the examples via qrun, most errors are pandas and numpy version issues. Change the pinned-version lines in requirements.txt to
pandas>=2.2.0 numpy>=1.26.0
pip install xgboost catboost
Running the First Qlib Example¶
qrun benchmarks/LightGBM/workflow_config_lightgbm_Alpha158.yaml
This output is the metric table for the strategy's daily return analysis in a quantitative backtest. The meaning of each parameter is as follows 👇
1. mean¶
- Meaning: the average daily return (here 0.000477 ≈ 0.0477%/day).
- Explanation: how much the strategy earns per day over the long run.
2. std¶
- Meaning: the standard deviation of daily returns (here 0.012295 ≈ 1.23%/day).
- Explanation: reflects the magnitude of return volatility; the larger the std, the higher the risk.
3. annualized_return¶
- Meaning: the daily return converted to an annualized return (here 0.113561 ≈ 11.36%/year).
- Calculation: the commonly used formula is
$$ (1 + \text{mean})^{252} - 1 $$
where 252 is the approximate number of trading days in a year.
4. information_ratio (IR)¶
- Meaning: the ratio of the strategy's excess return to its volatility, here 0.598699.
-
Explanation: similar to the Sharpe Ratio, but generally compared against the benchmark index's return.
-
0: means it beats the benchmark
- The larger the better; usually >0.5 is considered meaningful
5. max_drawdown¶
- Meaning: max drawdown, here -0.370479 ≈ -37%.
- Explanation: the maximum percentage loss from peak to trough during the backtest period.
- Intuitively, an investor could lose 37% of their capital in the worst-case scenario.
📊 One-sentence summary: Your strategy earns an average of 0.0477% per day with an annualized return of 11.36%, but could lose up to 37% at worst. An information ratio of 0.6 means the return is decent relative to the risk, but the drawdown risk is fairly large.
The strategy's performance during the backtest period is as follows:
-
Return performance
-
The strategy's average daily return is 0.0477%, which translates to an annualized return of approximately 11.36%.
-
This means that if you hold the strategy long term, the expected return level is in the double digits, a relatively stable return range.
-
Risk level
-
The strategy's daily return volatility (standard deviation) is 1.23%, indicating noticeable daily return fluctuation.
- The max drawdown reaches -37%, meaning that in the worst case, investors could face a 37% drawdown of their capital.
-
This suggests investors need a certain level of risk tolerance, and in particular the patience to hold through unfavorable market conditions.
-
Risk-adjusted return
-
The strategy's information ratio (IR) is 0.60, indicating that the excess return the strategy generates for the risk taken is meaningful.
- Generally speaking, IR > 0.5 means the strategy has good risk-adjusted performance.
🎯 Investment Conclusion¶
Overall, this strategy can deliver stable annualized returns (about 11%) over the long term, with acceptable risk-adjusted performance. However, the max drawdown is large (-37%), so the investment process will involve significant drawdowns. Investors need to be mentally prepared and avoid panic stop-losses during drawdown periods.