实时行情采集服务,支持新浪和腾讯数据源
Project description
qdata-quote
实时 A 股行情采集服务,支持新浪和腾讯两个数据源。性能超越 easyquotation 约 8-10%。
安装
pip install qdata-quote
快速开始
from qdata_quote import QuoteService, get_stock_codes
# 获取全市场股票代码(首次从网络获取约 4 秒,后续读缓存 <1ms)
codes = get_stock_codes()
service = QuoteService()
service.set_stock_codes(codes)
# 同步获取指定股票行情
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 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")
| 数据源 | 每批数量 | 特点 |
|---|---|---|
| sina | 800 只/批 | 速度快,基础字段齐全 |
| tencent | 60 只/批 | 额外提供涨跌、市盈率、市值、量比等 |
返回格式
返回统一的 pandas.DataFrame,index 为带市场前缀的股票代码(如 sh600000、sz000001)。
字段列表
| 字段 | 类型 | 新浪 | 腾讯 | 说明 |
|---|---|---|---|---|
| code | str | ✅ | ✅ | 股票代码(index) |
| name | str | ✅ | ✅ | 股票名称 |
| open | float | ✅ | ✅ | 开盘价 |
| close | float | ✅ | ✅ | 昨收价 |
| now | float | ✅ | ✅ | 当前价 |
| high | float | ✅ | ✅ | 最高价 |
| low | float | ✅ | ✅ | 最低价 |
| buy | float | ✅ | - | 买一价 |
| sell | float | ✅ | - | 卖一价 |
| volume | float | ✅ | ✅ | 成交量(股) |
| turnover | float | ✅ | ✅ | 成交额(元) |
| bid1 ~ bid5 | float | ✅ | ✅ | 买一到买五价 |
| bid1_volume ~ bid5_volume | float | ✅ | ✅ | 买一到买五量(股) |
| ask1 ~ ask5 | float | ✅ | ✅ | 卖一到卖五价 |
| ask1_volume ~ ask5_volume | float | ✅ | ✅ | 卖一到卖五量(股) |
| datetime | str | ✅ | ✅ | 行情时间 |
| 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 | - | ✅ | 跌停价 |
✅ 表示有数据,- 表示 NaN。腾讯源提供更丰富的衍生指标。
会话管理
建议使用上下文管理器复用连接,在频繁轮询场景下性能更佳:
# 同步
with QuoteService() as service:
service.set_stock_codes(get_stock_codes())
df = service.get_all_sync()
# 异步
async with QuoteService() as service:
service.set_stock_codes(get_stock_codes())
df = await service.get_all()
性能对比
与 easyquotation 对比(5500+ 只股票):
| 数据源 | easyquotation | qdata_quote sync | qdata_quote async |
|---|---|---|---|
| 新浪 | ~710ms | ~640ms | ~640ms |
| 腾讯 | ~1830ms | ~1720ms | ~1680ms |
- 同步引擎:
requests+ThreadPoolExecutor并发请求 - 异步引擎:
aiohttp+asyncio.gather并发请求 - 解析优化:文本合并后一次性正则匹配,元组直接构建 DataFrame
运行基准测试
python -m qdata_quote.bench
依赖
- Python >= 3.10
- requests >= 2.28
- aiohttp >= 3.9
- pandas >= 2.0
Project details
Release history Release notifications | RSS feed
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-0.3.0.tar.gz
(125.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qdata_quote-0.3.0.tar.gz.
File metadata
- Download URL: qdata_quote-0.3.0.tar.gz
- Upload date:
- Size: 125.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2327edae97b948221cbcad6453f3c92957365a776a3edc2a853aca9a520d33a
|
|
| MD5 |
c91c559d88230fb8a8b37a2f22158599
|
|
| BLAKE2b-256 |
14ce0f42ad8877f50eb14ab45f3a863ece997598ee4249f75e25e16cc4720b56
|
File details
Details for the file qdata_quote-0.3.0-py3-none-any.whl.
File metadata
- Download URL: qdata_quote-0.3.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7796ab8c93adb8b321c23d4e55456d4e8601a2b683f473918f7edd80fbd599e
|
|
| MD5 |
2b28fea2dc185c49c5cbb9a5930fe5ca
|
|
| BLAKE2b-256 |
9870b892aab99359f0e81251da7342e99925508e07b62b5bbb990f2e2b862541
|