API 参考

rqalpha-mod-tushare-datapatches 子包提供 40 个 rqdatac 函数的本地实现, 按职责拆分到 7 个子模块中。所有 patch 通过 apply_rqdatac_patch(bundle_path) 注册到 rqdatac 模块。

公共 API

apply_rqdatac_patch

apply_rqdatac_patch(bundle_path: str) -> None

patches.PATCH_MAP 中 40 个函数 setattrrqdatac 模块,并 patch 内部 _all_obid_to_type(rqfactor 的 ensure_order_book_ids 内部依赖)。

参数

说明

bundle_path

tushare-downloader 生成的 HDF5 bundle 根目录(包含 instruments.pk

副作用:首次调用会加载 instruments.pk(~23k 个工具)到内存。

PATCH_MAP

dict[str, Callable] — 40 个 rqdatac 函数名 → 补丁实现的映射。可用于直接内省或 第三方扩展。

from rqalpha_mod_tushare_data.patches import PATCH_MAP
print(len(PATCH_MAP))  # 40
print(list(PATCH_MAP.keys())[:5])
# ['all_instruments', 'instruments', 'get_trading_dates', ...]

按模块分组的 40 个 patch

instruments — 工具查询

PATCH_MAP 名

rqdatac 签名

说明

all_instruments

all_instruments(type=None, date=None, market='cn')

全部工具,返回 pd.DataFrame;支持按 type / date 过滤;market!='cn' 返回空 DataFrame

instruments

instruments(order_book_ids, market='cn')

按 order_book_id / symbol 查单个或多个工具;字符串入参返回官方 Instrument 对象,列表入参返回 list[Instrument]

calendar — 交易日历

PATCH_MAP 名

rqdatac 签名

说明

get_trading_dates

get_trading_dates(start_date, end_date, market='cn')

返回 list[datetime.date]

get_previous_trading_date

get_previous_trading_date(dt, n=1, market=None)

返回 datetime.date pd.Timestamp

get_next_trading_date

get_next_trading_date(dt, n=1, market=None)

返回 datetime.date

is_trading_date

is_trading_date(dt)

返回 bool

price — 行情 / 复权 / 流通股本

PATCH_MAP 名

rqdatac 签名

数据来源

get_price

get_price(order_book_ids, start_date=None, end_date=None, frequency='1d', fields=None, adjust_type='pre', skip_suspended=False, expect_df=True, time_slice=None, market='cn', **kwargs)

stocks.h5 / indexes.h5 / funds.h5

is_suspended

is_suspended(order_book_ids, start_date=None, end_date=None, market='cn')

stocks.h5(返回日期 × 标的布尔宽表)

is_st_stock

is_st_stock(order_book_ids, start_date=None, end_date=None, market='cn')

st_stock_days.h5(返回日期 × 标的布尔宽表)

get_turnover_rate

get_turnover_rate(order_book_ids, start_date=None, end_date=None, fields=None, expect_df=True, market='cn')

stocks.h5(行索引为 (order_book_id, tradedate)

get_ex_factor

get_ex_factor(order_book_ids, start_date=None, end_date=None, market='cn')

ex_cum_factor.h5

get_dividend

get_dividend(order_book_ids, start_date=None, end_date=None, adjusted=False, expect_df=True, market='cn')

dividends.h5

get_split

get_split(order_book_ids, start_date=None, end_date=None, market='cn')

split_factor.h5

get_shares

get_shares(order_book_ids, start_date=None, end_date=None, fields=None, expect_df=True, market='cn')

stocks.h5;字段使用官方 total / circulation_a / free_circulation

get_price_change_rate

get_price_change_rate(order_book_ids, start_date=None, end_date=None, expect_df=True, market='cn')

stocks.h5(返回日期 × 标的宽表)

get_market_cap

get_market_cap(order_book_ids, start_date, end_date, fields=None, expect_df=True)

stocks.h5

get_price(..., expect_df=True) 与官方 rqdatac 一致:返回普通字段列的 DataFrame, 行索引为 MultiIndex(order_book_id, date)。无数据时返回 None

financial — 财务三表 + 财务指标

未来函数保护:所有函数按 ann_date(公告日)过滤,避免回测用到未披露财报。

PATCH_MAP 名

rqdatac 签名

数据来源

income

income(order_book_ids, start_date, end_date, fields=None, expect_df=True)

income.h5

balance_sheet

balance_sheet(order_book_ids, start_date, end_date, fields=None, expect_df=True)

balance_sheet.h5

cash_flow

cash_flow(order_book_ids, start_date, end_date, fields=None, expect_df=True)

cash_flow.h5

fina_indicator

fina_indicator(order_book_ids, start_date=None, end_date=None, fields=None, expect_df=True)

fina_indicator.h5(按 ann_date 过滤)

industry — 行业分类 + 指数成分

未来函数保护:行业分类按 in_date / out_date 切片,跨期截面查询时返回 历史当时所属行业,非当前行业。

PATCH_MAP 名

rqdatac 签名

数据来源

get_industry

get_industry(industry, source='citics_2019', date=None, market='cn')

行业名称/代码 → list[str] 成分股;来自 industry_history.pk / instruments.pk

get_industry_raw

get_industry_raw(order_book_ids, level=1, source='SW2021')

本地扩展:标的 → 行业名称 list

get_industry_mapping

get_industry_mapping(source='citics_2019', date=None, market='cn', **kwargs)

返回 first_industry_code / first_industry_name 映射

industry

industry(code, market='cn')

行业名称/代码 → list[str] 成分股

get_instrument_industry

get_instrument_industry(order_book_ids, source='citics_2019', level=1, date=None, market='cn')

标的 → 行业 DataFrame,索引为 order_book_id

get_industry_sw

get_industry_sw(order_book_ids, date=None, level=1)

SW2021 别名

get_industry_sw_raw

get_industry_sw_raw(order_book_ids, level=1)

SW2021 raw 别名

get_industry_sw_mapping

get_industry_sw_mapping(level=1)

SW2021 mapping 别名

index_components

index_components(order_book_id, date=None, start_date=None, end_date=None, return_create_tm=False, market='cn')

index_weights.h5;单日返回 list,日期范围返回 dict

index_weights

index_weights(order_book_id, date=None, start_date=None, end_date=None, market='cn')

index_weights.h5;权重转换为官方小数比例,单日返回 Series,日期范围返回行 MultiIndex DataFrame

factor — 因子暴露

未来函数保护get_style_factor_exposureann_date <= dt(不是 report_date) 判断"哪期财报可用",避免公告前 look-ahead。

PATCH_MAP 名

rqdatac 签名

数据来源

get_factor

get_factor(order_book_ids, factor, start_date=None, end_date=None, universe=None, expect_df=True, market='cn', **kwargs)

stocks.h5universe 参数当前未使用,已知限制)

get_style_factor_exposure

get_style_factor_exposure(order_book_ids, start_date, end_date, factors=None, model='v1', industry_mapping='sws_2021', market='cn')

fina_indicator.h5 + stocks.h5model / industry_mapping / market 参数当前未使用,已知限制)

