7709-代码表¶
作用¶
分页或全量查询证券代码表,返回代码、名称、市场、价格精度、昨收,以及本地派生的品种分类和板块分类。
| 项目 | 内容 |
|---|---|
| 推荐 · 市场全量 | client.codes.all(market, ...) |
| 常用 · 沪深北 A 股 | client.codes.all_a_shares() |
| 手动分页 | client.codes.list(market, ...) |
| 底层接口 | 0x044d |
| 返回模型 | list[SecurityCode] |
示例¶
按常见需求直接取:
from eltdx import TdxClient
with TdxClient(timeout=3) as client:
sh_codes = client.codes.all("sh") # 上海市场全部代码对象
sh_a_shares = client.codes.a_shares("sh")
sh_etfs = client.codes.etfs("sh")
sh_indices = client.codes.indices("sh")
all_a_share_codes = client.codes.all_a_shares() # 沪深北 A 股代码字符串
all_etf_codes = client.codes.all_etfs()
all_index_codes = client.codes.all_indices()
all()、a_shares()、etfs() 和 indices() 返回 SecurityCode 对象,包含名称和分类等字段;all_a_shares() 等跨市场便捷方法返回带市场前缀的代码字符串。
真实返回样本¶
Python 实际返回 list[SecurityCode]。下面是 to_jsonable(sh_codes) 中一条 A 股记录的常用字段节选;分类和板块字段由 SDK 按代码规则派生。
真实返回 JSON · SecurityCode(1 条节选)
真实采样;代码表会返回大量记录,这里只展示平安银行一条。
{
"exchange": "sz",
"market_id": 0,
"code": "000001",
"name": "平安银行",
"multiple": 100,
"decimal": 2,
"previous_close_price": 11.109999656677246,
"volume_ratio_base": 3144.746337890625,
"category": "a_share",
"category_reason": "SZSE A-share code prefix",
"board": "szse_main_board",
"board_reason": "SZSE main-board prefix"
}
只有需要自己控制页大小和起始位置时,才使用 list():
from eltdx import TdxClient
with TdxClient(timeout=3) as client:
first_page = client.codes.list("sz", start=0, limit=1600)
按市场 + 品种 / 板块过滤:
from eltdx import TdxClient
with TdxClient(timeout=3) as client:
all_codes = client.codes.all_markets()
sh_a_share = [item for item in all_codes if item.exchange == "sh" and item.category == "a_share"]
sz_a_share = [item for item in all_codes if item.exchange == "sz" and item.category == "a_share"]
bj_a_share = [item for item in all_codes if item.exchange == "bj" and item.category == "a_share"]
sse_main = [item for item in all_codes if item.board == "sse_main_board"]
sse_star = [item for item in all_codes if item.board == "sse_star_market"]
szse_main = [item for item in all_codes if item.board == "szse_main_board"]
szse_chinext = [item for item in all_codes if item.board == "szse_chinext"]
bse_stock = [item for item in all_codes if item.board == "bse_listed_stock"]
etfs = [item for item in all_codes if item.category == "etf"]
indexes = [item for item in all_codes if item.category == "index"]
参数¶
| 参数 | 含义 |
|---|---|
market |
市场,日常推荐传 sz、sh、bj |
start |
分页起始位置,从 0 开始 |
limit |
本页最多取多少条,服务端上限 1600 |
page_size |
codes.all() 内部分页条数,默认 1600 |
市场参数¶
日常调用推荐传字符串,数字主要用于兼容旧代码或理解底层协议。
| 推荐写法 | 含义 | 底层 market_id |
|---|---|---|
sz |
深市 | 0 |
sh |
沪市 | 1 |
bj |
北交所 | 2 |
解析字段¶
SecurityCode 字段 |
含义 |
|---|---|
exchange / market_id |
市场前缀和市场编号 |
code / full_code |
六位代码 / 完整代码 |
name |
证券名称 |
multiple |
交易单位,股票常见为 100 |
decimal |
价格展示小数位 |
previous_close_price |
代码表里的昨收参考价 |
volume_ratio_base |
量比计算基准量 |
unknown0_raw |
量比基准量原始 4 字节 |
previous_close_raw |
昨收价原始 4 字节 |
unknown3_raw |
未拆细附加原始字段 |
category |
本地派生品种分类,如 a_share、b_share、etf、index |
category_reason |
分类规则说明 |
board |
本地派生板块,如主板、创业板、科创板、北交所 |
board_reason |
板块规则说明 |
category 和 board 是 SDK 根据 full_code 的交易所和代码段规则派生出来的字段。服务器原始记录里直接给的是代码、名称、交易单位、小数位、昨收、量比基准量和保留字段。
返回类型¶
| 调用 | 返回 |
|---|---|
client.codes.list("sz") |
list[SecurityCode],某一页代码对象 |
client.codes.all("sz") |
list[SecurityCode],某市场全量代码对象 |
client.codes.a_shares("sz") |
list[SecurityCode],某市场 A 股代码对象 |
client.codes.etfs("sz") |
list[SecurityCode],某市场 ETF 代码对象 |
client.codes.indices("sz") |
list[SecurityCode],某市场指数代码对象 |
client.codes.all_markets() |
list[SecurityCode],沪深北全量代码对象 |
client.codes.all_a_shares() |
list[str],完整 A 股代码字符串 |
client.codes.all_stocks() |
list[str],A 股 + B 股完整代码字符串 |
client.codes.all_etfs() |
list[str],ETF 完整代码字符串 |
client.codes.all_indices() |
list[str],指数完整代码字符串 |
市场取法¶
| 想取什么 | 调用 |
|---|---|
| 深市代码表 | client.codes.all("sz") |
| 沪市代码表 | client.codes.all("sh") |
| 北交所代码表 | client.codes.all("bj") |
| 深市分页 | client.codes.list("sz", start=0, limit=1600) |
| 沪市分页 | client.codes.list("sh", start=0, limit=1600) |
| 北交所分页 | client.codes.list("bj", start=0, limit=1600) |
client.codes.all(market) 每次按协议分页拉取全量代码表,不需要先调用 client.codes.count(market)。
分页规则¶
client.codes.all(market) 内部会自动按页循环:
| 步骤 | 说明 |
|---|---|
| 第一页 | start=0 |
| 下一页 | start += 当前页实际返回数量 |
| 停止条件 | 主站返回空页 |
| 默认页大小 | 1600 |
短页不代表数据已经结束;SDK 会按本页实际返回数量推进 start,继续查询到空页。如果只想自己控制分页,用 client.codes.list(market, start=..., limit=...)。
派生分类值¶
category 是本地根据代码前缀派生出来的品种分类。
category |
含义 | 规则 |
|---|---|---|
a_share |
A 股 / 北交所上市股票 | sh600/601/603/605/688/689、sz000/001/002/003/004/30、bj92 |
b_share |
B 股 | sh900、sz200 到 sz209 |
etf |
ETF / 场内基金口径 | sh510/511/512/513/515/516/517/518/520/560/561/562/563/588、sz158/159 |
index |
指数 | sh000/880/881/999、sz399、bj899 |
private_convertible_bond |
北交所私募可转债口径 | bj810 |
bond |
北交所债券样本口径 | bj821 |
unknown |
未命中当前内置规则 | 其他前缀 |
当前 SecurityCode.category 覆盖常用大类。LOF、REITs、可转债、回购、优先股等更细分品种,可以继续按代码段扩展;unknown3_raw 保留为原始附加字段。
board 是本地根据 A 股代码前缀派生出来的板块分类。非 A 股通常是 none。
board |
含义 | 规则 |
|---|---|---|
sse_main_board |
上交所主板 | sh600/601/603/605 |
sse_star_market |
科创板 | sh688/689 |
szse_main_board |
深交所主板 | sz000/001/002/003/004 |
szse_chinext |
创业板 | sz30xxxx |
bse_listed_stock |
北交所上市股票 | bj92 |
none |
不适用或未命中 | 非 A 股、未知前缀 |
常见过滤写法¶
| 想取什么 | 写法 |
|---|---|
| 全部 A 股 | [item for item in all_codes if item.category == "a_share"] |
| 沪市 A 股 | [item for item in all_codes if item.exchange == "sh" and item.category == "a_share"] |
| 深市 A 股 | [item for item in all_codes if item.exchange == "sz" and item.category == "a_share"] |
| 北交所股票 | [item for item in all_codes if item.board == "bse_listed_stock"] |
| 上交所主板 | [item for item in all_codes if item.board == "sse_main_board"] |
| 科创板 | [item for item in all_codes if item.board == "sse_star_market"] |
| 深交所主板 | [item for item in all_codes if item.board == "szse_main_board"] |
| 创业板 | [item for item in all_codes if item.board == "szse_chinext"] |
| ETF | [item for item in all_codes if item.category == "etf"] |
| 指数 | [item for item in all_codes if item.category == "index"] |
| B 股 | [item for item in all_codes if item.category == "b_share"] |
如果只要完整代码字符串,可以再取 full_code:
便捷过滤¶
| 方法 | 返回 |
|---|---|
client.codes.all_stocks() |
A 股 + B 股完整代码列表 |
client.codes.all_a_shares() |
A 股完整代码列表 |
client.codes.all_etfs() |
ETF 完整代码列表 |
client.codes.all_indices() |
指数完整代码列表 |
client.codes.stock_count(market) |
某市场股票数量 |
client.codes.a_share_count(market) |
某市场 A 股数量 |
这些便捷方法覆盖常用大类。主板、创业板、科创板、北交所、B 股等更细分的列表,直接按上面的 board / category 字段过滤即可。