QLib学习

开源地址:GitHub - microsoft/qlib: Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL.Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL. - microsoft/qlibhttps://github.com/microsoft/qlib

使用conda创建一个新的环境,python3.8

conda create --name quantenv python=3.8

采用源码安装的方式进行安装,在从源代码安装 Qlib 之前,用户需要安装一些依赖项:

pip install numpy
pip install --upgrade  cython
git clone https://github.com/microsoft/qlib.git && cd qlib
pip install .  # 推荐使用 `pip install -e .[dev]` 用于开发。详情参阅文档的 developer/code_standard_and_dev_guide.rst

注意:您也可以使用 python setup.py install 来安装 Qlib。但这不是推荐的方法,它将跳过 pip,可能导致模糊的问题。例如,只有命令 pip install . 才能覆盖由 pip install pyqlib 安装的稳定版本,而命令 python setup.py install 则无法做到。

在win11下安装报错:error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

需要Microsoft Visual C++14.0或更高版本,获取Microsoft C++ Build Tools。报错原因是pip所安装的包需要使用C++编译后才能够正常安装,但是当前安装环境中缺少完整的C++编译环境,因此安装失败。

按照输出的信息,我们可以下载安装"Microsoft C++ Build Tools"这个工具,为了安装这个环境,直接安装一个visual Studio十几个G也是可以,他会自动帮你把所有需要的包安装好,就是太大了,很多不是必要的包也安装了。

安装完成后,重新编译,编译通过。

在导入包时, from qlib.data import D,报错

import _ssl             # if we can't import it, let the error propagate

在Anaconda3目录下搜索找到以下两个文件:

libcrypto-1_1-x64.dll
libssl-1_1-x64.dl

复制到运行环境的DLLs的目录下

 ModuleNotFoundError: No module named ‘qlib.data._libs.rolling’

If the error occurs when importing qlib package with PyCharm IDE, users can execute the following command in the project root folder to compile Cython files and generate executable files:

python setup.py build_ext --inplace

### Qlib 深度学习模型使用教程和案例 #### 1. 数据准备与预处理 为了有效利用Qlib进行深度学习模型的开发,首先需要准备好适当的数据格式。Qlib支持多种数据源输入方式,但对于高效能运算特别推荐使用其专有的`bin`文件格式[^4]。 ```python import qlib from qlib.constant import REG_CN provider_uri = "~/.qlib/qlib_data/cn_data" qlib.init(provider_uri=provider_uri, region=REG_CN) # 下载并转换CSV至BIN格式 from qlib.tests.data import GetData aa = GetData() aa.csv_data_cn(target_dir=provider_uri) ``` #### 2. 构建深度学习模型 Qlib不仅限于传统量化分析工具的功能,在深度学习领域同样表现出色。通过集成先进的神经网络架构,能够更精准捕捉市场动态变化特征。对于希望尝试构建自定义深度学习策略的研究者来说,可以从官方文档获取详细的API指南以及丰富的实例代码[^1]。 例如,创建一个简单的LSTM模型来进行股票价格预测: ```python from torch.nn import LSTM, Linear from torch.optim import Adam from qlib.contrib.model.pytorch_base import PyTorchBaseModel class SimpleLSTM(PyTorchBaseModel): def __init__(self, input_size=10, hidden_layer_size=100, output_size=1): super().__init__() self.lstm = LSTM(input_size=input_size, hidden_size=hidden_layer_size) self.linear = Linear(hidden_layer_size, output_size) def forward(self, x): lstm_out, _ = self.lstm(x.view(len(x), 1, -1)) predictions = self.linear(lstm_out.view(len(x), -1)) return predictions[-1] model = SimpleLSTM() optimizer = Adam(model.parameters(), lr=0.01) ``` #### 3. 训练与评估 一旦完成了模型的设计工作之后,则可以通过调用相应的方法完成训练流程,并借助内置函数轻松实现性能评测。值得注意的是,Qlib还引入了元控制器的概念,其中包含了用于增强基础预测效果的各种机制,比如MetaGuideModel类就是一个很好的例子[^3]。 ```python from qlib.workflow.record_temp import SignalRecord from qlib.backtest import Backtest as BkTest from datetime import timedelta train_start_date = "2018-01-01" train_end_date = "2020-01-01" bk_tester = BkTest( model=model, start_time=train_start_date, end_time=train_end_date, freq="day", account=1e6, benchmark="SH000905", # 中证500指数作为基准比较对象 signal_record=SignalRecord(), adjust_factor=None, max_loss=-timedelta(days=7), min_profit=timedelta(days=7), ) results = bk_tester.run_backtest() print(results.summary()) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值