Skip to main content

rqalpha-futu-datasource

由于RQAlpha回测框架提供的数据,需要付费(还挺贵),并且只有A股的数据。 因此为RQAlpha框架提供自定义的Futu(富途)数据源,同时支持A股、港股、美股数据。用于基于rqalpha框架的股票和场内基金(ETF/LOF)回测。

注意:本项目依赖的rqalpha框架是fork自官方的版本rqalpha-plus,以同时支持A股、港股、美股回测交易。在编写回测代码时,原本依赖的rqalpha框架,需要替换为rqalpha-plus。

用法

本地开发

推荐使用uv工具管理项目依赖。执行以下命令安装项目依赖:

make sync

后续即可做开发和测试。

安装

  • pip install rqalpha-futu-datasource 或 uv add rqalpha-futu-datasource(uv管理依赖)

下载富途原始数据到本地

  • 启动本地 OpenD(默认 127.0.0.1:11111)
  • 运行下载脚本,将数据保存为 CSV:
    • python -m rqalpha_futu_datasource.download --data-dir data --codes 000001.XSHE,600000.XSHG,00700.XHKG,AAPL.XNAS --periods 1m,3m,5m,1d,1w --start 2024-01-01 --end 2024-12-31
    • 或使用代码文件:python -m rqalpha_futu_datasource.download --data-dir data --code-file ./codes.txt
  • 目录结构:data/<MARKET>/<ORDER_BOOK_ID>/<period>.csv,例如:data/CN/000001.XSHE/1d.csv, data/HK/00700.XHKG/1d.csv, data/US/AAPL.XNAS/1d.csv
  • 可通过环境变量指定目录:set FUTU_DATA_PATH=...(Windows)

注意:

  • 下载富途原始数据时,也可以指定富途OpenD的地址和端口号,如"--host 192.168.0.2 --port 22222"
  • --codes 只支持 RQAlpha 的股票代码格式(如 000001.XSHE、600000.XSHG、00700.XHKG、AAPL.XNAS)。
  • 同样,--code-file 参数指定的文件中,每个股票代码占一行,也必须是 RQAlpha 格式。

在 RQAlpha 中启用 Futu 数据源

通过扩展模块替换默认DataSource。参考:test_rqalpha.py, mod_futu_ds.py

1. 基础配置

from rqalpha import run_func


def test_run_with_futu_datasource():
    config = {
        "base": {
            "start_date": "2024-11-01",
            "end_date": "2024-11-06",
            "accounts": {"stock": 100000},
            "frequency": "1m",
            "data_bundle_path": os.path.abspath("tests/data"),
        },
        "extra": {
            "log_level": "info",
        },
        "mod": {
            "futu_ds": {
                "enabled": True,
                "lib": "rqalpha_futu_datasource.mod_futu_ds",
            }
        },
    }
    run_func(init=init, handle_bar=handle_bar, config=config)

2. 配置港股每手(Board Lot)

为避免网络依赖,港股每手数量支持以本地方式配置。如果不配置港股每手映射表,默认每手为100。支持以下两种方式(优先使用内存字典):

  • 通过 CSV 文件路径:配置 hk_lot_map_path。CSV 需包含 code 与 lot_size(或 lot/board_lot)列。
  • 通过内存字典:配置 hk_lot_map。

配置示例:

config = {
    "mod": {
        "futu_ds": {
            "enabled": True,
            "lib": "rqalpha_futu_datasource.mod_futu_ds",
            # 方式一:指定 CSV 文件路径
            "hk_lot_map_path": os.path.abspath("tests/data/hk_lot_map.csv"),
            # 方式二:直接传入字典 (优先级更高)
            "hk_lot_map": {"00700": 200, "00005": 500},
        }
    },
}

CSV 文件示例:

code,lot_size
00700,200
00005,500

3. 配置交易日历范围(Markets)

rqalpha-futu-datasource 需要知道在哪个市场中查找交易日历数据,可用数据时间范围以及加载那个市场的Instrument信息等,这可以通过 rqalpha 的 base 配置中的 market 参数来确定:

