Skip to main content

实时行情采集服务,支持新浪和腾讯数据源

Project description

qdata-quote

实时 A 股行情采集服务,支持新浪和腾讯两个数据源。性能超越 easyquotation 约 8-10%。

安装

pip install qdata-quote

快速开始

from qdata_quote import QuoteService

# 默认加载全市场股票列表(初始化时自动从网络刷新)
service = QuoteService()

# 同步获取指定股票行情
df = service.get_real_sync(["000001", "600000"])
print(df)

# 同步获取全市场行情快照
df_all = service.get_all_sync()

# 异步获取(高性能路径)
import asyncio
df = asyncio.run(service.get_real(["000001", "600000"]))
df_all = asyncio.run(service.get_all())

自定义股票列表

from qdata_quote import QuoteService

# 传入自定义股票列表(不会自动刷新)
service = QuoteService(codes=["000001", "600000", "300750"])

# 随时替换股票列表(切换为自定义模式,不再接受 refresh)
service.set_stock_codes(["000001", "600000"])

# 刷新全市场股票列表(仅在使用默认列表时生效)
service.refresh_stock_codes()

获取股票代码

from qdata_quote import get_stock_codes

# 读取缓存(如果没有缓存则自动获取)
codes = get_stock_codes()

# 强制从网络刷新
codes = get_stock_codes(refresh=True)

缓存文件位置:~/.qdata_quote/stock_codes.json

数据源

支持三个数据源,通过 source 参数指定:

# 新浪源(默认)
df = service.get_real_sync(["000001"], source="sina")

# 腾讯源(字段更丰富)
df = service.get_real_sync(["000001"], source="tencent")

# 腾讯完整源(跨市场 + 最多字段)
df = service.get_real_sync(["000001"], source="tencent_full")
数据源 每批数量 字段数 市场支持 特点
sina 800 只/批 42 仅 A 股 速度快,基础字段齐全
tencent 60 只/批 42 仅 A 股 额外提供涨跌、市盈率、市值、量比等
tencent_full 800 只/批 66 A 股 + 港股 + 美股 + ETF + 北交所 最多字段(+24 扩展),跨市场支持

tencent_full 扩展字段

相比 sina/tencent,tencent_full 额外提供 24 列:

类别 字段
累计涨跌幅 change_ytd change_5d change_10d change_20d change_60d change_6m change_1y
52 周高低 high_52w low_52w
涨速 speed_5min speed_5min_real
外内盘 outer_volume inner_volume
股本 float_shares total_shares
中文化 stock_type_cn market_tags_cn
状态 currency is_suspend market_mark
其它 turnover_rate turnover_full capital_ratio secu_code

跨市场支持

tencent_full 支持 A 股 / 港股 / 美股 / ETF / 北交所:

# 跨市场查询(港美股字段较少,错位字段自动置 NaN)
df = service.get_real_sync(
    ["600519.SH", "000001.SZ", "00700.HK", "AAPL.US"],
    source="tencent_full"
)

返回格式

返回统一的 pandas.DataFrame,index 为带后缀的股票代码(如 600000.SH000001.SZ830789.BJ)。

股票代码格式

  • 输出统一后缀格式<代码>.<交易所>,交易所后缀大写 —— .SH(上交所)、.SZ(深交所)、.BJ(北交所)、.HK(港股)、.US(美股)。
  • 输入兼容多种写法(tencent_full 独有跨市场支持):
    • 纯代码:00000160051900700AAPL
    • 带后缀:000001.SZ00700.HKAAPL.US(大小写不敏感)
    • 带前缀:sh600519hk00700usAAPL
  • get_stock_codes() 返回的默认列表同样为后缀格式(仅 A 股)。
  • 跨市场查询仅 tencent_full 支持(sina/tencent 仅 A 股)。

字段列表

字段按数据源覆盖范围递增排列:三源共有tencent/tencent_full 共有tencent_full 独有

