# QLib｜开源AI量化投资平台｜外汇股票API

随着 AI 技术的不断发展，其应用领域得到进一步扩展。同时，基于外汇API、贵金属API、股票API等量化投资凭借其科学、系统的决策方式，在金融领域中扮演着愈发重要的角色。QLib，作为一个专注于量化投资研究的开源项目，就利用了 AI 技术，结合iTick的免费外汇API、股票API数据接口，为广大研究者和从业人员提供了强大的量化投资工具支持。

<figure><img src="/files/0yLEgPMr6LBdsad3CCdT" alt=""><figcaption></figcaption></figure>

## QLib 简介

QLib 是一个由微软开源的量化投资研究平台，其代码仓库地址为 <https://github.com/microsoft/qlib> 。它旨在为量化投资领域的研究者和从业者提供一个高效、便捷的工具，帮助他们构建完整的量化研究工作流程，快速验证自己的投资想法。

QLib 的主要目的，是利用人工智能技术在量化投资领域创造价值，并赋能研究，涵盖从投资思路探索到产品实施的全过程。Qlib 支持多种机器学习建模范式，包括监督学习、市场动态建模和强化学习。QLib 包含了数据处理、模型训练、和回测的完整机器学习流程，并且覆盖了量化投资的整个链条，包括：阿尔法收益寻找、风险建模、投资组合优化，和交易执行。

## iTick 简介

iTick 是一家为金融科技公司和开发者提供可靠数据源 APIs 的数据代理机构。其官网地址：<https://itick.org> 。提供外汇 API、股票 API、加密货币 API、指数 API 等，能满足金融科技公司和开发者在不同金融领域的数据需求，帮助构建创新的交易和分析工具。目前为个人量化开发者提供免费套餐，基本可以满足其日常使用需求，降低了个人开发者的开发成本和数据获取门槛。可以直接进入官网文档了解，<https://doc.itick.org>

## 使用

### 请求K线数据

`python -m pip install requests`

```
"""
**iTick**：是一家数据代理机构，为金融科技公司和开发者提供可靠的数据源APIs，涵盖外汇API、股票API、加密货币API、指数API等，帮助构建创新的交易和分析工具，目前有免费的套餐可以使用基本可以满足个人量化开发者需求
https://github.com/itick-org
https://itick.org
"""
​
import requests
​
url = "https://api.itick.org/stock/kline?region=hk&code=700&kType=1"
​
headers = {
    "accept": "application/json",
    "token": "bb42e24746784dc0af821abdd1188861d945a07051c8414a8337697a752de1eb"
}
​
response = requests.get(url, headers=headers)
​
print(response.text)
```

### QLib 使用

QLib 要求 Python 3.8+，并建议使用 Conda 进行环境管理。使用 pip 安装 pyqlib 库：

```
pip install pyqlib
pip install numpy
pip install --upgrade cython
​
git clone https://github.com/microsoft/qlib.git && cd qlib
pip install .  # `pip install -e .[dev]` is recommended for development. check details in docs/developer/code_standard_and_dev_guide.rst
```

安装完成后，可以使用获取的数据来开始进行使用，运行以下命令：

```
# get 1d data
python -m qlib.run.get_data qlib_data --target_dir ~/.qlib/qlib_data/cn_data --region cn
​
# get 1min data
python -m qlib.run.get_data qlib_data --target_dir ~/.qlib/qlib_data/cn_data_1min --region cn --interval 1min
```

数据准备好后，可以运行 Quant Research Workflow。使用以下命令：

```
 cd examples  # Avoid running program under the directory contains `qlib`
 qrun benchmarks/LightGBM/workflow_config_lightgbm_Alpha158.yaml
```

运行后，能够得到以下输出，这是对投资数据的一个初步分析结果：

```
'The following are analysis results of the excess return without cost.'
                       risk
mean               0.000708
std                0.005626
annualized_return  0.178316
information_ratio  1.996555
max_drawdown      -0.081806
'The following are analysis results of the excess return with cost.'
                       risk
mean               0.000512
std                0.005626
annualized_return  0.128982
information_ratio  1.444287
max_drawdown      -0.091078
```

## 总结

QLib 作为一个强大的量化投资研究平台，为量化投资领域的研究者和从业者提供了丰富的功能和便捷的工具。它的灵活性和易用性，使得用户可以快速构建量化研究工作流程，验证自己的投资想法。

QLib 可以广泛应用于股票、期货、基金等金融领域的量化投资研究，帮助投资者进行风险控制、资产配置、投资决策等。同时，随着 AI 大模型技术的不断演进，QLib 将能提供越来越强的量化投资分析和研究能力。\
\
原始文章：<https://www.itick.org/blog/quant-tools/qlib-itick-forex-stock-integration>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.itick.org/ai-quantization-tutorial/qlib-kai-yuan-ai-liang-hua-tou-zi-ping-tai-wai-hui-gu-piao-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