config = {
    "base": {
        "market": "cn",  # 可选值: "cn" (默认), "hk", "us" (不区分大小写), 也可同时指定多个市场, 如: ["cn", "hk", "us"]
        # ... 其他 base 配置
    },
    "mod": {
        "futu_ds": {
            "enabled": True,
            # ... 其他配置
        }
    }
}
  • cn: 对应 A 股市场(包含 SH 和 SZ)。
  • hk: 对应港股市场(HK)。
  • us: 对应美股市场(US)。

如果未配置 base.market,默认使用 cn。

指定富途数据存储目录有三种方式

  1. 在RQAlpha的模块配置中指定:

    config = {
       "mod": {
             "futu_ds": {
                 "enabled": True,
                 "lib": "rqalpha_futu_datasource.mod_futu_ds",
                 "futu_data_path": "path/to/futu/data",
             }
         },
    }
    
  2. 在RQAlpha base配置中指定:

    config = {
        "base": {
            "data_bundle_path": "path/to/futu/data",
        },
    }
    
  3. 在环境变量中指定:

    export FUTU_DATA_PATH=path/to/futu/data
    

优先级按此顺序:模块配置 > base配置 > 环境变量。

限制及说明

  • RQAlpha与富途股票代码之间的对应关系是:
    • A股:000001.XSHE -> SZ.000001 (深圳交易所)
    • A股:603728.XSHG -> SH.603728 (上海交易所)
    • 港股:00700.XHKG -> HK.00700 (香港交易所)
    • 美股:AAPL.XNAS -> US.AAPL (纳斯达克交易所)
    • 美股:TSM.XNYS -> US.TSM (纽约交易所)
  • 限制:
    • 仅支持股票和场内基金(包含A股和港股的ETF/LOF,美股ETF按股票处理),不支持期货、期权等其他品种。
    • 原始从富途下载的数据就只能是前复权数据。不支持其他复权方式。
    • 数据不能增量更新,有新数据需求只能重新下载全量数据(富途的OpenD进程会本地缓存数据,所以不用太担心此问题)。
    • 回测频率支持周期 1m、1d(RQAlpha框架限制),在handle_bar回调函数中,可以查询的历史数据周期为:1m, 3m, 5m, 1d, 1w(RQAlpha框架限制,无法支持到月级别)。

Metadata

Release files for rqalpha-futu-datasource 3.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rqalpha-futu-datasource 3.0.2
File Size Uploaded Provenance Actions
rqalpha_futu_datasource-3.0.2.tar.gz 3.1 MB With provenance Details for rqalpha_futu_datasource-3.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rqalpha-futu-datasource 3.0.2
File Interpreter ABI Platform Actions
rqalpha_futu_datasource-3.0.2-py3-none-any.whl Python 3 none any Details for rqalpha_futu_datasource-3.0.2-py3-none-any.whl

Total release size: 3.1 MB

Release files / rqalpha_futu_datasource-3.0.2.tar.gz

Download URL rqalpha_futu_datasource-3.0.2.tar.gz
Size 3.1 MB
Tags Source
SHA-256 checksum
How to use checksums
2511197b21341010b5c96514ee7f4662715288d0b84b1c385d457585d6212753
BLAKE2b-256 checksum
How to use checksums
8a432cfde8ecec6bbfb251e7528eaa340bd57ab27a538238fa0e5268aab2f623
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 30, 2026.

Transparency log

Release files / rqalpha_futu_datasource-3.0.2-py3-none-any.whl

Download URL rqalpha_futu_datasource-3.0.2-py3-none-any.whl
Size 24.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dbbb3ba323d74f178c93a30e40ea857d657c0d585a30f70a9c2732c7ccd96df2
BLAKE2b-256 checksum
How to use checksums
af5c04bf31d50f334fe5d84e7eb3f85ae778c8c6160f3f021b4117f860dc9e39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 30, 2026.

Transparency log

Security for rqalpha-futu-datasource 3.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for rqalpha_futu_datasource-3.0.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page