字段 类型 新浪 腾讯 腾讯完整 说明
A. 三源共有
code str 股票代码(index)
name str 股票名称
high float 最高价
open float 开盘价
low float 最低价
pre_close float 昨收价
now float 当前价
volume float 成交量(股)
turnover float 成交额(元)
bid1 ~ bid5 float 买一到买五价
ask1 ~ ask5 float 卖一到卖五价
bid1_volume ~ bid5_volume float 买一到买五量(股)
ask1_volume ~ ask5_volume float 卖一到卖五量(股)
datetime str 行情时间
B. 腾讯/腾讯完整共有(新浪为 NaN)
change float - 涨跌额
change_pct float - 涨跌幅(%)
amplitude float - 振幅
pe_dynamic float - 动态市盈率
pe_static float - 静态市盈率
pb float - 市净率
total_market_cap float - 总市值
circulating_market_cap float - 流通市值
volume_ratio float - 量比
bid_ask_ratio float - 委比
avg_price float - 均价
limit_up float - 涨停价
limit_down float - 跌停价
C. 腾讯完整独有(扩展字段)
secu_code str - - 原始证券代码
market_mark int - - 市场标记
stock_type_cn str - - 股票类型(中文)
market_tags_cn str - - 市场标签(中文)
currency str - - 币种(CNY/HKD/USD)
is_suspend int - - 是否停牌
turnover_rate float - - 换手率(%)
turnover_full float - - 成交额(完整精度)
outer_volume int - - 外盘(主动买盘,股)
inner_volume int - - 内盘(主动卖盘,股)
float_shares int - - 流通股本
total_shares int - - 总股本
capital_ratio float - - 流通占比
speed_5min float - - 5 分钟涨速
speed_5min_real float - - 5 分钟涨速(实时)
change_ytd float - - 年初至今涨跌幅(%)
change_5d/10d/20d/60d float - - 5/10/20/60 日涨跌幅(%)
change_6m/1y float - - 近 6 月/1 年涨跌幅(%)
high_52w/low_52w float - - 52 周最高/最低价

✅ 表示有数据,- 表示 NaN。腾讯完整源(tencent_full)提供最丰富的字段(66 列)和跨市场支持。

会话管理

建议使用上下文管理器复用连接,在频繁轮询场景下性能更佳:

# 同步
with QuoteService() as service:
    df = service.get_all_sync()

# 异步
async with QuoteService() as service:
    df = await service.get_all()

性能对比

与 easyquotation 对比(5500+ 只股票):

数据源 easyquotation qdata_quote sync qdata_quote async
新浪 ~710ms ~640ms ~640ms
腾讯 ~1830ms ~1720ms ~1680ms

三个 source 实测对比(全 A 股 5527 只):

source 耗时 速率 字段数 市场支持
sina 0.12s 44304 只/秒 42 仅 A 股
tencent 0.28s 19680 只/秒 42 仅 A 股
tencent_full 0.48s 11479 只/秒 66 A 股 + 港股 + 美股 + ETF + 北交所
  • 同步引擎:requests + ThreadPoolExecutor 并发请求
  • 异步引擎:aiohttp + asyncio.gather 并发请求
  • 解析优化:文本合并后一次性正则匹配,元组直接构建 DataFrame
  • tencent_full:qt 端点纯文本解析(GBK ~ 分隔),无 JSON 开销

运行基准测试

python -m qdata_quote.bench

依赖

  • Python >= 3.10
  • requests >= 2.28
  • aiohttp >= 3.9
  • pandas >= 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qdata_quote-1.0.0.tar.gz (139.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qdata_quote-1.0.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file qdata_quote-1.0.0.tar.gz.

File metadata

  • Download URL: qdata_quote-1.0.0.tar.gz
  • Upload date:
  • Size: 139.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for qdata_quote-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8856eb8c3668e334175c4eff6e5b5939d17e980db1f666a776e68c2870788ac0
MD5 469d1cce1be8908aff6c535c8dc24d1b
BLAKE2b-256 f92d6f1c748dae3ac1a3cf63db74bfb45933883ba9d7ea5c168e744ea38c5310

See more details on using hashes here.

File details

Details for the file qdata_quote-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: qdata_quote-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for qdata_quote-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10d05049fae6cfc86d9eb628465bb2c30b74b316d9ced9ca5b48f5e0898db1c1
MD5 abe593bbbde527a54dc049f9b3a464a5
BLAKE2b-256 1b3942fa333dc69352f7023177855786c34cf4ed227ff6838fac3e2b666c769e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page