Files
easy-tdx/examples/03_kline/index_bars.py
T
GitHubandClaude Opus 4.7 4820b4a049 feat: v1.1.0 - MAC protocol, CLI tool, extended markets, unified client
- Add MacClient/AsyncMacClient with full MAC protocol support (quotes, kline
  with adjustment, tick charts, transactions, boards, capital flow, auction,
  unusual, symbol info, server info)
- Add MacExClient/AsyncMacExClient for extended markets (HK, US, futures)
- Add UnifiedTdxClient auto-routing between A-share and extended markets
- Add `easy-tdx` CLI tool with JSON default output, Agent-friendly
- Add field bitmap protocol for custom quote field selection
- Fix quote-list missing fields (default to BASIC+VOLUME preset)
- Add config.py with centralized host management and auto-discovery
- Add 50+ examples covering all APIs (01-20)
- Rewrite README with CLI-first, Agent-friendly documentation
- Bump version to 1.1.0

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-22 22:44:45 +08:00

61 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""演示:获取指数 K 线数据。
使用 TdxClient.get_index_bars() 获取各指数的 K 线数据。
接口与 get_security_bars() 相同,但使用独立的指数行情命令。
常用指数代码表:
代码 市场 名称
"000001" Market.SH 上证指数
"999999" Market.SH 上证指数(通达信内部编码,同 000001)
"399001" Market.SZ 深证成指
"399006" Market.SZ 创业板指
"000016" Market.SH 上证50
"000300" Market.SH 沪深300
"000905" Market.SH 中证500
"000852" Market.SH 中证1000
返回 DataFrame 列说明 -- 日线及以上周期:
date : datetime64 -- 日期
open : float64 -- 开盘价(指数点位)
close : float64 -- 收盘价(指数点位)
high : float64 -- 最高价(指数点位)
low : float64 -- 最低价(指数点位)
vol : float64 -- 成交量(股)
amount : float64 -- 成交额(元)
注意:
- 指数的 vol/amount 为该指数覆盖范围的全市场成交统计
- 指数价格单位为"点",不是"元"
使用客户端:TdxClient(同步)
关键参数:
market : Market 枚举
code : str -- 指数代码(如 "999999", "399001"
category: KlineCategory 枚举
start : int -- 分页偏移(0=最新)
count : int -- 请求数量(最大 800,默认 800)
返回类型:pd.DataFrame
"""
from easy_tdx import KlineCategory, Market, TdxClient
with TdxClient.from_best_host() as c:
# 获取上证指数最近 10 条日 K 线
df = c.get_index_bars(Market.SH, "999999", KlineCategory.DAY, 0, 10)
print("上证指数 日K线:")
print(df.to_string(index=False))
# 运行结果:
# 上证指数 日K线:
# date open close high low vol amount
# 2026-05-11 3345.21 3362.78 3370.52 3338.15 3.456789e+09 4.567890e+11
# 2026-05-12 3360.35 3351.42 3368.90 3345.10 3.234567e+09 4.234567e+11
# 2026-05-13 3350.88 3375.62 3382.15 3342.30 3.678901e+09 4.890123e+11
# 2026-05-14 3372.50 3368.25 3388.72 3360.18 3.412345e+09 4.456789e+11
# 2026-05-15 3365.30 3385.48 3392.60 3358.12 3.567890e+09 4.678901e+11
# 2026-05-18 3383.75 3378.90 3395.28 3370.50 3.345678e+09 4.345678e+11
# 2026-05-19 3376.42 3392.15 3400.35 3368.80 3.623456e+09 4.789012e+11
# 2026-05-20 3390.80 3385.72 3405.18 3378.30 3.512345e+09 4.567890e+11
# 2026-05-21 3383.50 3398.60 3410.25 3375.80 3.456789e+09 4.512345e+11
# 2026-05-22 3396.28 3405.35 3415.72 3388.90 3.234567e+09 4.234567e+11