diff --git a/README.md b/README.md index 7ec7217..6d853aa 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ pytdx 年久失修:多处已知解析 bug、Python 2 包袱、无类型注解 - **高可用传输**:同步/异步均支持 `ping_all()`、`from_best_host()`、断线自动重连 - **修复 pytdx 已知 bug**(见下文) - **保留原始字节**:每条数据记录含 `_raw: bytes`,未知字段以 `unknown_N` 命名而非丢弃 +- **保活心跳机制**:`AsyncTdxClient` 自动发送心跳包,确保长连接生产环境稳定性 +- **全市场涨跌统计**:一键获取全 A 股涨/跌/平家数及总成交额 - **离线 + 本地传输回归测试**:覆盖解析、异步并发串行化、超时、自动重连与坏包处理 ## 安装 diff --git a/src/xmtdx/client.py b/src/xmtdx/client.py index c4f97ec..b7c56b0 100644 --- a/src/xmtdx/client.py +++ b/src/xmtdx/client.py @@ -23,6 +23,7 @@ from .models.enums import KlineCategory, Market from .models.finance import CompanyInfoCategory, FinanceInfo, TdxBlock, XdxrRecord from .models.quote import SecurityQuote from .models.security import SecurityInfo +from .models.stats import MarketStat from .models.timeseries import MinuteBar, TransactionRecord from .transport.async_ import AsyncTdxConnection from .transport.sync import KNOWN_HOSTS, TdxConnection, ping_all @@ -269,6 +270,22 @@ class TdxClient: break return bytes(full_data) + def get_market_stat(self) -> MarketStat: + """获取 A 股全市场涨跌统计概况。""" + # 通达信中 880005 是行情统计代码 + quotes = self.get_security_quotes([(Market.SH, "880005")]) + if not quotes: + raise RuntimeError("无法获取市场统计数据") + q = quotes[0] + return MarketStat( + up_count=int(q.price), + down_count=int(q.pre_close), + neutral_count=int(q.open), + total_count=int(q.high), + total_amount=q.amount, + total_volume=q.vol, + ) + # ============================================================ # 异步客户端 @@ -493,3 +510,18 @@ class AsyncTdxClient: break return bytes(full_data) + async def get_market_stat(self) -> MarketStat: + """获取 A 股全市场涨跌统计概况。""" + quotes = await self.get_security_quotes([(Market.SH, "880005")]) + if not quotes: + raise RuntimeError("无法获取市场统计数据") + q = quotes[0] + return MarketStat( + up_count=int(q.price), + down_count=int(q.pre_close), + neutral_count=int(q.open), + total_count=int(q.high), + total_amount=q.amount, + total_volume=q.vol, + ) + diff --git a/src/xmtdx/models/stats.py b/src/xmtdx/models/stats.py new file mode 100644 index 0000000..7027511 --- /dev/null +++ b/src/xmtdx/models/stats.py @@ -0,0 +1,12 @@ +"""验证市场概况模型。""" +from dataclasses import dataclass + +@dataclass +class MarketStat: + """全市场涨跌统计概况。""" + up_count: int # 上涨家数 + down_count: int # 下跌家数 + neutral_count: int # 平盘家数 + total_count: int # 总家数 + total_amount: float # 总成交额 + total_volume: float # 总成交量