stubs — 无本地数据时的官方格式兼容

以下 API 中部分没有对应 bundle 数据。没有数据时仍返回与官方一致的空结构, 避免仅因 Python 类型或索引结构不同导致调用方崩溃;空结果不代表具备真实数据。

PATCH_MAP 名

rqdatac 签名

返回

fundamentals

fundamentals(query, entry_date=None)

pd.DataFrame()

get_share_transformation

get_share_transformation(predecessor=None, market='cn')

DataFrame,包含官方 8 个字段

current_snapshot

current_snapshot(order_book_ids, market='cn')

用最新日线构造官方 TickObject / list[TickObject]

get_dominant_future

get_dominant_future(underlying_symbol, start_date=None, end_date=None, rule=0, rank=1, market='cn')

Series(bundle 无期货主力数据)

get_stock_connect

get_stock_connect(order_book_ids, start_date=None, end_date=None, fields=None, expect_df=True)

DataFrame,行索引 (order_book_id, trading_date)

get_securities_margin

get_securities_margin(order_book_ids, start_date=None, end_date=None, fields=None, expect_df=True, market='cn')

DataFrame,行索引 (order_book_id, date)

concept

concept(*concept_names, date=None)

list[str]

id_convert

id_convert(order_book_ids, to=None)

to=None/'rq' 时原样返回;to='normal'XSHE→SZ / XSHG→SH

Mod 公共 API

rqalpha_mod_tushare_data.__init__ 暴露给 RQAlpha mod 系统的标准接口:

from rqalpha_mod_tushare_data import load_mod
mod = load_mod()  # TushareBundleMod(AbstractMod) 实例

名称

说明

__mod_name__

"tushare-data"(mod 注册名)

__version__

当前版本号

__config__

默认配置:{"enabled": True, "priority": 10}

load_mod()

工厂函数,返回 TushareBundleMod 新实例

TushareBundleMod

AbstractMod 子类,实现 start_up(env, mod_config) / tear_down(code, exception=None)

start_up 完成两件事:

  1. env.set_data_source(TushareBundleDataSource(bundle_path=mod_config.get("bundle_path", ...)))

  2. apply_rqdatac_patch(env.data_source.bundle_path),注册 40 个 patch