合约信息#
先用 all_instruments 确定研究范围,再用 instruments 解析具体代码。
order_book_id 是代码,name 是名称。目录里有四种类型——股票(stock)、指数(index)、行业(industry)、主题(theme),type 选类型,source 是编号机构:股票是交易所,指数是发布机构,行业是分类体系(申万 SW;GICS、ICB、NAICS、SIC),主题是主题目录(THS)。历史查询用 as_of 指定时点;股票、指数与申万行业有历史,美国行业分类与主题目前只有当前快照。
函数 / 类 |
解决的问题 |
|---|---|
按类型(股票、指数、行业、主题)、市场、编号机构或历史时点筛选证券 |
|
按代码查询证券,一个列表可混合四种类型与中美市场 |
all_instruments — 按类型(股票、指数、行业、主题)、市场、编号机构或历史时点筛选证券#
- libfinance.all_instruments(type=None, market=None, source=None, as_of=None, cached=True)[源代码]#
获取证券目录:全部类型,或指定类型、市场、编号机构的证券。
- 参数
type –
"stock"/"index"/"industry"/"theme",或它们的列表;省略为全部类型。market – 市场,如
"cn"/"us";省略为全部市场。source – 编号机构,如
"XSHG"、"CSI"、"SW",或它们的列表;省略为全部。as_of – 给出则为该日的历史视图;省略时各类型取各自的当前状态。某类型不覆盖的日期 报 CoverageError 并写明类型,缩小
type或source即可。cached – 是否使用按服务端数据版本更新的缓存;显式指定市场时直接查询。
- 返回
DataFrame,列为
order_book_id, type, market, name, exchange, source。
示例
1. 目录全貌:按类型与市场计数
catalog = lf.all_instruments()
print(catalog.groupby(["type", "market"]).size())
type market
index CN 1004
US 8
industry CN 608
US 2386
stock CN 5572
US 5383
theme CN 375
dtype: int64
count_by(lf::all_instruments(), "type", "market");
index CN 1004
index US 8
industry CN 608
industry US 2386
stock CN 5572
stock US 5383
theme CN 375
2. A 股股票:source 是上市交易所
print(lf.all_instruments(type="stock", market="cn").head())
order_book_id type market name exchange source
0 688669.XSHG stock CN 聚石化学 XSHG XSHG
1 603052.XSHG stock CN 可川科技 XSHG XSHG
2 600298.XSHG stock CN 安琪酵母 XSHG XSHG
3 601007.XSHG stock CN 金陵饭店 XSHG XSHG
4 688339.XSHG stock CN 亿华通 XSHG XSHG
show(lf::all_instruments("stock", "cn"));
order_book_id type market name exchange source
688669.XSHG stock CN 聚石化学 XSHG XSHG
603052.XSHG stock CN 可川科技 XSHG XSHG
600298.XSHG stock CN 安琪酵母 XSHG XSHG
601007.XSHG stock CN 金陵饭店 XSHG XSHG
688339.XSHG stock CN 亿华通 XSHG XSHG
...
[5572 rows x 6 columns]
3. 中证指数公司(CSI)发布的指数
print(lf.all_instruments(type="index", source="CSI").head())
order_book_id type market name exchange source
0 000987.XSHG index CN 全指材料 NaN CSI
1 000836.XSHG index CN 成长7030 NaN CSI
2 000977.XSHG index CN 内地低碳 NaN CSI
3 000834.XSHG index CN 价值7030 NaN CSI
4 399966.XSHE index CN 800证保 NaN CSI
show(lf::all_instruments("index", std::nullopt, "CSI"));
order_book_id type market name exchange source
000987.XSHG index CN 全指材料 NaN CSI
000836.XSHG index CN 成长7030 NaN CSI
000977.XSHG index CN 内地低碳 NaN CSI
000834.XSHG index CN 价值7030 NaN CSI
399966.XSHE index CN 800证保 NaN CSI
...
[178 rows x 6 columns]
4. 申万(SW)行业:source 是分类体系
print(lf.all_instruments(type="industry", source="SW").head())
order_book_id type market name exchange source
0 110000.SW industry CN 农林牧渔 NaN SW
1 110100.SW industry CN 种植业 NaN SW
2 110101.SW industry CN 种子 NaN SW
3 110102.SW industry CN 粮食种植 NaN SW
4 110103.SW industry CN 其他种植业 NaN SW
show(lf::all_instruments("industry", std::nullopt, "SW"));
order_book_id type market name exchange source
110000.SW industry CN 农林牧渔 NaN SW
110100.SW industry CN 种植业 NaN SW
110101.SW industry CN 种子 NaN SW
110102.SW industry CN 粮食种植 NaN SW
110103.SW industry CN 其他种植业 NaN SW
...
[608 rows x 6 columns]
5. THS 主题,仅 A 股
print(lf.all_instruments(type="theme").head())
order_book_id type market name exchange source
0 300008.THS theme CN 新能源汽车 NaN THS
1 300013.THS theme CN 大飞机 NaN THS
2 300018.THS theme CN 参股保险 NaN THS
3 300019.THS theme CN 网络游戏 NaN THS
4 300023.THS theme CN 食品安全 NaN THS
show(lf::all_instruments("theme"));
order_book_id type market name exchange source
300008.THS theme CN 新能源汽车 NaN THS
300013.THS theme CN 大飞机 NaN THS
300018.THS theme CN 参股保险 NaN THS
300019.THS theme CN 网络游戏 NaN THS
300023.THS theme CN 食品安全 NaN THS
...
[375 rows x 6 columns]
6. 一次取几种类型:type 给列表
both = lf.all_instruments(type=["index", "industry"], market="cn")
print(both.groupby(["type", "source"]).size())
type source
index CNI 110
CSI 178
SZSE 2
industry SW 608
dtype: int64
count_by(lf::all_instruments({"index", "industry"}, "cn"), "type", "source");
index CNI 110
index CSI 178
index SZSE 2
industry SW 608
7. 历史时点:申万 2021 年改版前后的行业数
before = lf.all_instruments(type="industry", source="SW", as_of="2020-01-02")
after = lf.all_instruments(type="industry", source="SW", as_of="2022-04-15")
print(len(before), len(after))
400 608
const auto before = lf::all_instruments("industry", std::nullopt, "SW", "2020-01-02");
const auto after = lf::all_instruments("industry", std::nullopt, "SW", "2022-04-15");
std::cout << before->num_rows() << " " << after->num_rows() << "\n";
400 608
输出由 docs/_shared/update_examples.py 运行示例脚本生成:数据版本 sha256:6feda8ca,2026-10-09。
下载示例脚本: 02_instrument.cpp · 02_instrument.py
结果解读:四种类型的返回形状相同,type、market、source 只改变范围;as_of 改变身份快照(申万 2021 年改版前 400 个行业节点,改版后 608 个)。
instruments — 按代码查询证券,一个列表可混合四种类型与中美市场#
- libfinance.instruments(order_book_ids, as_of=None, last_known=False)[源代码]#
按代码解析证券,类型由代码本身决定。
- 参数
order_book_ids – 单个代码或跨市场、跨类型的代码列表,如
"000001.XSHE"、"000300.XSHG"。as_of – 按该日有效的代码解析;省略时各类型取各自的当前状态。
last_known – 为 True 时,
as_of当日已终止上市的股票或指数代码按它最后一次的证券解析 (行业、主题节点没有上市可回退)。
- 返回
传入单个代码时返回一个
Instrument(查不到则返回None); 传入列表时按传入顺序返回Instrument列表,查不到的代码会被跳过。 两种类型同一天都认这个代码时报 AmbiguousInstrumentError。
示例
1. 单个代码:返回一个证券,查不到为 None
stock = lf.instruments("000001.XSHE")
print(stock.order_book_id, stock.name, stock.type, stock.market)
000001.XSHE 平安银行 stock CN
for (const auto& stock : lf::instruments("000001.XSHE"))
std::cout << stock.order_book_id() << " " << stock.name() << " " << stock.type() << " "
<< stock.market() << "\n";
000001.XSHE 平安银行 stock CN
2. 一个列表混合四种类型、两个市场:类型由代码决定
for item in lf.instruments(["000001.XSHE", "000300.XSHG", "480000.SW", "300008.THS", "AAPL.US", "45.GICS"]):
print(item.order_book_id, item.type, item.market, item.name)
000001.XSHE stock CN 平安银行
000300.XSHG index CN 沪深300
480000.SW industry CN 银行
300008.THS theme CN 新能源汽车
AAPL.US stock US Apple Inc. - Common Stock
45.GICS industry US Information Technology
for (const auto& item :
lf::instruments({"000001.XSHE", "000300.XSHG", "480000.SW", "300008.THS", "AAPL.US", "45.GICS"}))
std::cout << item.order_book_id() << " " << item.type() << " " << item.market() << " " << item.name() << "\n";
000001.XSHE stock CN 平安银行
000300.XSHG index CN 沪深300
480000.SW industry CN 银行
300008.THS theme CN 新能源汽车
AAPL.US stock US Apple Inc. - Common Stock
45.GICS industry US Information Technology
3. as_of:按当日有效的身份解析(Meta 在 2022 年仍叫 FB.US)
for item in lf.instruments(["FB.US", "000300.XSHG"], as_of="2022-04-15"):
print(item.order_book_id, item.name)
FB.US META PLATFORMS INC
000300.XSHG 沪深300
for (const auto& item : lf::instruments({"FB.US", "000300.XSHG"}, "2022-04-15"))
std::cout << item.order_book_id() << " " << item.name() << "\n";
FB.US META PLATFORMS INC
000300.XSHG 沪深300
4. last_known:已退市的代码按最后一次的身份解析
print(lf.instruments("ATVI.US"))
print(lf.instruments("ATVI.US", last_known=True).name)
None
ACTIVISION BLIZZARD INC
std::cout << lf::instruments("ATVI.US").size() << "\n";
std::cout << lf::instruments("ATVI.US", std::nullopt, true).front().name() << "\n";
0
ACTIVISION BLIZZARD INC
输出由 docs/_shared/update_examples.py 运行示例脚本生成:数据版本 sha256:6feda8ca,2026-10-09。
下载示例脚本: 02_instrument.cpp · 02_instrument.py
结果解读:类型由代码决定,字符串输入得到单个对象,列表输入按输入顺序返回对象列表。历史查询展示当时的代码和身份;早于某类型发布的 as_of 报 CoverageError。
Instrument — 合约对象#
- class libfinance.api.instrument.Instrument(d)[源代码]#
一只证券。属性名就是
all_instruments()的列名。