v0.4.0: 初始开源发布
通达信(TDX)行情数据客户端 —— 动态连接池 + 双层TTL缓存 + 3连接并发分页, 纯 asyncio、零运行时依赖。一套接口覆盖 A股(沪/深/北) / 港股 / 美股 / 期货 / 期权。 主要能力: - 四层架构:api → pool(标准/MAC/EX 三池) → protocol(16 命令模块) → cache(内存+SQLite) - TdxData 提供 51 个公开异步方法;CLI(tdx) 提供 19 个子命令 - 服务器文件下载 + 解析:板块 block_*.dat / 行业 tdxhy.cfg / 历史财报 gpcw*.zip - 85 个离线测试(协议 / 连接池 / 缓存 / 分页 / API / 文件解析 / CLI) 协议层参照 easy-tdx 的字节级格式独立实现,不依赖 easy-tdx 包。
This commit is contained in:
commit
9183c3294f
68 files changed
+9249
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,33 @@
|
||||
"""pytdxdata: 通达信行情数据客户端(动态连接池 + 双层TTL缓存 + 3连接并发分页,纯asyncio)
|
||||
|
||||
支持 A股(沪/深/北) + 港股 + 美股 + 期货 + 期权,一套接口全市场。
|
||||
"""
|
||||
from .api import TdxData
|
||||
from .models import (
|
||||
Adjust,
|
||||
ExMarket,
|
||||
FinanceRecord,
|
||||
KlinePeriod,
|
||||
Market,
|
||||
MinuteBar,
|
||||
SecurityBar,
|
||||
SecurityQuote,
|
||||
TransactionRecord,
|
||||
XdxrRecord,
|
||||
)
|
||||
|
||||
__version__ = "0.4.0"
|
||||
__all__ = [
|
||||
"TdxData",
|
||||
"Market",
|
||||
"ExMarket",
|
||||
"KlinePeriod",
|
||||
"Adjust",
|
||||
"SecurityBar",
|
||||
"SecurityQuote",
|
||||
"TransactionRecord",
|
||||
"MinuteBar",
|
||||
"XdxrRecord",
|
||||
"FinanceRecord",
|
||||
"__version__",
|
||||
]
|
||||
@@ -0,0 +1,20 @@
|
||||
"""二进制解包工具"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
from typing import Any
|
||||
|
||||
|
||||
def unpack_from(fmt: str, data: bytes | bytearray | memoryview, pos: int, what: str = "") -> tuple[Any, ...]:
|
||||
"""从 pos 处按 fmt 解包,越界抛清晰错误。"""
|
||||
try:
|
||||
return struct.unpack_from(fmt, data, pos)
|
||||
except struct.error as e:
|
||||
raise ValueError(f"{what} 解包失败 @{pos} fmt={fmt}: {e}") from e
|
||||
|
||||
|
||||
def slice_bytes(data: bytes | bytearray | memoryview, pos: int, length: int, what: str = "") -> bytes:
|
||||
end = pos + length
|
||||
if end > len(data):
|
||||
raise ValueError(f"{what} 截断 @{pos} need {length} have {len(data) - pos}")
|
||||
return bytes(data[pos:end])
|
||||
File diff suppressed because it is too large.
Load diff
Vendored
Whitespace-only changes.
Vendored
+148
@@ -0,0 +1,148 @@
|
||||
"""磁盘持久缓存:SQLite WAL + asyncio.to_thread(事件循环零阻塞)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sqlite3
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
_SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS cache (
|
||||
key TEXT PRIMARY KEY,
|
||||
ttl_name TEXT NOT NULL,
|
||||
expires_at REAL NOT NULL,
|
||||
size INTEGER NOT NULL,
|
||||
payload BLOB NOT NULL,
|
||||
created_at REAL NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_cache_expires ON cache(expires_at);
|
||||
"""
|
||||
|
||||
|
||||
class DiskCache:
|
||||
def __init__(self, path: Path, *, max_bytes: int = 512 * 1024 * 1024) -> None:
|
||||
self.path = Path(path)
|
||||
self.max_bytes = max_bytes
|
||||
self._write_conn: sqlite3.Connection | None = None
|
||||
self._write_lock = threading.Lock()
|
||||
self._read_conn: sqlite3.Connection | None = None
|
||||
|
||||
async def open(self) -> None:
|
||||
def _init():
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self._write_conn = sqlite3.connect(str(self.path), check_same_thread=False)
|
||||
self._write_conn.execute("PRAGMA journal_mode=WAL")
|
||||
self._write_conn.execute("PRAGMA synchronous=NORMAL")
|
||||
self._write_conn.executescript(_SCHEMA)
|
||||
self._write_conn.commit()
|
||||
# 读连接
|
||||
self._read_conn = sqlite3.connect(str(self.path), check_same_thread=False)
|
||||
self._read_conn.execute("PRAGMA query_only=ON")
|
||||
await asyncio.to_thread(_init)
|
||||
|
||||
async def close(self) -> None:
|
||||
def _close():
|
||||
for c in (self._write_conn, self._read_conn):
|
||||
if c is not None:
|
||||
c.close()
|
||||
await asyncio.to_thread(_close)
|
||||
|
||||
async def get(self, key: str) -> bytes | None:
|
||||
return (await self.get_many([key]))[0]
|
||||
|
||||
async def get_many(self, keys: list[str]) -> list[bytes | None]:
|
||||
"""批量读,减少往返。"""
|
||||
def _read() -> list[bytes | None]:
|
||||
conn = self._read_conn
|
||||
if conn is None:
|
||||
return [None] * len(keys)
|
||||
now = time.time()
|
||||
out: list[bytes | None] = []
|
||||
for k in keys:
|
||||
row = conn.execute(
|
||||
"SELECT payload, expires_at FROM cache WHERE key=?", (k,)
|
||||
).fetchone()
|
||||
if row is None:
|
||||
out.append(None)
|
||||
elif row[1] > 0 and now > row[1]:
|
||||
out.append(None)
|
||||
else:
|
||||
out.append(row[0])
|
||||
return out
|
||||
return await asyncio.to_thread(_read)
|
||||
|
||||
async def set(self, key: str, value: bytes, ttl: float, ttl_name: str = "") -> None:
|
||||
def _write():
|
||||
with self._write_lock:
|
||||
if self._write_conn is None:
|
||||
return
|
||||
now = time.time()
|
||||
expires_at = 0.0 if ttl <= 0 else now + ttl
|
||||
self._write_conn.execute(
|
||||
"INSERT OR REPLACE INTO cache (key, ttl_name, expires_at, size, payload, created_at) "
|
||||
"VALUES (?,?,?,?,?,?)",
|
||||
(key, ttl_name, expires_at, len(value), value, now),
|
||||
)
|
||||
self._write_conn.commit()
|
||||
await asyncio.to_thread(_write)
|
||||
|
||||
async def delete(self, key: str) -> None:
|
||||
def _del():
|
||||
with self._write_lock:
|
||||
if self._write_conn is not None:
|
||||
self._write_conn.execute("DELETE FROM cache WHERE key=?", (key,))
|
||||
self._write_conn.commit()
|
||||
await asyncio.to_thread(_del)
|
||||
|
||||
async def delete_prefix(self, prefix: str) -> int:
|
||||
"""按前缀批量删除,返回删除条数。"""
|
||||
def _del_prefix() -> int:
|
||||
with self._write_lock:
|
||||
if self._write_conn is None:
|
||||
return 0
|
||||
cur = self._write_conn.execute(
|
||||
"DELETE FROM cache WHERE key LIKE ?", (f"{prefix}%",)
|
||||
)
|
||||
self._write_conn.commit()
|
||||
return cur.rowcount
|
||||
return await asyncio.to_thread(_del_prefix)
|
||||
|
||||
async def sweep(self) -> int:
|
||||
"""清理过期行 + 超限淘汰,返回清理条数。"""
|
||||
def _sweep() -> int:
|
||||
with self._write_lock:
|
||||
if self._write_conn is None:
|
||||
return 0
|
||||
now = time.time()
|
||||
cur = self._write_conn.execute(
|
||||
"DELETE FROM cache WHERE expires_at > 0 AND expires_at < ?", (now,)
|
||||
)
|
||||
n = cur.rowcount
|
||||
# 超限淘汰最旧
|
||||
total = self._write_conn.execute("SELECT COALESCE(SUM(size),0) FROM cache").fetchone()[0]
|
||||
if total > self.max_bytes:
|
||||
overflow = total - self.max_bytes
|
||||
rows = self._write_conn.execute(
|
||||
"SELECT key, size FROM cache ORDER BY created_at ASC"
|
||||
).fetchall()
|
||||
freed = 0
|
||||
for k, sz in rows:
|
||||
if freed >= overflow:
|
||||
break
|
||||
self._write_conn.execute("DELETE FROM cache WHERE key=?", (k,))
|
||||
freed += sz
|
||||
n += 1
|
||||
self._write_conn.commit()
|
||||
return n
|
||||
return await asyncio.to_thread(_sweep)
|
||||
|
||||
async def stats(self) -> dict:
|
||||
def _stats() -> dict:
|
||||
if self._read_conn is None:
|
||||
return {"rows": 0, "bytes": 0}
|
||||
r = self._read_conn.execute(
|
||||
"SELECT count(*), COALESCE(SUM(size),0) FROM cache"
|
||||
).fetchone()
|
||||
return {"rows": r[0], "bytes": r[1]}
|
||||
return await asyncio.to_thread(_stats)
|
||||
Vendored
+46
@@ -0,0 +1,46 @@
|
||||
"""缓存键构造:{category}|{market}|{code}|{period}|{start}|{count}|{extra}"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CacheKey:
|
||||
category: str # kline|quote|transaction|list|minute|xdxr|finance
|
||||
market: int | None = None
|
||||
code: str | None = None
|
||||
period: str | None = None
|
||||
start: int | None = None
|
||||
count: int | None = None
|
||||
extra: str = "" # adjust/date/股票集哈希 等规范化参数
|
||||
|
||||
def to_str(self) -> str:
|
||||
return "|".join([
|
||||
self.category,
|
||||
"" if self.market is None else str(self.market),
|
||||
self.code or "",
|
||||
self.period or "",
|
||||
"" if self.start is None else str(self.start),
|
||||
"" if self.count is None else str(self.count),
|
||||
self.extra,
|
||||
])
|
||||
|
||||
@classmethod
|
||||
def from_str(cls, s: str) -> "CacheKey":
|
||||
parts = s.split("|")
|
||||
while len(parts) < 7:
|
||||
parts.append("")
|
||||
def _i(v: str) -> int | None:
|
||||
return int(v) if v else None
|
||||
return cls(
|
||||
category=parts[0], market=_i(parts[1]), code=parts[2] or None,
|
||||
period=parts[3] or None, start=_i(parts[4]), count=_i(parts[5]),
|
||||
extra=parts[6],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def quote_key(cls, stocks: Sequence[tuple[int, str]]) -> "CacheKey":
|
||||
"""报价键:对股票集规范化排序,顺序无关可命中。"""
|
||||
norm = sorted(f"{m}:{c}" for m, c in stocks)
|
||||
return cls(category="quote", extra=",".join(norm))
|
||||
Vendored
+125
@@ -0,0 +1,125 @@
|
||||
"""缓存管理器:内存LRU + 磁盘SQLite 双层 + single-flight 并发去重"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Any
|
||||
|
||||
from .disk import DiskCache
|
||||
from .key import CacheKey
|
||||
from .memory import MemoryCache
|
||||
from .ttl import ttl_for
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CacheManager:
|
||||
"""双层缓存:读 内存→磁盘→回源;写 内存立即+异步回写磁盘。
|
||||
|
||||
single-flight:同 key 并发请求只回源一次,其余 await 同一 future。
|
||||
"""
|
||||
|
||||
def __init__(self, memory: MemoryCache, disk: DiskCache | None = None,
|
||||
*, max_inflight: int = 64) -> None:
|
||||
self.memory = memory
|
||||
self.disk = disk
|
||||
self._locks: dict[str, asyncio.Lock] = {}
|
||||
self._semaphore = asyncio.Semaphore(max_inflight)
|
||||
|
||||
async def start(self) -> None:
|
||||
if self.disk is not None:
|
||||
await self.disk.open()
|
||||
await self.disk.sweep()
|
||||
|
||||
async def close(self) -> None:
|
||||
if self.disk is not None:
|
||||
await self.disk.close()
|
||||
|
||||
async def get(self, key: CacheKey) -> bytes | None:
|
||||
ks = key.to_str()
|
||||
# 1. 内存
|
||||
v = self.memory.get(ks)
|
||||
if v is not None:
|
||||
return v
|
||||
# 2. 磁盘
|
||||
if self.disk is not None:
|
||||
v = (await self.disk.get_many([ks]))[0]
|
||||
if v is not None:
|
||||
ttl = ttl_for(key.category, period=key.period, adjust=key.extra, date=_parse_date(key.extra))
|
||||
self.memory.set(ks, v, ttl)
|
||||
return v
|
||||
return None
|
||||
|
||||
async def set(self, key: CacheKey, payload: bytes, *, ttl: float | None = None) -> None:
|
||||
ks = key.to_str()
|
||||
t = ttl if ttl is not None else ttl_for(
|
||||
key.category, period=key.period, adjust=key.extra, date=_parse_date(key.extra)
|
||||
)
|
||||
self.memory.set(ks, payload, t)
|
||||
if self.disk is not None:
|
||||
# 异步回写(best-effort,失败不阻塞)
|
||||
try:
|
||||
await self.disk.set(ks, payload, t, ttl_name=key.category)
|
||||
except Exception:
|
||||
log.debug("磁盘缓存写失败: %s", ks)
|
||||
|
||||
async def get_or_fetch(
|
||||
self,
|
||||
key: CacheKey,
|
||||
fetcher: Callable[[], Awaitable[bytes]],
|
||||
) -> bytes:
|
||||
"""命中缓存返回;否则 single-flight 回源并缓存。"""
|
||||
ks = key.to_str()
|
||||
cached = await self.get(key)
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
lock = self._locks.setdefault(ks, asyncio.Lock())
|
||||
try:
|
||||
async with lock:
|
||||
# 双检:等待期间可能已被其他任务写入
|
||||
cached = await self.get(key)
|
||||
if cached is not None:
|
||||
return cached
|
||||
async with self._semaphore:
|
||||
payload = await fetcher()
|
||||
await self.set(key, payload)
|
||||
return payload
|
||||
finally:
|
||||
# 确保清理锁(即使 fetcher 异常也清理,避免无限增长)
|
||||
self._locks.pop(ks, None)
|
||||
|
||||
async def invalidate(self, category: str, *, market: int | None = None,
|
||||
code: str | None = None) -> int:
|
||||
"""按前缀批量失效,返回失效条数。"""
|
||||
n = 0
|
||||
# 内存:遍历匹配(使用公共 keys() 方法)
|
||||
keys = self.memory.keys()
|
||||
for k in keys:
|
||||
try:
|
||||
ck = CacheKey.from_str(k)
|
||||
except Exception:
|
||||
continue
|
||||
if ck.category == category:
|
||||
if market is not None and ck.market != market:
|
||||
continue
|
||||
if code is not None and ck.code != code:
|
||||
continue
|
||||
self.memory.delete(k)
|
||||
n += 1
|
||||
# 磁盘:按前缀删除(best-effort)
|
||||
if self.disk is not None:
|
||||
prefix = f"{category}|{'' if market is None else market}|"
|
||||
try:
|
||||
await self.disk.delete_prefix(prefix)
|
||||
except Exception:
|
||||
pass
|
||||
return n
|
||||
|
||||
|
||||
def _parse_date(extra: str) -> int | None:
|
||||
"""从 extra 解析 date(transaction类)。extra 可能是 '20260811' 或空。"""
|
||||
if extra and extra.isdigit() and len(extra) == 8:
|
||||
return int(extra)
|
||||
return None
|
||||
Vendored
+63
@@ -0,0 +1,63 @@
|
||||
"""内存TTL感知LRU缓存(OrderedDict,get命中刷新热度,超容量逐出最旧)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
class MemoryCache:
|
||||
def __init__(self, max_entries: int = 4096, max_bytes: int = 128 * 1024 * 1024) -> None:
|
||||
self.max_entries = max_entries
|
||||
self.max_bytes = max_bytes
|
||||
self._data: OrderedDict[str, tuple[bytes, float]] = OrderedDict() # key -> (value, expires_at)
|
||||
self._bytes = 0
|
||||
|
||||
def get(self, key: str) -> bytes | None:
|
||||
item = self._data.get(key)
|
||||
if item is None:
|
||||
return None
|
||||
value, expires_at = item
|
||||
if expires_at > 0 and time.monotonic() > expires_at:
|
||||
self._delete(key)
|
||||
return None
|
||||
self._data.move_to_end(key) # LRU刷新
|
||||
return value
|
||||
|
||||
def set(self, key: str, value: bytes, ttl: float) -> None:
|
||||
expires_at = 0.0 if ttl <= 0 else time.monotonic() + ttl
|
||||
old = self._data.pop(key, None)
|
||||
if old is not None:
|
||||
self._bytes -= len(old[0])
|
||||
self._data[key] = (value, expires_at)
|
||||
self._bytes += len(value)
|
||||
# 超容量逐出最旧
|
||||
while (len(self._data) > self.max_entries or self._bytes > self.max_bytes) and self._data:
|
||||
self._data.popitem(last=False)
|
||||
# 简化:逐出后重新计算字节数
|
||||
self._recount()
|
||||
|
||||
def delete(self, key: str) -> None:
|
||||
self._delete(key)
|
||||
|
||||
def keys(self) -> list[str]:
|
||||
"""返回当前所有缓存 key(供失效/清理使用)。"""
|
||||
return list(self._data.keys())
|
||||
|
||||
def _delete(self, key: str) -> None:
|
||||
item = self._data.pop(key, None)
|
||||
if item is not None:
|
||||
self._bytes -= len(item[0])
|
||||
|
||||
def _recount(self) -> None:
|
||||
self._bytes = sum(len(v[0]) for v in self._data.values())
|
||||
|
||||
def clear(self) -> None:
|
||||
self._data.clear()
|
||||
self._bytes = 0
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._data)
|
||||
|
||||
@property
|
||||
def bytes_used(self) -> int:
|
||||
return self._bytes
|
||||
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
"""TTL分级策略:按数据类型返回缓存有效期"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, time as dtime, timedelta, timezone
|
||||
|
||||
# 上海时区 UTC+8
|
||||
SH_TZ = timezone(timedelta(hours=8))
|
||||
|
||||
# 分钟级周期(盘中数据变化快)
|
||||
MINUTE_PERIODS = {"MIN_1", "MIN_5", "MIN_15", "MIN_30", "MIN_60"}
|
||||
|
||||
|
||||
def ttl_for(
|
||||
category: str,
|
||||
*,
|
||||
period: str | None = None,
|
||||
adjust: str | None = None,
|
||||
date: int | None = None,
|
||||
) -> float:
|
||||
"""返回 TTL(秒)。0=不过期(永久),负值=立即失效。"""
|
||||
if category == "quote":
|
||||
return 5.0
|
||||
if category == "kline":
|
||||
if period in MINUTE_PERIODS:
|
||||
return 60.0
|
||||
if adjust in ("QFQ", "HFQ"):
|
||||
return 86400.0
|
||||
return _until_next_midnight() # 日线当天(次日00:30失效)
|
||||
if category == "transaction":
|
||||
if date is None or date == _today_ymd():
|
||||
return 60.0 # 当日逐笔变化快
|
||||
return 0.0 # 历史逐笔永久
|
||||
if category == "minute":
|
||||
return 60.0
|
||||
if category in ("list", "count", "xdxr", "finance"):
|
||||
return 86400.0
|
||||
return 60.0
|
||||
|
||||
|
||||
def _until_next_midnight() -> float:
|
||||
"""距次日00:30(上海时区)的秒数,12h兜底。"""
|
||||
now = datetime.now(SH_TZ)
|
||||
nxt = datetime.combine(now.date() + timedelta(days=1), dtime(0, 30), tzinfo=SH_TZ)
|
||||
ttl = (nxt - now).total_seconds()
|
||||
return max(0.0, min(ttl, 12 * 3600))
|
||||
|
||||
|
||||
def _today_ymd() -> int:
|
||||
now = datetime.now(SH_TZ)
|
||||
return now.year * 10000 + now.month * 100 + now.day
|
||||
@@ -0,0 +1,541 @@
|
||||
"""pytdxdata CLI 工具
|
||||
|
||||
用法:
|
||||
tdx quotes sz000001 # 行情报价(五档)
|
||||
tdx kline sz000001 --period day --count 30 --adjust qfq
|
||||
tdx kline hk00700 --count 30 # 港股K线
|
||||
tdx kline cffex:IFL0 --count 30 # 期货K线(郑商所:zz / 大商所:dl / 上期所:sh / 中金所:cffex / 广期所:gz)
|
||||
tdx transactions sz000001 --count 50 # 逐笔成交
|
||||
tdx minute sz000001 # 分时(今日/历史--date)
|
||||
tdx index 000001 --count 30 # 指数K线
|
||||
tdx board list [--type 1] # 板块列表(0全部/1行业/2概念)
|
||||
tdx board members 881001 --count 20 # 板块成分
|
||||
tdx board ranking [--top 10] # 板块涨跌幅排行
|
||||
tdx board change --days 5 --top 10 # 板块N日涨幅排行
|
||||
tdx info sz000001 # 个股特征快照
|
||||
tdx list [--market sz] [--count 100] # 证券列表
|
||||
tdx auction sz000001 # 集合竞价
|
||||
tdx unusual [--market sh] [--count 30] # 市场异动
|
||||
tdx xdxr sz000001 # 除权除息
|
||||
tdx finance sz000001 # 财务快照
|
||||
tdx flow sz000001 # 资金流向
|
||||
tdx server-info # 服务器交易时段
|
||||
tdx market-stat # 市场统计
|
||||
tdx block [block_gn.dat] [--limit N] # 板块文件下载+解析(成分股)
|
||||
tdx industry [codes...] [--search 词] # 行业分类下载+解析(tdxhy.cfg)
|
||||
tdx financial-list # 历史专业财报文件索引(gpcw.txt)
|
||||
tdx financial gpcw20260331.zip # 历史专业财报记录下载+解析(gpcw*.zip)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
from .api import TdxData
|
||||
from dataclasses import asdict
|
||||
|
||||
from .models import Adjust, ExMarket, KlinePeriod, Market
|
||||
|
||||
PERIODS = {
|
||||
"min1": KlinePeriod.MIN_1, "min5": KlinePeriod.MIN_5, "min15": KlinePeriod.MIN_15,
|
||||
"min30": KlinePeriod.MIN_30, "min60": KlinePeriod.MIN_60,
|
||||
"day": KlinePeriod.DAY, "week": KlinePeriod.WEEK, "month": KlinePeriod.MONTH,
|
||||
"year": KlinePeriod.YEAR, "season": KlinePeriod.SEASON,
|
||||
}
|
||||
ADJUSTS = {"none": Adjust.NONE, "qfq": Adjust.QFQ, "hfq": Adjust.HFQ}
|
||||
MARKET_NAMES = {"sz": Market.SZ, "sh": Market.SH, "bj": Market.BJ}
|
||||
EX_MARKETS = {
|
||||
"hk": ExMarket.HK_MAIN_BOARD, "us": ExMarket.US_STOCK,
|
||||
"zz": ExMarket.ZZ_FUTURES, "dl": ExMarket.DL_FUTURES,
|
||||
"shf": ExMarket.SH_FUTURES, "cffex": ExMarket.CFFEX_FUTURES, "gz": ExMarket.GZ_FUTURES,
|
||||
}
|
||||
|
||||
|
||||
class Symbol:
|
||||
"""解析后的标的: kind=cn(标准) / ex(扩展市场) / index(指数)"""
|
||||
|
||||
def __init__(self, kind: str, market: int, code: str):
|
||||
self.kind = kind
|
||||
self.market = market
|
||||
self.code = code
|
||||
|
||||
def to_market(self) -> Market:
|
||||
return Market(self.market)
|
||||
|
||||
|
||||
def parse_symbol(sym: str) -> Symbol:
|
||||
"""sz000001 / sh600000 / bj920992 / hk00700 / usAAPL / cffex:IFL0 / 000001(指数)"""
|
||||
sym = sym.strip()
|
||||
if ":" in sym:
|
||||
mkt, code = sym.split(":", 1)
|
||||
mkt = mkt.lower()
|
||||
if mkt in EX_MARKETS:
|
||||
return Symbol("ex", int(EX_MARKETS[mkt]), code.upper())
|
||||
if mkt in MARKET_NAMES:
|
||||
return Symbol("cn", int(MARKET_NAMES[mkt]), code.upper())
|
||||
raise ValueError("未知市场前缀: %s (cn: sz/sh/bj; ex: hk/us/zz/dl/shf/cffex/gz)" % mkt)
|
||||
prefix, code = sym[:2].lower(), sym[2:]
|
||||
if prefix in MARKET_NAMES and len(code) == 6:
|
||||
return Symbol("cn", int(MARKET_NAMES[prefix]), code.upper())
|
||||
if prefix == "hk" and len(code) == 5:
|
||||
return Symbol("ex", int(ExMarket.HK_MAIN_BOARD), code.upper())
|
||||
if prefix == "us":
|
||||
return Symbol("ex", int(ExMarket.US_STOCK), code.upper())
|
||||
if len(sym) == 6 and sym.isdigit():
|
||||
return Symbol("index", int(Market.SH), sym.upper())
|
||||
raise ValueError("无法识别标的: %s" % sym)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 打印辅助
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def _fmt_num(v: float) -> str:
|
||||
if v >= 1e8:
|
||||
return "%.2f亿" % (v / 1e8)
|
||||
if v >= 1e4:
|
||||
return "%.2f万" % (v / 1e4)
|
||||
return str(int(v))
|
||||
|
||||
|
||||
def _pct(price: float, pre: float) -> str:
|
||||
if not pre:
|
||||
return ""
|
||||
return "%+.2f%%" % ((price / pre - 1) * 100)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 各子命令
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
async def cmd_quotes(td: TdxData, symbols: list[str]) -> None:
|
||||
cn, ex = [], []
|
||||
for sym in symbols:
|
||||
s = parse_symbol(sym)
|
||||
if s.kind == "cn":
|
||||
cn.append((Market(s.market), s.code))
|
||||
else:
|
||||
ex.append((s.market, s.code))
|
||||
for q in await td.get_quotes(cn):
|
||||
_print_cn_quote(q)
|
||||
if ex:
|
||||
from .protocol.commands.ex_proto import GetExInstrumentQuoteCmd
|
||||
for market, code in ex:
|
||||
q = await td._execute_ex(GetExInstrumentQuoteCmd(market, code))
|
||||
_print_ex_quote(market, code, q)
|
||||
|
||||
|
||||
def _print_cn_quote(q) -> None:
|
||||
pct = (q.price / q.pre_close - 1) * 100 if q.pre_close else 0
|
||||
print("=" * 58)
|
||||
print(" %s %s" % (q.code, q.server_time or datetime.now().strftime("%H:%M:%S")))
|
||||
print(" 最新 %-10.2f 涨跌 %+8.2f (%+.2f%%)" % (q.price, q.price - q.pre_close, pct))
|
||||
print(" 今开 %-10.2f 最高 %.2f 最低 %.2f" % (q.open, q.high, q.low))
|
||||
print(" 昨收 %-10.2f 成交量 %s 成交额 %s" % (q.pre_close, _fmt_num(q.vol), _fmt_num(q.amount)))
|
||||
if q.bid and q.ask:
|
||||
print(" " + "-" * 56)
|
||||
for i in range(max(len(q.bid), len(q.ask))):
|
||||
b = q.bid[i] if i < len(q.bid) else (0, 0)
|
||||
a = q.ask[i] if i < len(q.ask) else (0, 0)
|
||||
print(" 买%-1d %8.2f %10s | 卖%-1d %8.2f %10s" % (
|
||||
i + 1, b[0], _fmt_num(b[1]), i + 1, a[0], _fmt_num(a[1])))
|
||||
print()
|
||||
|
||||
|
||||
def _print_ex_quote(market: int, code: str, q: dict | None) -> None:
|
||||
if not q:
|
||||
print(" %s: 无报价数据" % code)
|
||||
return
|
||||
price, pre = q.get("price") or 0, q.get("pre_close") or 0
|
||||
mkt = {31: "港股", 74: "美股"}.get(market, "扩展")
|
||||
print("=" * 58)
|
||||
print(" %s %s" % (mkt, code))
|
||||
print(" 最新 %-10.2f 涨跌 %+8.2f (%s)" % (price, price - pre, _pct(price, pre)))
|
||||
print(" 今开 %-10.2f 最高 %.2f 最低 %.2f" % (
|
||||
q.get("open") or 0, q.get("high") or 0, q.get("low") or 0))
|
||||
print(" 昨收 %-10.2f 总量 %s" % (pre, _fmt_num(q.get("zongliang") or 0)))
|
||||
print()
|
||||
|
||||
|
||||
async def cmd_kline(td: TdxData, sym: str, period: KlinePeriod, count: int,
|
||||
start: int, adjust: Adjust) -> None:
|
||||
s = parse_symbol(sym)
|
||||
if s.kind == "cn":
|
||||
bars = await td.get_kline(Market(s.market), s.code, period, start=start,
|
||||
count=count, adjust=adjust)
|
||||
elif s.kind == "index":
|
||||
bars = await td.get_index_kline(Market(s.market), s.code, period, start=start, count=count)
|
||||
else:
|
||||
bars = await td.get_kline(ExMarket(s.market), s.code, period, start=start, count=count)
|
||||
print("%s K线 (%s, %d根):" % (sym, period.name, len(bars)))
|
||||
print(" %-12s %8s %8s %8s %8s %12s" % ("日期", "开盘", "最高", "最低", "收盘", "成交量"))
|
||||
for b in bars:
|
||||
dt = "%04d-%02d-%02d" % (b.year, b.month, b.day)
|
||||
if b.hour:
|
||||
dt += " %02d:%02d" % (b.hour, b.minute)
|
||||
print(" %-12s %8.2f %8.2f %8.2f %8.2f %12s" % (
|
||||
dt, b.open, b.high, b.low, b.close, _fmt_num(b.vol)))
|
||||
|
||||
|
||||
async def cmd_transactions(td: TdxData, sym: str, count: int, date: int | None) -> None:
|
||||
s = parse_symbol(sym)
|
||||
tx = await td.get_transactions(Market(s.market), s.code, date=date, count=count)
|
||||
print("%s 逐笔成交 (%d条):" % (sym, len(tx)))
|
||||
print(" 时间 价格 成交量 方向")
|
||||
for t in tx:
|
||||
bs = "买" if getattr(t, "bs_flag", 0) == 1 else "卖" if getattr(t, "bs_flag", 0) == 2 else "-"
|
||||
tc = getattr(t, "trade_count", 0)
|
||||
print(" %-10s %8.2f %10s %s%s" % (t.time, t.price, _fmt_num(t.vol), bs,
|
||||
" (%d笔)" % tc if tc else ""))
|
||||
|
||||
|
||||
async def cmd_minute(td: TdxData, sym: str, date: int | None) -> None:
|
||||
s = parse_symbol(sym)
|
||||
bars = await td.get_minute(Market(s.market), s.code, date=date)
|
||||
print("%s 分时 (%d条):" % (sym, len(bars)))
|
||||
print(" 时间 价格 均价 成交量")
|
||||
for i, b in enumerate(bars):
|
||||
print(" %02d:%02d %8.2f %8.2f %10s" % (
|
||||
i // 60, i % 60, b.price, b.avg_price, _fmt_num(b.vol)))
|
||||
|
||||
|
||||
async def cmd_board(td: TdxData, args) -> None:
|
||||
if args.action == "list":
|
||||
items = await td.get_board_list(args.type, args.count)
|
||||
print("板块列表 (%d条, type=%d):" % (len(items), args.type))
|
||||
print(" %-10s %-16s %10s %10s" % ("代码", "名称", "涨跌幅%", "成交额"))
|
||||
for b in items:
|
||||
name = getattr(b, "name", "")
|
||||
code = getattr(b, "code", "")
|
||||
chg = getattr(b, "change_pct", None)
|
||||
amt = getattr(b, "amount", None)
|
||||
print(" %-10s %-16s %10s %10s" % (
|
||||
code, name, ("%.2f" % chg) if chg is not None else "-",
|
||||
_fmt_num(amt) if amt else "-"))
|
||||
elif args.action == "members":
|
||||
items = await td.get_board_members(args.symbol, count=args.count)
|
||||
print("%s 成分股 (%d条):" % (args.symbol, len(items)))
|
||||
for m in items:
|
||||
f = getattr(m, "fields", {})
|
||||
price = f.get("0x4") or f.get("price") or 0
|
||||
chg = f.get("0x2e")
|
||||
print(" %-10s %-10s 价=%.2f%s" % (
|
||||
m.code, m.name[:6], price, " 涨%.2f%%" % chg if chg else ""))
|
||||
elif args.action == "ranking":
|
||||
rows = await td.get_board_ranking(args.type, args.top)
|
||||
print("板块涨跌幅排行 (type=%d, top%d):" % (args.type, args.top))
|
||||
for r in rows:
|
||||
print(" %-12s %+8.2f%%" % (r.get("name", ""), r.get("change_pct", 0)))
|
||||
elif args.action == "change":
|
||||
rows = await td.get_board_change_ranking(args.type, args.days, args.top)
|
||||
print("板块%d日涨幅排行 (top%d):" % (args.days, args.top))
|
||||
for r in rows:
|
||||
print(" %-12s %+8.2f%%" % (r.get("name", ""), r.get("change_pct", 0)))
|
||||
|
||||
|
||||
async def cmd_info(td: TdxData, sym: str) -> None:
|
||||
s = parse_symbol(sym)
|
||||
info = await td.get_symbol_info(Market(s.market), s.code)
|
||||
if isinstance(info, dict):
|
||||
items = info.items()
|
||||
else:
|
||||
items = asdict(info).items()
|
||||
for k, v in items:
|
||||
print(" %-24s %s" % (k, v))
|
||||
|
||||
|
||||
async def cmd_list(td: TdxData, market: str, count: int) -> None:
|
||||
m = MARKET_NAMES[market]
|
||||
n = await td.get_security_count(m)
|
||||
items = await td.get_security_list(m, count=count)
|
||||
print("%s市场证券数: %d, 前%d条:" % (market.upper(), n, len(items)))
|
||||
for it in items:
|
||||
print(" %s %s" % (it.code, it.name.strip()))
|
||||
|
||||
|
||||
async def cmd_auction(td: TdxData, sym: str) -> None:
|
||||
s = parse_symbol(sym)
|
||||
items = await td.get_auction(Market(s.market), s.code)
|
||||
print("%s 集合竞价 (%d条):" % (sym, len(items)))
|
||||
for a in items:
|
||||
print(" %s 价格=%.3f 量=%d %s" % (
|
||||
getattr(a, "time", ""), getattr(a, "price", 0),
|
||||
getattr(a, "volume", 0), getattr(a, "side", "")))
|
||||
|
||||
|
||||
async def cmd_unusual(td: TdxData, market: str, count: int) -> None:
|
||||
items = await td.get_unusual(MARKET_NAMES[market], count=count)
|
||||
print("市场异动 (%d条):" % len(items))
|
||||
for u in items:
|
||||
print(" %s %s %s" % (getattr(u, "code", ""), getattr(u, "name", ""),
|
||||
getattr(u, "describe", "") or getattr(u, "type", "")))
|
||||
|
||||
|
||||
async def cmd_xdxr(td: TdxData, sym: str) -> None:
|
||||
s = parse_symbol(sym)
|
||||
items = await td.get_xdxr(Market(s.market), s.code)
|
||||
print("%s 除权除息 (%d条):" % (sym, len(items)))
|
||||
for x in items[:20]:
|
||||
print(" %s 送%g 配%g 派%g" % (
|
||||
getattr(x, "year", ""), getattr(x, "songzhuangu", 0) or 0,
|
||||
getattr(x, "peigu", 0) or 0, getattr(x, "peixian", 0) or 0))
|
||||
|
||||
|
||||
async def cmd_finance(td: TdxData, sym: str) -> None:
|
||||
s = parse_symbol(sym)
|
||||
f = await td.get_finance(Market(s.market), s.code)
|
||||
if not f:
|
||||
print("无财务数据")
|
||||
return
|
||||
for k, v in asdict(f).items():
|
||||
print(" %-24s %s" % (k, v))
|
||||
|
||||
|
||||
async def cmd_flow(td: TdxData, sym: str) -> None:
|
||||
s = parse_symbol(sym)
|
||||
f = await td.get_capital_flow(Market(s.market), s.code)
|
||||
if isinstance(f, dict):
|
||||
items = f.items()
|
||||
else:
|
||||
items = asdict(f).items()
|
||||
for k, v in items:
|
||||
print(" %-20s %s" % (k, v))
|
||||
|
||||
|
||||
_BLOCK_CATEGORY = {0: "行业", 1: "地域", 2: "概念", 3: "风格"}
|
||||
|
||||
|
||||
async def cmd_block(td: TdxData, filename: str, limit: int, show_codes: bool) -> None:
|
||||
blocks = await td.get_block_parsed(filename)
|
||||
print("%s 板块文件 (%d 个板块):" % (filename, len(blocks)))
|
||||
if not blocks:
|
||||
return
|
||||
print(" %-4s %-14s %6s %s" % ("类别", "板块名", "成分数", "代码示例"))
|
||||
shown = blocks if limit <= 0 else blocks[:limit]
|
||||
for b in shown:
|
||||
cat = _BLOCK_CATEGORY.get(getattr(b, "category", -1), "?")
|
||||
codes = getattr(b, "codes", None) or []
|
||||
count = getattr(b, "count", len(codes))
|
||||
sample = " ".join(codes[:5]) + (" ..." if len(codes) > 5 else "")
|
||||
print(" %-4s %-14s %6d %s" % (cat, getattr(b, "name", ""), count, sample))
|
||||
if show_codes and codes:
|
||||
print(" " + " ".join(codes))
|
||||
if 0 < limit < len(blocks):
|
||||
print(" ... 其余 %d 个板块省略(--limit 0 显示全部)" % (len(blocks) - limit))
|
||||
|
||||
|
||||
async def cmd_industry(td: TdxData, codes: list[str], limit: int, search: str | None) -> None:
|
||||
hy = await td.get_industry_map()
|
||||
if codes:
|
||||
print("行业分类 (共 %d 只):" % len(hy))
|
||||
for c in codes:
|
||||
info = hy.get(c)
|
||||
if info is None:
|
||||
print(" %-8s 未收录" % c)
|
||||
else:
|
||||
print(" %-8s 通达信=%-12s 申万=%s" % (
|
||||
c, info.tdx_industry or "-", info.sw_industry or "-"))
|
||||
return
|
||||
rows = list(hy.items())
|
||||
if search:
|
||||
rows = [(c, i) for c, i in rows
|
||||
if search in (i.tdx_industry or "") or search in (i.sw_industry or "")]
|
||||
tip = ",匹配 '%s' %d 条" % (search, len(rows)) if search else ""
|
||||
print("行业分类 tdxhy.cfg (共 %d 只%s):" % (len(hy), tip))
|
||||
print(" %-8s %-14s %s" % ("代码", "通达信行业", "申万行业"))
|
||||
shown = rows if limit <= 0 else rows[:limit]
|
||||
for c, i in shown:
|
||||
print(" %-8s %-14s %s" % (c, i.tdx_industry or "-", i.sw_industry or "-"))
|
||||
if 0 < limit < len(rows):
|
||||
print(" ... 其余 %d 条省略(--limit 0 显示全部)" % (len(rows) - limit))
|
||||
|
||||
|
||||
async def cmd_financial_list(td: TdxData, limit: int) -> None:
|
||||
infos = await td.get_financial_file_infos()
|
||||
print("历史专业财报文件 (gpcw.txt, 共 %d 个):" % len(infos))
|
||||
if not infos:
|
||||
return
|
||||
print(" %-24s %-34s %12s" % ("文件名", "MD5", "大小"))
|
||||
shown = infos if limit <= 0 else infos[:limit]
|
||||
for i in shown:
|
||||
print(" %-24s %-34s %12d" % (i.filename, i.hash, i.filesize))
|
||||
if 0 < limit < len(infos):
|
||||
print(" ... 其余 %d 个省略(--limit 0 显示全部)" % (len(infos) - limit))
|
||||
print("\n 提示: 用 `tdx financial %s --limit 5` 查看最新一期财报" % infos[-1].filename)
|
||||
|
||||
|
||||
async def cmd_financial(td: TdxData, filename: str, code: str | None,
|
||||
limit: int, fields: int) -> None:
|
||||
records = await td.get_financial_records_parsed(filename)
|
||||
if code:
|
||||
records = [r for r in records if r.code == code]
|
||||
print("%s 财报记录 (%d 条):" % (filename, len(records)))
|
||||
if not records:
|
||||
return
|
||||
shown = records if limit <= 0 else records[:limit]
|
||||
for r in shown:
|
||||
sample = " ".join("%g" % v for v in r.fields[:fields]) if fields > 0 else ""
|
||||
print(" %-8s 市场=%s 报告期=%s 字段数=%d %s" % (
|
||||
r.code, "沪" if r.market == 1 else "深",
|
||||
r.report_date or "-", len(r.fields), sample))
|
||||
if 0 < limit < len(records):
|
||||
print(" ... 其余 %d 条省略(--limit 0 显示全部)" % (len(records) - limit))
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 入口
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def main() -> None:
|
||||
ap = argparse.ArgumentParser(prog="tdx", description="通达信行情CLI (pytdxdata)")
|
||||
sub = ap.add_subparsers(dest="cmd", required=True)
|
||||
|
||||
def add(name, help):
|
||||
return sub.add_parser(name, help=help)
|
||||
|
||||
q = add("quotes", "行情报价")
|
||||
q.add_argument("symbols", nargs="+")
|
||||
q.set_defaults(func=lambda td, a: cmd_quotes(td, a.symbols))
|
||||
|
||||
k = add("kline", "K线")
|
||||
k.add_argument("symbol")
|
||||
k.add_argument("--period", choices=list(PERIODS), default="day")
|
||||
k.add_argument("--count", type=int, default=30)
|
||||
k.add_argument("--start", type=int, default=0)
|
||||
k.add_argument("--adjust", choices=list(ADJUSTS), default="none")
|
||||
k.set_defaults(func=lambda td, a: cmd_kline(
|
||||
td, a.symbol, PERIODS[a.period], a.count, a.start, ADJUSTS[a.adjust]))
|
||||
|
||||
t = add("transactions", "逐笔成交")
|
||||
t.add_argument("symbol")
|
||||
t.add_argument("--count", type=int, default=30)
|
||||
t.add_argument("--date", type=str, default=None)
|
||||
t.set_defaults(func=lambda td, a: cmd_transactions(
|
||||
td, a.symbol, a.count, int(a.date) if a.date else None))
|
||||
|
||||
m = add("minute", "分时")
|
||||
m.add_argument("symbol")
|
||||
m.add_argument("--date", type=str, default=None)
|
||||
m.set_defaults(func=lambda td, a: cmd_minute(td, a.symbol, int(a.date) if a.date else None))
|
||||
|
||||
ix = add("index", "指数K线")
|
||||
ix.add_argument("code")
|
||||
ix.add_argument("--count", type=int, default=30)
|
||||
ix.set_defaults(func=lambda td, a: cmd_kline(
|
||||
td, a.code, KlinePeriod.DAY, a.count, 0, Adjust.NONE))
|
||||
|
||||
b = add("board", "板块")
|
||||
b.add_argument("action", choices=["list", "members", "ranking", "change"])
|
||||
b.add_argument("symbol", nargs="?", default="")
|
||||
b.add_argument("--type", type=int, default=1)
|
||||
b.add_argument("--count", type=int, default=30)
|
||||
b.add_argument("--top", type=int, default=10)
|
||||
b.add_argument("--days", type=int, default=20)
|
||||
b.set_defaults(func=lambda td, a: cmd_board(td, a))
|
||||
|
||||
i = add("info", "个股特征快照")
|
||||
i.add_argument("symbol")
|
||||
i.set_defaults(func=lambda td, a: cmd_info(td, a.symbol))
|
||||
|
||||
ls = add("list", "证券列表")
|
||||
ls.add_argument("--market", choices=list(MARKET_NAMES), default="sz")
|
||||
ls.add_argument("--count", type=int, default=30)
|
||||
ls.set_defaults(func=lambda td, a: cmd_list(td, a.market, a.count))
|
||||
|
||||
au = add("auction", "集合竞价")
|
||||
au.add_argument("symbol")
|
||||
au.set_defaults(func=lambda td, a: cmd_auction(td, a.symbol))
|
||||
|
||||
un = add("unusual", "市场异动")
|
||||
un.add_argument("--market", choices=list(MARKET_NAMES), default="sh")
|
||||
un.add_argument("--count", type=int, default=30)
|
||||
un.set_defaults(func=lambda td, a: cmd_unusual(td, a.market, a.count))
|
||||
|
||||
xd = add("xdxr", "除权除息")
|
||||
xd.add_argument("symbol")
|
||||
xd.set_defaults(func=lambda td, a: cmd_xdxr(td, a.symbol))
|
||||
|
||||
fn = add("finance", "财务快照")
|
||||
fn.add_argument("symbol")
|
||||
fn.set_defaults(func=lambda td, a: cmd_finance(td, a.symbol))
|
||||
|
||||
fl = add("flow", "资金流向")
|
||||
fl.add_argument("symbol")
|
||||
fl.set_defaults(func=lambda td, a: cmd_flow(td, a.symbol))
|
||||
|
||||
bk = add("block", "板块文件 下载+解析 (block_*.dat)")
|
||||
bk.add_argument("filename", nargs="?", default="block_gn.dat",
|
||||
help="板块文件名(默认 block_gn.dat;常用 block_zs.dat 行业 / block_fg.dat 风格)")
|
||||
bk.add_argument("--limit", type=int, default=30, help="最多显示板块数(0=全部)")
|
||||
bk.add_argument("--codes", action="store_true", help="显示每个板块完整成分股代码")
|
||||
bk.set_defaults(func=lambda td, a: cmd_block(td, a.filename, a.limit, a.codes))
|
||||
|
||||
ind = add("industry", "行业分类 下载+解析 (tdxhy.cfg)")
|
||||
ind.add_argument("codes", nargs="*", help="查询指定 6 位代码(缺省列出全部)")
|
||||
ind.add_argument("--limit", type=int, default=30, help="最多显示条数(0=全部)")
|
||||
ind.add_argument("--search", default=None, help="按行业名关键词过滤")
|
||||
ind.set_defaults(func=lambda td, a: cmd_industry(td, a.codes, a.limit, a.search))
|
||||
|
||||
ff = add("financial-list", "历史专业财报文件索引 下载+解析 (gpcw.txt)")
|
||||
ff.add_argument("--limit", type=int, default=30, help="最多显示条数(0=全部)")
|
||||
ff.set_defaults(func=lambda td, a: cmd_financial_list(td, a.limit))
|
||||
|
||||
fr = add("financial", "历史专业财报记录 下载+解析 (gpcw*.zip)")
|
||||
fr.add_argument("filename", help="如 gpcw20260331.zip(用 financial-list 查看可用文件)")
|
||||
fr.add_argument("--code", default=None, help="只显示指定 6 位股票代码")
|
||||
fr.add_argument("--limit", type=int, default=5, help="最多显示条数(0=全部)")
|
||||
fr.add_argument("--fields", type=int, default=4, help="每条显示前 N 个原始字段(0=不显示)")
|
||||
fr.set_defaults(func=lambda td, a: cmd_financial(td, a.filename, a.code, a.limit, a.fields))
|
||||
|
||||
si = add("server-info", "服务器交易时段")
|
||||
si.set_defaults(func=_server_info)
|
||||
|
||||
ms = add("market-stat", "市场统计")
|
||||
ms.set_defaults(func=_market_stat)
|
||||
|
||||
args = ap.parse_args()
|
||||
|
||||
async def run() -> None:
|
||||
td = TdxData(cache_dir=None, pool_min=2, pool_max=6)
|
||||
await td.start()
|
||||
try:
|
||||
await args.func(td, args)
|
||||
finally:
|
||||
await td.close()
|
||||
|
||||
try:
|
||||
asyncio.run(run())
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
async def _server_info(td: TdxData, args) -> None:
|
||||
await _print_obj(await td.get_server_info())
|
||||
|
||||
|
||||
async def _market_stat(td: TdxData, args) -> None:
|
||||
await _print_obj(await td.get_market_stat())
|
||||
|
||||
|
||||
async def _print_obj(obj) -> None:
|
||||
if isinstance(obj, dict):
|
||||
for k, v in obj.items():
|
||||
print(" %-24s %s" % (k, v))
|
||||
elif hasattr(obj, "_fields"):
|
||||
for f in obj._fields:
|
||||
print(" %-24s %s" % (f, getattr(obj, f)))
|
||||
elif hasattr(obj, "__dataclass_fields__"):
|
||||
for k, v in asdict(obj).items():
|
||||
print(" %-24s %s" % (k, v))
|
||||
elif hasattr(obj, "__slots__"):
|
||||
for k in obj.__slots__:
|
||||
print(" %-24s %s" % (k, getattr(obj, k)))
|
||||
else:
|
||||
print(obj)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,20 @@
|
||||
"""服务器文件解析器:板块(.dat) / 行业(.cfg) / 历史专业财报(.zip+.dat)。
|
||||
|
||||
均为纯标准库实现(struct / zipfile),无第三方依赖,可独立单测。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from .block import infer_block_category, parse_block_dat
|
||||
from .financial import parse_financial_dat, parse_financial_file_list
|
||||
from .industry import parse_tdxhy_cfg
|
||||
from .payload import pack_payload, unpack_payload
|
||||
|
||||
__all__ = [
|
||||
"infer_block_category",
|
||||
"pack_payload",
|
||||
"parse_block_dat",
|
||||
"parse_financial_dat",
|
||||
"parse_financial_file_list",
|
||||
"parse_tdxhy_cfg",
|
||||
"unpack_payload",
|
||||
]
|
||||
@@ -0,0 +1,73 @@
|
||||
"""板块文件 (.dat) 解析:通达信 block_zs/gn/fg.dat -> TdxBlock 列表。
|
||||
|
||||
字节格式(与 easy-tdx / pytdx 一致)::
|
||||
|
||||
Header : 384 字节(跳过,含版本/日期等未知字段)
|
||||
Count : 2 字节 uint16 LE —— 板块条数
|
||||
Body : count × 2813 字节
|
||||
每条 = 9s 板块名(GBK)
|
||||
+ H 成分股数量
|
||||
+ H 类型
|
||||
+ 2800s 代码区(每只股票占 7 字节 ASCII)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..models import TdxBlock
|
||||
|
||||
_HEADER_SIZE = 384
|
||||
_RECORD_SIZE = 2813
|
||||
_CODE_SLOT = 7
|
||||
_MAX_CODES = 2800 // _CODE_SLOT # 单条记录最多 400 只
|
||||
|
||||
|
||||
def infer_block_category(filename: str) -> int:
|
||||
"""按文件名推断板块分类:zs=行业(0) / gn=概念(2) / fg=风格(3)。"""
|
||||
name = filename.lower()
|
||||
if "gn" in name:
|
||||
return 2
|
||||
if "fg" in name:
|
||||
return 3
|
||||
return 0
|
||||
|
||||
|
||||
def parse_block_dat(data: bytes, filename: str = "") -> list[TdxBlock]:
|
||||
"""解析通达信 .dat 板块文件字节内容。
|
||||
|
||||
Args:
|
||||
data: 文件完整字节。
|
||||
filename: 用于推断 category(如 ``block_gn.dat``)。
|
||||
|
||||
Returns:
|
||||
TdxBlock 列表;数据不足或为空时返回空列表。
|
||||
"""
|
||||
if len(data) < _HEADER_SIZE + 2:
|
||||
return []
|
||||
|
||||
(count,) = struct.unpack_from("<H", data, _HEADER_SIZE)
|
||||
category = infer_block_category(filename)
|
||||
|
||||
results: list[TdxBlock] = []
|
||||
pos = _HEADER_SIZE + 2
|
||||
for _ in range(count):
|
||||
if pos + _RECORD_SIZE > len(data):
|
||||
break
|
||||
|
||||
name = data[pos : pos + 9].decode("gbk", errors="replace").strip("\x00").strip()
|
||||
(stock_count,) = struct.unpack_from("<H", data, pos + 9)
|
||||
|
||||
codes: list[str] = []
|
||||
codes_start = pos + 13
|
||||
for i in range(min(stock_count, _MAX_CODES)):
|
||||
raw = data[codes_start + i * _CODE_SLOT : codes_start + (i + 1) * _CODE_SLOT]
|
||||
code = raw.decode("ascii", errors="replace").strip("\x00").strip()
|
||||
if code:
|
||||
codes.append(code)
|
||||
|
||||
results.append(
|
||||
TdxBlock(name=name, category=category, count=stock_count, codes=codes)
|
||||
)
|
||||
pos += _RECORD_SIZE
|
||||
|
||||
return results
|
||||
@@ -0,0 +1,102 @@
|
||||
"""历史专业财报解析:gpcw.txt 索引 + gpcw*.zip 内 .dat 二进制。
|
||||
|
||||
.dat 二进制格式(参考 pytdx.crawler.history_financial_crawler)::
|
||||
|
||||
Header 20B : <1h I 1H 3L
|
||||
- unknown int16 (h)
|
||||
- report_date uint32 (I) 报告期 YYYYMMDD
|
||||
- max_count uint16 (H) 股票索引条目数
|
||||
- unknown1 uint32 (L)
|
||||
- report_size uint32 (L) 每条股票数据字节数
|
||||
- unknown2 uint32 (L)
|
||||
Index max_count × 11B : <6s B L
|
||||
- code 6 bytes 股票代码
|
||||
- market 1 byte 市场(0=深 1=沪)
|
||||
- file_offset uint32 绝对偏移(从文件开头算)
|
||||
Data file_offset 处 : report_size/4 个 float32
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..models import FinancialFileInfo, FinancialRecord
|
||||
|
||||
_HEADER_FMT = "<1hI1H3L"
|
||||
_INDEX_FMT = "<6sBL"
|
||||
|
||||
|
||||
def parse_financial_file_list(data: bytes) -> list[FinancialFileInfo]:
|
||||
"""解析 tdxfin/gpcw.txt 内容(每行 ``filename,md5hash,filesize``)。"""
|
||||
if not data:
|
||||
return []
|
||||
text = data.decode("utf-8", errors="replace").strip()
|
||||
out: list[FinancialFileInfo] = []
|
||||
for line in text.splitlines():
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
parts = line.split(",")
|
||||
if len(parts) < 3:
|
||||
continue
|
||||
try:
|
||||
filesize = int(parts[2])
|
||||
except ValueError:
|
||||
continue
|
||||
out.append(FinancialFileInfo(filename=parts[0], hash=parts[1], filesize=filesize))
|
||||
return out
|
||||
|
||||
|
||||
def parse_financial_dat(data: bytes, report_date: int = 0) -> list[FinancialRecord]:
|
||||
"""解析 gpcw*.zip 内的 .dat 二进制。
|
||||
|
||||
Args:
|
||||
data: .dat 文件完整字节。
|
||||
report_date: 报告期 YYYYMMDD;传 0 时使用 header 内嵌的报告期。
|
||||
|
||||
Returns:
|
||||
FinancialRecord 列表;数据不足或字段非法时返回空列表。
|
||||
"""
|
||||
header_size = struct.calcsize(_HEADER_FMT)
|
||||
if len(data) < header_size:
|
||||
return []
|
||||
|
||||
header = struct.unpack(_HEADER_FMT, data[:header_size])
|
||||
dat_report_date = header[1]
|
||||
max_count = header[2]
|
||||
report_size = header[4]
|
||||
|
||||
if report_date == 0:
|
||||
report_date = dat_report_date
|
||||
|
||||
num_fields = report_size // 4
|
||||
if num_fields <= 0:
|
||||
return []
|
||||
|
||||
index_size = struct.calcsize(_INDEX_FMT)
|
||||
report_fmt = f"<{num_fields}f"
|
||||
report_pack_size = struct.calcsize(report_fmt)
|
||||
|
||||
out: list[FinancialRecord] = []
|
||||
for i in range(max_count):
|
||||
idx_pos = header_size + i * index_size
|
||||
if idx_pos + index_size > len(data):
|
||||
break
|
||||
|
||||
code_bytes, market, file_offset = struct.unpack(
|
||||
_INDEX_FMT, data[idx_pos : idx_pos + index_size]
|
||||
)
|
||||
code = code_bytes.decode("ascii", errors="replace").rstrip("\x00")
|
||||
if not code or file_offset == 0:
|
||||
continue
|
||||
if file_offset + report_pack_size > len(data):
|
||||
continue
|
||||
|
||||
fields = list(
|
||||
struct.unpack(report_fmt, data[file_offset : file_offset + report_pack_size])
|
||||
)
|
||||
out.append(
|
||||
FinancialRecord(
|
||||
code=code, market=market, report_date=report_date, fields=fields
|
||||
)
|
||||
)
|
||||
return out
|
||||
@@ -0,0 +1,40 @@
|
||||
"""通达信行业分类配置 (tdxhy.cfg) 解析。
|
||||
|
||||
文件为 GBK 文本,每行形如::
|
||||
|
||||
市场|代码|通达信行业|||申万行业|...
|
||||
|
||||
只保留 6 位 A 股代码;通达信行业取第 3 列,申万行业取第 6 列(可能缺失)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from ..models import IndustryInfo
|
||||
|
||||
|
||||
def parse_tdxhy_cfg(content: bytes) -> dict[str, IndustryInfo]:
|
||||
"""解析 tdxhy.cfg 字节内容。
|
||||
|
||||
Args:
|
||||
content: 文件完整字节(GBK 编码)。
|
||||
|
||||
Returns:
|
||||
``{6位代码: IndustryInfo}``;空输入或非法行会被跳过。
|
||||
"""
|
||||
results: dict[str, IndustryInfo] = {}
|
||||
if not content:
|
||||
return results
|
||||
|
||||
text = content.decode("gbk", errors="replace")
|
||||
for line in text.splitlines():
|
||||
parts = line.strip().split("|")
|
||||
if len(parts) < 3:
|
||||
continue
|
||||
code = parts[1].strip()
|
||||
if len(code) != 6 or not code.isdigit():
|
||||
continue
|
||||
results[code] = IndustryInfo(
|
||||
code=code,
|
||||
tdx_industry=parts[2].strip(),
|
||||
sw_industry=parts[5].strip() if len(parts) >= 6 else "",
|
||||
)
|
||||
return results
|
||||
@@ -0,0 +1,16 @@
|
||||
"""缓存载荷序列化:records <-> bytes(pickle + zlib level1,性能优先)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pickle
|
||||
import zlib
|
||||
from typing import Any
|
||||
|
||||
|
||||
def pack_payload(obj: Any) -> bytes:
|
||||
"""序列化缓存载荷(压缩)。"""
|
||||
return zlib.compress(pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL), level=1)
|
||||
|
||||
|
||||
def unpack_payload(data: bytes) -> Any:
|
||||
"""反序列化缓存载荷。"""
|
||||
return pickle.loads(zlib.decompress(data))
|
||||
@@ -0,0 +1,127 @@
|
||||
"""服务器清单与默认配置
|
||||
|
||||
服务器清单存于包内 servers.json,由 server_probe.py 导入时自动探测可用性与延时。
|
||||
config.py 加载时只采用 status="ok" 的服务器;环境变量可覆盖;代码内嵌清单为兜底。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
DEFAULT_PORT = 7709
|
||||
DEFAULT_TIMEOUT = 10.0
|
||||
|
||||
_SERVERS_FILE = Path(__file__).resolve().parent / "servers.json" # 包内清单(随包分发)
|
||||
|
||||
|
||||
def _load_ok_hosts(protocol: str, *, fast_only: bool = False) -> list[str]:
|
||||
"""从 servers.json 加载 status=ok 且支持指定协议的服务器。
|
||||
|
||||
fast_only=True 时只返回当日测速入池(fast_pool)的服务器(排名前N或延迟<阈值),
|
||||
用于动态连接池优先使用优质节点。
|
||||
"""
|
||||
try:
|
||||
cfg = json.loads(_SERVERS_FILE.read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
return []
|
||||
out = []
|
||||
for s in cfg.get("servers", []):
|
||||
if s.get("status") != "ok":
|
||||
continue
|
||||
if protocol not in s.get("protocols", []):
|
||||
continue
|
||||
if fast_only and not s.get("fast_pool", False):
|
||||
continue
|
||||
out.append(s["ip"])
|
||||
return out
|
||||
|
||||
|
||||
def _load_ex_handshakes() -> dict[str, str]:
|
||||
try:
|
||||
cfg = json.loads(_SERVERS_FILE.read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
return {s["ip"]: s.get("handshake", "setup") for s in cfg.get("servers", [])
|
||||
if "ex" in s.get("protocols", []) and s.get("status") == "ok"}
|
||||
|
||||
# 标准行情服务器 —— 真实K线探测验证可用(2026-08-12, GetSecurityBarsCmd 600000 日K)
|
||||
# 27台可用 = pytdxwrapper券商服务器(国泰君安/中信短线宝/华林/安信等) + 通达信官方主站
|
||||
# 来源①: pytdxwrapper config.py TDX_HOSTS(券商+官方, 51台探测27可用)
|
||||
# 来源②: easy-tdx known_hosts + connect.cfg官方主站(54台探测仅5可用, 已被券商清单覆盖)
|
||||
# 另2台端口80可用(池统一7709暂不收录): 180.153.18.172 / 202.108.253.139
|
||||
# 不可用特征: K线空响应(握手正常无数据, 仅ping测不出) —— 探测脚本: examples/probe_pytdxwrapper.py / probe_all_hosts.py
|
||||
STANDARD_HOSTS: list[str] = [
|
||||
"139.9.52.158", "103.251.85.94", "58.67.221.146", "59.36.5.11",
|
||||
"103.221.142.82", "218.106.92.182", "60.12.136.251", "218.106.92.183",
|
||||
"60.12.136.250", "115.238.90.170", "220.178.55.86", "220.178.55.71",
|
||||
"180.153.18.170", "115.238.90.165", "60.191.117.167", "218.75.126.9",
|
||||
"115.238.56.198", "117.34.114.16", "159.75.55.232", "117.34.114.17",
|
||||
"117.34.114.14", "117.34.114.15", "117.34.114.13", "117.34.114.27",
|
||||
"117.34.114.18", "117.34.114.20", "117.34.114.30",
|
||||
]
|
||||
|
||||
# MAC 服务器(独立主机池, 2026-08-12 全量复测: 24台→23台可用, 剔除117.34.114.17空响应)
|
||||
# MAC专用(3): 121.37.207.165 / 123.60.47.136 / 121.36.248.138 (对标准命令返回空, 池探测用MacKlineCmd)
|
||||
# 标准服务器MAC支持20台: 券商(国泰君安/中信/安信)与官方主站, 与STANDARD_HOSTS同端口7709
|
||||
# 标准服务器不支持MAC的6台: 218.106.92.182/183 220.178.55.71/86(华林) 159.75.55.232 117.34.114.30
|
||||
# 探测脚本: examples/probe_mac_all.py / probe_mac_support.py
|
||||
MAC_HOSTS: list[str] = [
|
||||
"121.37.207.165", "139.9.52.158", "58.67.221.146", "59.36.5.11",
|
||||
"60.12.136.250", "60.12.136.251", "121.36.248.138", "60.191.117.167",
|
||||
"103.221.142.82", "218.75.126.9", "123.60.47.136", "180.153.18.170",
|
||||
"115.238.56.198", "115.238.90.170", "115.238.90.165", "103.251.85.94",
|
||||
"117.34.114.15", "117.34.114.18", "117.34.114.14", "117.34.114.20",
|
||||
"117.34.114.13", "117.34.114.27", "117.34.114.16",
|
||||
]
|
||||
|
||||
# EX扩展行情服务器(港股/期货/美股,端口7727)
|
||||
# MAC EX专用2台(0x2454 login 80B): 116.205.135.205 / 121.37.232.167
|
||||
# pytdx EX协议2台(setup 0x2454 92B): 通达信扩展市场116.205.143.214(52市场) + 国泰君安103.221.142.82(60市场)
|
||||
# 发现方式(2026-08-12): 券商服务器同机多协议——103.221.142.82同时是MAC服务器(7709)与EX服务器(7727)
|
||||
EX_HOSTS: list[str] = [
|
||||
"116.205.135.205", "121.37.232.167", "116.205.143.214", "103.221.142.82",
|
||||
]
|
||||
# 各服务器握手模式: login(0x2454 login) / setup(0x2454 setup)
|
||||
EX_HANDSHAKES: dict[str, str] = {
|
||||
"116.205.135.205": "login",
|
||||
"121.37.232.167": "login",
|
||||
"116.205.143.214": "setup",
|
||||
"103.221.142.82": "setup",
|
||||
}
|
||||
|
||||
|
||||
def get_standard_hosts() -> list[str]:
|
||||
env = os.environ.get("PYPYTDX_DATA_STANDARD_HOSTS")
|
||||
if env:
|
||||
return [h.strip() for h in env.split(",") if h.strip()]
|
||||
fast = _load_ok_hosts("standard", fast_only=True)
|
||||
if fast:
|
||||
return fast
|
||||
from_json = _load_ok_hosts("standard")
|
||||
return from_json or list(STANDARD_HOSTS)
|
||||
|
||||
|
||||
def get_mac_hosts() -> list[str]:
|
||||
env = os.environ.get("PYPYTDX_DATA_MAC_HOSTS")
|
||||
if env:
|
||||
return [h.strip() for h in env.split(",") if h.strip()]
|
||||
fast = _load_ok_hosts("mac", fast_only=True)
|
||||
if fast:
|
||||
return fast
|
||||
from_json = _load_ok_hosts("mac")
|
||||
return from_json or list(MAC_HOSTS)
|
||||
|
||||
|
||||
def get_ex_hosts() -> list[str]:
|
||||
env = os.environ.get("PYPYTDX_DATA_EX_HOSTS")
|
||||
if env:
|
||||
return [h.strip() for h in env.split(",") if h.strip()]
|
||||
from_json = _load_ok_hosts("ex")
|
||||
return from_json or list(EX_HOSTS)
|
||||
|
||||
|
||||
def get_ex_handshakes() -> dict[str, str]:
|
||||
"""EX服务器握手模式(json优先,代码兜底)。"""
|
||||
from_json = _load_ex_handshakes()
|
||||
return from_json or dict(EX_HANDSHAKES)
|
||||
@@ -0,0 +1,136 @@
|
||||
"""轻量技术指标(纯Python,零依赖):MACD/KDJ/RSI/BOLL/MA/EMA"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Callable
|
||||
|
||||
|
||||
def _ema(values: list[float], period: int) -> list[float]:
|
||||
"""EMA。"""
|
||||
if not values:
|
||||
return []
|
||||
k = 2.0 / (period + 1)
|
||||
out = [values[0]]
|
||||
for v in values[1:]:
|
||||
out.append(v * k + out[-1] * (1 - k))
|
||||
return out
|
||||
|
||||
|
||||
def _sma(values: list[float], period: int) -> list[float]:
|
||||
"""简单移动平均(尾部对齐,前period-1个为NaN用None)。"""
|
||||
out: list[float | None] = []
|
||||
s = 0.0
|
||||
for i, v in enumerate(values):
|
||||
s += v
|
||||
if i >= period:
|
||||
s -= values[i - period]
|
||||
out.append(s / period if i >= period - 1 else None)
|
||||
return out
|
||||
|
||||
|
||||
def ma(values: list[float], period: int) -> list[float | None]:
|
||||
return _sma(values, period)
|
||||
|
||||
|
||||
def ema(values: list[float], period: int) -> list[float]:
|
||||
return _ema(values, period)
|
||||
|
||||
|
||||
def macd(values: list[float], fast: int = 12, slow: int = 26, signal: int = 9) -> dict:
|
||||
"""MACD: DIF/DEA/MACD柱。返回与values等长。"""
|
||||
ema_fast = _ema(values, fast)
|
||||
ema_slow = _ema(values, slow)
|
||||
dif = [f - s for f, s in zip(ema_fast, ema_slow)]
|
||||
dea = _ema(dif, signal)
|
||||
hist = [(d - e) * 2 for d, e in zip(dif, dea)]
|
||||
return {"DIF": dif, "DEA": dea, "MACD": hist}
|
||||
|
||||
|
||||
def rsi(values: list[float], period: int = 14) -> list[float | None]:
|
||||
"""RSI。"""
|
||||
out: list[float | None] = [None] * len(values)
|
||||
if len(values) <= period:
|
||||
return out
|
||||
gains = losses = 0.0
|
||||
for i in range(1, period + 1):
|
||||
chg = values[i] - values[i - 1]
|
||||
if chg > 0:
|
||||
gains += chg
|
||||
else:
|
||||
losses -= chg
|
||||
for i in range(period, len(values)):
|
||||
if i > period:
|
||||
chg = values[i] - values[i - 1]
|
||||
gains = (gains * (period - 1) + max(chg, 0)) / period
|
||||
losses = (losses * (period - 1) + max(-chg, 0)) / period
|
||||
out[i] = 100.0 if losses == 0 else 100.0 - 100.0 / (1 + gains / losses) if losses else 100.0
|
||||
if losses == 0 and gains == 0:
|
||||
out[i] = 50.0
|
||||
return out
|
||||
|
||||
|
||||
def kdj(values_high: list[float], values_low: list[float],
|
||||
values_close: list[float], period: int = 9) -> dict:
|
||||
"""KDJ(简化:9日随机指标)。"""
|
||||
n = len(values_close)
|
||||
k = [50.0] * n
|
||||
d = [50.0] * n
|
||||
j = [50.0] * n
|
||||
for i in range(n):
|
||||
lo = min(values_low[max(0, i - period + 1): i + 1])
|
||||
hi = max(values_high[max(0, i - period + 1): i + 1])
|
||||
rsv = 50.0 if hi == lo else (values_close[i] - lo) / (hi - lo) * 100
|
||||
k[i] = k[i - 1] * 2 / 3 + rsv / 3
|
||||
d[i] = d[i - 1] * 2 / 3 + k[i] / 3
|
||||
j[i] = 3 * k[i] - 2 * d[i]
|
||||
return {"K": k, "D": d, "J": j}
|
||||
|
||||
|
||||
def boll(values: list[float], period: int = 20, mult: float = 2.0) -> dict:
|
||||
"""BOLL布林带。"""
|
||||
import statistics
|
||||
n = len(values)
|
||||
mid = [None] * n
|
||||
upper = [None] * n
|
||||
lower = [None] * n
|
||||
for i in range(period - 1, n):
|
||||
window = values[i - period + 1: i + 1]
|
||||
m = sum(window) / period
|
||||
sd = statistics.pstdev(window)
|
||||
mid[i] = m
|
||||
upper[i] = m + mult * sd
|
||||
lower[i] = m - mult * sd
|
||||
return {"MID": mid, "UPPER": upper, "LOWER": lower}
|
||||
|
||||
|
||||
_INDICATORS: dict[str, Callable] = {
|
||||
"MA": lambda c, **kw: {"MA": ma(c, kw.get("period", 5))},
|
||||
"EMA": lambda c, **kw: {"EMA": ema(c, kw.get("period", 12))},
|
||||
"MACD": macd,
|
||||
"RSI": lambda c, **kw: {"RSI": rsi(c, kw.get("period", 14))},
|
||||
"KDJ": lambda h, l, c, **kw: kdj(h, l, c, kw.get("period", 9)),
|
||||
"BOLL": lambda c, **kw: boll(c, kw.get("period", 20), kw.get("mult", 2.0)),
|
||||
}
|
||||
|
||||
|
||||
def compute_indicators(high: list[float], low: list[float], close: list[float],
|
||||
names: list[str], params: dict | None = None,
|
||||
tail: int | None = None) -> dict:
|
||||
"""计算指标,返回 {指标名: 序列}。"""
|
||||
params = params or {}
|
||||
result: dict = {}
|
||||
for name in names:
|
||||
name_u = name.upper()
|
||||
fn = _INDICATORS.get(name_u)
|
||||
if fn is None:
|
||||
continue
|
||||
kw = params.get(name, {}) or {}
|
||||
try:
|
||||
if name_u == "KDJ":
|
||||
r = fn(high, low, close, **kw)
|
||||
else:
|
||||
r = fn(close, **kw)
|
||||
except Exception:
|
||||
continue
|
||||
for k, v in r.items():
|
||||
result[k] = v[-tail:] if tail else v
|
||||
return result
|
||||
@@ -0,0 +1,407 @@
|
||||
"""数据模型:K线/报价/列表/逐笔/分时/财务(全部dataclass,性能优先不依赖pandas)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from enum import IntEnum
|
||||
from typing import Any
|
||||
|
||||
|
||||
|
||||
|
||||
class ExMarket(IntEnum):
|
||||
"""扩展市场代码(港股/期货/期权,通达信EX通道)"""
|
||||
TEMP_STOCK = 1
|
||||
ZZ_FUTURES_OPTION = 4
|
||||
DL_FUTURES_OPTION = 5
|
||||
SH_FUTURES_OPTION = 6
|
||||
CFFEX_OPTION = 7
|
||||
SH_STOCK_OPTION = 8
|
||||
SZ_STOCK_OPTION = 9
|
||||
BASIC_FX = 10
|
||||
CROSS_FX = 11
|
||||
INTL_INDEX = 12
|
||||
COMEX_FUTURES = 16
|
||||
NYMEX_FUTURES = 17
|
||||
CBOT_FUTURES = 18
|
||||
HK_FINANCIAL_FUTURES = 23
|
||||
HK_FINANCIAL_OPTIONS = 24
|
||||
HK_STOCK_FUTURES = 25
|
||||
HK_STOCK_OPTIONS = 26
|
||||
HK_INDEX = 27
|
||||
ZZ_FUTURES = 28
|
||||
DL_FUTURES = 29
|
||||
SH_FUTURES = 30
|
||||
HK_MAIN_BOARD = 31
|
||||
OPEN_END_FUND = 33
|
||||
MONETARY_FUND = 34
|
||||
MACRO_INDICATOR = 38
|
||||
FUTURES_INDEX = 42
|
||||
B_TO_H = 43
|
||||
NEEQ = 44
|
||||
SH_GOLD = 46
|
||||
CFFEX_FUTURES = 47
|
||||
HK_GEM = 48
|
||||
HK_FUND = 49
|
||||
SUNSHINE_PRIVATE_FUND = 56
|
||||
MAIN_FUTURES_CONTRACT = 60
|
||||
CSI_INDEX = 62
|
||||
GZ_ARBITRAGE_FUTURES = 65
|
||||
GZ_FUTURES = 66
|
||||
GZ_OPTIONS = 67
|
||||
EXTENDED_SECTOR_INDEX = 70
|
||||
HK_STOCK_GGT = 71
|
||||
GE_STOCK = 73
|
||||
US_STOCK = 74
|
||||
SG_STOCK = 78
|
||||
HK_DARK_POOL = 98
|
||||
CODE_MIRROR = 100
|
||||
SZSE_INDEX = 102
|
||||
class Market(IntEnum):
|
||||
SZ = 0
|
||||
SH = 1
|
||||
BJ = 2
|
||||
|
||||
|
||||
class KlinePeriod(IntEnum):
|
||||
"""通达信K线周期编码(KlineCategory,与 easy-tdx 一致)"""
|
||||
MIN_5 = 0
|
||||
MIN_15 = 1
|
||||
MIN_30 = 2
|
||||
MIN_60 = 3
|
||||
DAY = 4
|
||||
WEEK = 5
|
||||
MONTH = 6
|
||||
MIN_1 = 7
|
||||
MIN_3 = 8
|
||||
YEAR = 9
|
||||
SEASON = 10
|
||||
YEAR_ALT = 11
|
||||
|
||||
@property
|
||||
def is_minute(self) -> bool:
|
||||
return self in (KlinePeriod.MIN_1, KlinePeriod.MIN_5, KlinePeriod.MIN_15,
|
||||
KlinePeriod.MIN_30, KlinePeriod.MIN_60)
|
||||
|
||||
|
||||
class Adjust(IntEnum):
|
||||
"""复权方式(MAC通道)"""
|
||||
NONE = 0
|
||||
QFQ = 1 # 前复权
|
||||
HFQ = 2 # 后复权
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class SecurityBar:
|
||||
"""K线记录(价格单位:元)"""
|
||||
market: int
|
||||
code: str
|
||||
open: float
|
||||
high: float
|
||||
low: float
|
||||
close: float
|
||||
vol: float # 股票=股 / 转债=手(与vipdoc一致)
|
||||
amount: float # 元
|
||||
year: int
|
||||
month: int
|
||||
day: int
|
||||
hour: int = 0
|
||||
minute: int = 0
|
||||
|
||||
@property
|
||||
def datetime(self) -> datetime:
|
||||
return datetime(self.year, self.month, self.day, self.hour, self.minute)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class IndexBar(SecurityBar):
|
||||
"""指数K线(多涨跌家数字段,结构同K线仅解析时跳过4字节)"""
|
||||
pass
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class SecurityQuote:
|
||||
"""实时五档报价"""
|
||||
market: int
|
||||
code: str
|
||||
price: float
|
||||
pre_close: float
|
||||
open: float
|
||||
high: float
|
||||
low: float
|
||||
vol: float
|
||||
amount: float
|
||||
s_vol: float # 主动卖
|
||||
b_vol: float # 主动买
|
||||
bid: list[tuple[float, float]] = field(default_factory=list) # [(价,量)x5]
|
||||
ask: list[tuple[float, float]] = field(default_factory=list) # [(价,量)x5]
|
||||
server_time: str = ""
|
||||
trading_status: int = 0
|
||||
cur_vol: float = 0.0 # 当前成交量(手)
|
||||
rise_speed: float = 0.0 # 涨速(%)
|
||||
limit_up: float = 0.0 # 涨停价(本地计算, 未填为0)
|
||||
limit_down: float = 0.0 # 跌停价
|
||||
decimal_point: int = 2 # 价格小数位
|
||||
open_amount: float = 0.0 # 开盘金额(个股)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class SecurityInfo:
|
||||
"""证券列表条目"""
|
||||
market: int
|
||||
code: str
|
||||
name: str
|
||||
volunit: int
|
||||
decimal_point: int
|
||||
pre_close: float
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class TransactionRecord:
|
||||
"""逐笔成交(MAC通道含 trade_count=成交笔数)"""
|
||||
market: int
|
||||
code: str
|
||||
time: str # HH:MM:SS
|
||||
price: float
|
||||
vol: float # 手(MAC逐笔)
|
||||
trade_count: int # 成交笔数(MAC=逐笔记录字段;标准当日逐笔协议也有但需单独解析)
|
||||
bs_flag: int # 1=主动买 0=主动卖
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class MinuteBar:
|
||||
"""分时数据"""
|
||||
price: float
|
||||
vol: float
|
||||
avg_price: float = 0.0 # 均价(协议第2字段)
|
||||
hour: int = 0
|
||||
minute: int = 0
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class XdxrRecord:
|
||||
"""除权除息记录"""
|
||||
market: int
|
||||
code: str
|
||||
year: int
|
||||
month: int
|
||||
day: int
|
||||
category: int
|
||||
name: str
|
||||
fenhong: float = 0.0 # 每股分红
|
||||
peigujia: float = 0.0
|
||||
songzhuangu: float = 0.0 # 每股送转
|
||||
peigu: float = 0.0
|
||||
suogu: float = 0.0
|
||||
xingquanjia: float = 0.0
|
||||
fenshu: float = 0.0
|
||||
# 股本变动类(category 2~10): 前后流通/总股本(万股)
|
||||
panqian_liutong: float = 0.0
|
||||
qian_zongguben: float = 0.0
|
||||
panhou_liutong: float = 0.0
|
||||
hou_zongguben: float = 0.0
|
||||
|
||||
@property
|
||||
def date(self) -> str:
|
||||
"""日期字符串 YYYY-MM-DD。"""
|
||||
return "%04d-%02d-%02d" % (self.year, self.month, self.day)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class FinanceRecord:
|
||||
"""财务快照(股本/金额单位:万股/万元)"""
|
||||
market: int
|
||||
code: str
|
||||
liutong_guben: float = 0.0
|
||||
zong_guben: float = 0.0
|
||||
guojia_gu: float = 0.0
|
||||
faqiren_faren_gu: float = 0.0
|
||||
faren_gu: float = 0.0
|
||||
b_gu: float = 0.0
|
||||
h_gu: float = 0.0
|
||||
zhigong_gu: float = 0.0
|
||||
province: int = 0
|
||||
industry: int = 0
|
||||
updated_date: int = 0
|
||||
ipo_date: int = 0
|
||||
gudong_renshu: float = 0.0
|
||||
zong_zichan: float = 0.0
|
||||
liudong_zichan: float = 0.0
|
||||
guding_zichan: float = 0.0
|
||||
wuxing_zichan: float = 0.0
|
||||
liudong_fuzhai: float = 0.0
|
||||
changqi_fuzhai: float = 0.0
|
||||
ziben_gongjijin: float = 0.0
|
||||
jing_zichan: float = 0.0
|
||||
zhuying_shouru: float = 0.0
|
||||
zhuying_lirun: float = 0.0
|
||||
yingshou_zhangkuan: float = 0.0
|
||||
yingye_lirun: float = 0.0
|
||||
touzi_shouyu: float = 0.0
|
||||
jingying_xianjinliu: float = 0.0
|
||||
zong_xianjinliu: float = 0.0
|
||||
cunhuo: float = 0.0
|
||||
lirun_zonghe: float = 0.0
|
||||
shuihou_lirun: float = 0.0
|
||||
jing_lirun: float = 0.0
|
||||
weifen_lirun: float = 0.0
|
||||
meigujing_zichan: float = 0.0
|
||||
reserve2: float = 0.0
|
||||
|
||||
|
||||
# ===== MAC 扩展命令模型 =====
|
||||
|
||||
@dataclass(slots=True)
|
||||
class BoardInfo:
|
||||
"""板块列表条目(含领涨股信息)"""
|
||||
market: int
|
||||
code: str
|
||||
name: str
|
||||
price: float
|
||||
rise_speed: float
|
||||
pre_close: float
|
||||
# 领涨股(160B记录的后80B)
|
||||
symbol_market: int = 0
|
||||
symbol_code: str = ""
|
||||
symbol_name: str = ""
|
||||
symbol_price: float = 0.0
|
||||
symbol_rise_speed: float = 0.0
|
||||
symbol_pre_close: float = 0.0
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class MemberQuote:
|
||||
"""板块成分股报价(MAC自定义字段)"""
|
||||
market: int
|
||||
code: str
|
||||
name: str
|
||||
fields: dict[str, float] = field(default_factory=dict)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class BelongBoard:
|
||||
"""个股所属板块"""
|
||||
market: int
|
||||
board_code: str
|
||||
board_name: str
|
||||
close: float
|
||||
pre_close: float
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class AuctionItem:
|
||||
"""集合竞价"""
|
||||
time: str
|
||||
price: float
|
||||
matched: int
|
||||
unmatched: int
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class UnusualItem:
|
||||
"""市场异动"""
|
||||
market: int
|
||||
code: str
|
||||
name: str
|
||||
type: int
|
||||
desc: str
|
||||
value: float
|
||||
time: str
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class SymbolSnapshot:
|
||||
"""个股特征快照"""
|
||||
market: int
|
||||
code: str
|
||||
name: str
|
||||
date: int
|
||||
time: int
|
||||
activity: int
|
||||
pre_close: float
|
||||
open: float
|
||||
high: float
|
||||
low: float
|
||||
close: float
|
||||
vol: float
|
||||
amount: float
|
||||
turnover: float
|
||||
avg_price: float
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class GoodsItem:
|
||||
"""扩展市场商品(期货/期权)"""
|
||||
category: int
|
||||
name: str
|
||||
code: float
|
||||
switch: int
|
||||
index: int
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class ServerInfo:
|
||||
"""服务器交易时段信息"""
|
||||
date: int
|
||||
last_trade_date: int
|
||||
sessions: list[tuple[int, int]] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class CapitalFlow:
|
||||
"""资金流向"""
|
||||
market: int
|
||||
code: str
|
||||
main_buy: float
|
||||
main_sell: float
|
||||
retail_buy: float
|
||||
retail_sell: float
|
||||
buy5: float
|
||||
sell5: float
|
||||
big5: float
|
||||
mid5: float
|
||||
|
||||
|
||||
# ===== 服务器文件解析模型(板块/行业/历史专业财报) =====
|
||||
|
||||
@dataclass(slots=True)
|
||||
class TdxBlock:
|
||||
"""通达信板块条目(解析自 block_zs/gn/fg.dat)。
|
||||
|
||||
category: 0=行业 1=地域 2=概念 3=风格(按文件名推断)
|
||||
count: 文件内声明的成分股数量(可能大于 codes 实际解析数)
|
||||
"""
|
||||
name: str
|
||||
category: int
|
||||
count: int
|
||||
codes: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class IndustryInfo:
|
||||
"""个股行业分类(解析自 tdxhy.cfg)"""
|
||||
code: str
|
||||
tdx_industry: str # 通达信行业代码
|
||||
sw_industry: str = "" # 申万行业代码(部分文件缺失)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class FinancialFileInfo:
|
||||
"""历史专业财报文件索引条目(解析自 tdxfin/gpcw.txt)"""
|
||||
filename: str # 如 gpcw20260331.zip
|
||||
hash: str # MD5
|
||||
filesize: int # 字节
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class FinancialRecord:
|
||||
"""历史专业财报记录(解析自 gpcw*.zip 内 .dat)。
|
||||
|
||||
注意与 FinanceRecord(最新财务快照)区分:本类保留协议原始 float 字段,
|
||||
字段含义随 report_size 变化,需结合 gpcw.txt 字段字典解释。
|
||||
"""
|
||||
code: str
|
||||
market: int # 0=深 1=沪
|
||||
report_date: int # 报告期 YYYYMMDD
|
||||
fields: list[float] = field(default_factory=list)
|
||||
Whitespace-only changes.
@@ -0,0 +1,137 @@
|
||||
"""单条 asyncio TCP 连接:连接 + 3条握手 + 帧收发 + 命令执行"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from ..protocol.commands.base import BaseCommand
|
||||
from ..protocol.commands.setup import SETUP_COMMANDS
|
||||
from ..protocol.frame import HEADER_SIZE, decompress_body, parse_header
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class TdxConnection(Generic[T]):
|
||||
"""一条通达信 TCP 连接(连接时自动握手,execute 发送命令并解析响应)。"""
|
||||
|
||||
def __init__(self, host: str, port: int = 7709, timeout: float = 10.0) -> None:
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.timeout = timeout
|
||||
self._reader: asyncio.StreamReader | None = None
|
||||
self._writer: asyncio.StreamWriter | None = None
|
||||
self._last_result: T | None = None
|
||||
|
||||
@property
|
||||
def alive(self) -> bool:
|
||||
return self._writer is not None and not self._writer.is_closing()
|
||||
|
||||
async def connect(self) -> None:
|
||||
self._reader, self._writer = await asyncio.wait_for(
|
||||
asyncio.open_connection(self.host, self.port), timeout=self.timeout
|
||||
)
|
||||
await self._send_setup()
|
||||
|
||||
async def close(self) -> None:
|
||||
if self._writer is not None:
|
||||
try:
|
||||
self._writer.close()
|
||||
await self._writer.wait_closed()
|
||||
except (ConnectionError, OSError):
|
||||
pass
|
||||
self._writer = None
|
||||
self._reader = None
|
||||
|
||||
def _force_close(self) -> None:
|
||||
"""强制中断底层连接(abort transport),用于超时/错误后让挂起的 read 立即失效。"""
|
||||
writer = self._writer
|
||||
self._writer = None
|
||||
self._reader = None
|
||||
if writer is not None:
|
||||
try:
|
||||
transport = writer.transport
|
||||
transport.abort() # 立即 RST,解除任何挂起的 read(),避免协程无法被取消
|
||||
except (ConnectionError, OSError, AttributeError):
|
||||
try:
|
||||
writer.close()
|
||||
except (ConnectionError, OSError):
|
||||
pass
|
||||
|
||||
async def ping(self) -> float | None:
|
||||
"""连接+握手+发一条setup命令测延迟(秒)。失败返回 None。"""
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
await self.connect()
|
||||
assert self._writer is not None
|
||||
self._writer.write(SETUP_COMMANDS[0])
|
||||
await self._writer.drain()
|
||||
hdr_buf = await self._recv_exact(HEADER_SIZE)
|
||||
hdr = parse_header(hdr_buf)
|
||||
if hdr.zipsize > 0:
|
||||
await self._recv_exact(hdr.zipsize)
|
||||
return time.monotonic() - t0
|
||||
except (ConnectionError, OSError, asyncio.TimeoutError, ValueError):
|
||||
return None
|
||||
finally:
|
||||
await self.close()
|
||||
|
||||
async def execute(self, cmd: BaseCommand[T]) -> T:
|
||||
"""发送请求 → 读16B帧头 → 读body → 解压 → 命令解析。"""
|
||||
if not self.alive:
|
||||
raise ConnectionError(f"连接未建立或已关闭: {self.host}")
|
||||
assert self._reader is not None and self._writer is not None
|
||||
try:
|
||||
try:
|
||||
# 用手动 wait_for 包裹 IO,超时后强制关闭底层连接,
|
||||
# 避免 asyncio.timeout 与外层 wait_for 取消产生竞态导致协程无法中断。
|
||||
await asyncio.wait_for(self._io(cmd), self.timeout)
|
||||
except asyncio.TimeoutError:
|
||||
# 半开连接/服务器无响应:关闭连接使其真正失效,便于重试时换连接
|
||||
self._force_close()
|
||||
raise ConnectionError(f"通信超时 {self.host} ({self.timeout}s)") from None
|
||||
except (ConnectionError, OSError) as e:
|
||||
self._force_close()
|
||||
raise ConnectionError(f"通信错误 {self.host}: {e}") from e
|
||||
except (ConnectionError, OSError) as e:
|
||||
raise e
|
||||
return self._last_result # type: ignore[return-value]
|
||||
|
||||
async def _io(self, cmd: BaseCommand[T]) -> None:
|
||||
assert self._reader is not None and self._writer is not None
|
||||
self._writer.write(cmd.build_request())
|
||||
await self._writer.drain()
|
||||
header_buf = await self._recv_exact(HEADER_SIZE)
|
||||
header = parse_header(header_buf)
|
||||
raw_body = await self._recv_exact(header.zipsize)
|
||||
body = decompress_body(header, raw_body)
|
||||
self._last_result: T = cmd.parse_response(body)
|
||||
|
||||
async def _send_setup(self) -> None:
|
||||
"""按序发送3条握手命令并丢弃响应。"""
|
||||
assert self._reader is not None and self._writer is not None
|
||||
for cmd_bytes in SETUP_COMMANDS:
|
||||
try:
|
||||
async with asyncio.timeout(self.timeout):
|
||||
self._writer.write(cmd_bytes)
|
||||
await self._writer.drain()
|
||||
hdr_buf = await self._recv_exact(HEADER_SIZE)
|
||||
hdr = parse_header(hdr_buf)
|
||||
if hdr.zipsize > 0:
|
||||
await self._recv_exact(hdr.zipsize)
|
||||
except (ConnectionError, OSError, asyncio.TimeoutError):
|
||||
# 部分服务器握手无响应,忽略
|
||||
pass
|
||||
|
||||
async def _recv_exact(self, n: int) -> bytes:
|
||||
"""循环读取直到 n 字节。"""
|
||||
assert self._reader is not None
|
||||
buf = bytearray()
|
||||
while len(buf) < n:
|
||||
chunk = await asyncio.wait_for(
|
||||
self._reader.read(n - len(buf)), timeout=self.timeout
|
||||
)
|
||||
if not chunk:
|
||||
raise ConnectionError(f"连接被服务器关闭: {self.host}")
|
||||
buf.extend(chunk)
|
||||
return bytes(buf)
|
||||
@@ -0,0 +1,103 @@
|
||||
"""MAC EX 扩展行情通道:login(0x2454) + 端口7727 + 帧首字节改写"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import struct
|
||||
import time
|
||||
|
||||
from ..protocol.commands.base import BaseCommand
|
||||
from ..protocol.frame import HEADER_SIZE, decompress_body, parse_header
|
||||
|
||||
# 80字节 login body(来自 opentdx,easy-tdx 已验证)
|
||||
_LOGIN_BODY = bytes(
|
||||
bytearray.fromhex(
|
||||
"e5bb1c2fafe52594" "1f32c6e5d53dfb41" "5b734cc9cdbf0ac9"
|
||||
"2021bfdd1eb06d22" "d008884c1611cb13" "78f6abd824d899d2"
|
||||
"1f32c6e5d53dfb41" "1f32c6e5d53dfb41" "a9325ac935dc0837"
|
||||
"335a16e4ce17c1bb"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class MacExLoginCmd(BaseCommand[bool]):
|
||||
"""EX扩展行情登录(0x2454,head_flag=0x01)。"""
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
inner = struct.pack("<H", 0x2454) + _LOGIN_BODY
|
||||
header = struct.pack("<BIBHH", 0x01, 0, 1, len(inner), len(inner))
|
||||
return header + inner
|
||||
|
||||
def parse_response(self, body: bytes) -> bool:
|
||||
return len(body) >= 2 # 响应body非空即成功
|
||||
|
||||
|
||||
class ExTdxConnection:
|
||||
"""EX扩展行情连接:TCP(7727) + login + 帧首字节0x1C→0x01改写。
|
||||
|
||||
扩展行情服务器要求先login,且请求帧head_flag为0x01(MAC为0x1C)。
|
||||
"""
|
||||
|
||||
def __init__(self, host: str, port: int = 7727, timeout: float = 10.0,
|
||||
handshake: str = "login") -> None:
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.timeout = timeout
|
||||
self.handshake = handshake # "login"=0x2454 login 80B | "setup"=0x2454 setup 70B
|
||||
self._reader: asyncio.StreamReader | None = None
|
||||
self._writer: asyncio.StreamWriter | None = None
|
||||
self.last_used: float = 0.0
|
||||
|
||||
@property
|
||||
def alive(self) -> bool:
|
||||
return self._writer is not None and not self._writer.is_closing()
|
||||
|
||||
async def connect(self) -> None:
|
||||
self._reader, self._writer = await asyncio.wait_for(
|
||||
asyncio.open_connection(self.host, self.port), timeout=self.timeout
|
||||
)
|
||||
if self.handshake == "setup":
|
||||
from ..protocol.commands.ex_proto import ExSetupCmd
|
||||
await self.execute(ExSetupCmd())
|
||||
else:
|
||||
await self.execute(MacExLoginCmd())
|
||||
|
||||
async def close(self) -> None:
|
||||
if self._writer is not None:
|
||||
try:
|
||||
self._writer.close()
|
||||
await self._writer.wait_closed()
|
||||
except (ConnectionError, OSError):
|
||||
pass
|
||||
self._writer = None
|
||||
self._reader = None
|
||||
|
||||
async def execute(self, cmd: BaseCommand) -> object:
|
||||
if not self.alive:
|
||||
raise ConnectionError(f"EX连接未建立: {self.host}")
|
||||
assert self._reader is not None and self._writer is not None
|
||||
try:
|
||||
request = cmd.build_request()
|
||||
if request and request[0] == 0x1C:
|
||||
request = b"\x01" + request[1:] # EX模式首字节改写
|
||||
self._writer.write(request)
|
||||
await self._writer.drain()
|
||||
self.last_used = time.monotonic()
|
||||
header_buf = await self._recv_exact(HEADER_SIZE)
|
||||
header = parse_header(header_buf)
|
||||
raw_body = await self._recv_exact(header.zipsize)
|
||||
except (ConnectionError, OSError, asyncio.TimeoutError) as e:
|
||||
raise ConnectionError(f"EX通信错误 {self.host}: {e}") from e
|
||||
body = decompress_body(header, raw_body)
|
||||
return cmd.parse_response(body)
|
||||
|
||||
async def _recv_exact(self, n: int) -> bytes:
|
||||
assert self._reader is not None
|
||||
buf = bytearray()
|
||||
while len(buf) < n:
|
||||
chunk = await asyncio.wait_for(
|
||||
self._reader.read(n - len(buf)), timeout=self.timeout
|
||||
)
|
||||
if not chunk:
|
||||
raise ConnectionError(f"EX连接被关闭: {self.host}")
|
||||
buf.extend(chunk)
|
||||
return bytes(buf)
|
||||
@@ -0,0 +1,119 @@
|
||||
"""EX扩展市场连接池(简化版:固定连接数,借还+坏连接重建+login自动)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import time
|
||||
|
||||
from .ex_connection import ExTdxConnection
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ExPool:
|
||||
"""EX通道连接池(港股/期货,端口7727,连接时自动login)。"""
|
||||
|
||||
def __init__(self, servers: list[str], *, timeout: float = 10.0,
|
||||
min_size: int = 2, max_size: int = 6,
|
||||
per_server_cap: int = 1,
|
||||
handshake: str = "login",
|
||||
handshake_map: dict | None = None) -> None:
|
||||
self.servers = servers
|
||||
self.timeout = timeout
|
||||
self.handshake = handshake
|
||||
self.handshake_map = handshake_map or {}
|
||||
self.min_size = min_size
|
||||
self.max_size = max_size
|
||||
self.per_server_cap = max(1, per_server_cap) # 一台服务器最多几个连接(默认1=单连接)
|
||||
self._idle: list[tuple[str, ExTdxConnection]] = []
|
||||
self._server_count: dict[str, int] = {}
|
||||
self._total = 0
|
||||
self._closed = False
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def start(self) -> None:
|
||||
async with self._lock:
|
||||
for host in self.servers[: self.min_size]:
|
||||
if self._server_count.get(host, 0) >= self.per_server_cap:
|
||||
continue
|
||||
try:
|
||||
conn = ExTdxConnection(host, 7727, self.timeout, self.handshake_map.get(host, self.handshake))
|
||||
await conn.connect()
|
||||
self._idle.append((host, conn))
|
||||
self._total += 1
|
||||
self._server_count[host] = self._server_count.get(host, 0) + 1
|
||||
except Exception as e:
|
||||
log.warning("EX连接失败 %s: %s", host, str(e)[:60])
|
||||
|
||||
async def close(self) -> None:
|
||||
self._closed = True
|
||||
async with self._lock:
|
||||
for _, conn in self._idle:
|
||||
try:
|
||||
await conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
self._idle.clear()
|
||||
self._total = 0
|
||||
self._server_count.clear()
|
||||
|
||||
async def acquire(self) -> ExTdxConnection:
|
||||
for _ in range(3):
|
||||
stale_conn = None
|
||||
new_conn = None
|
||||
new_host = None
|
||||
async with self._lock:
|
||||
if self._idle:
|
||||
_, conn = self._idle.pop()
|
||||
fresh = time.monotonic() - conn.last_used < 30.0
|
||||
if conn.alive and fresh:
|
||||
return conn
|
||||
# 连接空闲过久(login会话可能失效)→ 丢弃重连
|
||||
self._total = max(0, self._total - 1)
|
||||
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
|
||||
stale_conn = conn
|
||||
# 扩容:在锁内预留槽位,锁外执行网络IO
|
||||
elif self._total < self.max_size and self.servers:
|
||||
host = self._next_available_server()
|
||||
if host:
|
||||
self._total += 1
|
||||
self._server_count[host] = self._server_count.get(host, 0) + 1
|
||||
new_conn = ExTdxConnection(host, 7727, self.timeout, self.handshake_map.get(host, self.handshake))
|
||||
new_host = host
|
||||
# 锁外:关闭过期连接、建立新连接
|
||||
if stale_conn is not None:
|
||||
try:
|
||||
await stale_conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
if new_conn is not None:
|
||||
try:
|
||||
await new_conn.connect()
|
||||
return new_conn
|
||||
except Exception as e:
|
||||
log.warning("EX扩容失败 %s: %s", new_host, str(e)[:60])
|
||||
# 连接失败,回滚预留槽位
|
||||
async with self._lock:
|
||||
self._total = max(0, self._total - 1)
|
||||
self._server_count[new_host] = max(0, self._server_count.get(new_host, 0) - 1)
|
||||
await asyncio.sleep(0.05)
|
||||
raise ConnectionError("EX连接池无可用连接")
|
||||
|
||||
def _next_available_server(self) -> str | None:
|
||||
for h in self.servers:
|
||||
if self._server_count.get(h, 0) < self.per_server_cap:
|
||||
return h
|
||||
return None
|
||||
|
||||
async def release(self, conn: ExTdxConnection, *, broken: bool = False) -> None:
|
||||
if broken or not conn.alive:
|
||||
async with self._lock:
|
||||
self._total = max(0, self._total - 1)
|
||||
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
|
||||
try:
|
||||
await conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
async with self._lock:
|
||||
self._idle.append((conn.host, conn))
|
||||
@@ -0,0 +1,61 @@
|
||||
"""健康分引擎:失败乘性×0.5 / 成功加性+0.2 / 连续失败冷却 / EWMA延迟"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
class HealthEngine:
|
||||
"""服务器健康状态管理(语义参照 easy-tdx 已验证实现)。"""
|
||||
|
||||
COOLDOWN_SECONDS = 120.0
|
||||
FAIL_PENALTY = 0.5
|
||||
SUCCESS_BONUS = 0.2
|
||||
FAIL_STREAK_COOLDOWN = 1 # 任意一次通信失败即冷却,避免坏服务器被反复选中
|
||||
EWMA_ALPHA = 0.3
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._score: dict[str, float] = defaultdict(lambda: 1.0)
|
||||
self._fail_streak: dict[str, int] = defaultdict(int)
|
||||
self._cooldown_until: dict[str, float] = {}
|
||||
self._latency: dict[str, float] = {} # EWMA延迟(秒)
|
||||
|
||||
def record_success(self, server: str, latency: float | None = None) -> None:
|
||||
self._score[server] = min(1.0, self._score[server] + self.SUCCESS_BONUS)
|
||||
self._fail_streak[server] = 0
|
||||
self._cooldown_until.pop(server, None)
|
||||
if latency is not None:
|
||||
self.observe_latency(server, latency)
|
||||
|
||||
def record_failure(self, server: str) -> None:
|
||||
self._score[server] = max(0.05, self._score[server] * self.FAIL_PENALTY)
|
||||
self._fail_streak[server] += 1
|
||||
if self._fail_streak[server] >= self.FAIL_STREAK_COOLDOWN:
|
||||
self._cooldown_until[server] = time.monotonic() + self.COOLDOWN_SECONDS
|
||||
|
||||
def observe_latency(self, server: str, latency: float) -> None:
|
||||
old = self._latency.get(server)
|
||||
self._latency[server] = (
|
||||
latency if old is None else self.EWMA_ALPHA * latency + (1 - self.EWMA_ALPHA) * old
|
||||
)
|
||||
|
||||
def score(self, server: str) -> float:
|
||||
return self._score.get(server, 1.0)
|
||||
|
||||
def latency(self, server: str) -> float:
|
||||
return self._latency.get(server, float("inf"))
|
||||
|
||||
def in_cooldown(self, server: str) -> bool:
|
||||
return time.monotonic() < self._cooldown_until.get(server, 0.0)
|
||||
|
||||
def effective_latency(self, server: str) -> float:
|
||||
"""排序键 = 延迟/健康分(越高越差)。"""
|
||||
return self.latency(server) / self.score(server)
|
||||
|
||||
def rank(self, candidates: list[str]) -> list[str]:
|
||||
"""按 effective_latency 升序排序,冷却中的服务器排最后。"""
|
||||
ok = [c for c in candidates if not self.in_cooldown(c)]
|
||||
bad = [c for c in candidates if self.in_cooldown(c)]
|
||||
ok.sort(key=self.effective_latency)
|
||||
bad.sort(key=self.effective_latency)
|
||||
return ok + bad
|
||||
@@ -0,0 +1,328 @@
|
||||
"""异步动态连接池(标准 / MAC 通道 7709)。
|
||||
|
||||
设计目标:
|
||||
- 高并发:池内连接数 = 服务器数 × per_server_cap,多服务器并行请求。
|
||||
- 自愈:坏连接自动丢弃重建;服务器故障时自动切换到下一个可用服务器。
|
||||
- 动态:支持运行时 add() 新服务器;支持后台全量测速入库(当日首次启动)。
|
||||
- 单连接约束(核心需求):per_server_cap 默认 1 —— 一台服务器只允许建立 1 个连接,
|
||||
不允许多开,避免对单一券商服务器造成并发压力被限流/封禁。
|
||||
- 后台测速(核心需求):当日首次启动时,在后台对全量候选服务器测速,按
|
||||
「排名前 FAST_POOL_TOP_N 或 延迟 < FAST_POOL_MAX_LATENCY」筛选入库,结果写回 servers.json。
|
||||
- 弹性:空闲连接超时后由 reaper 缩容回 min_size;支持 stats 统计。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import random
|
||||
import time
|
||||
|
||||
from .connection import TdxConnection
|
||||
from .health import HealthEngine
|
||||
from .probe_cache import get_probe_cache
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_PORT = 7709
|
||||
|
||||
|
||||
class PoolTimeoutError(Exception):
|
||||
"""连接池在限定重试内无法获取可用连接。"""
|
||||
|
||||
|
||||
class _Stats:
|
||||
def __init__(self) -> None:
|
||||
self.borrowed = 0
|
||||
self.total = 0
|
||||
self.created = 0
|
||||
self.dropped = 0
|
||||
self.reaped = 0
|
||||
|
||||
|
||||
class PooledConnection:
|
||||
"""池化连接包装:记录所属 host,转发 execute / close。
|
||||
|
||||
server 是 host 的别名(兼容历史调用)。
|
||||
"""
|
||||
|
||||
__slots__ = ("conn", "host", "server", "last_used", "alive")
|
||||
|
||||
def __init__(self, conn, host: str) -> None:
|
||||
self.conn = conn
|
||||
self.host = host
|
||||
self.server = host # 别名
|
||||
self.last_used = time.monotonic()
|
||||
self.alive = True
|
||||
|
||||
async def execute(self, cmd):
|
||||
self.last_used = time.monotonic()
|
||||
return await self.conn.execute(cmd)
|
||||
|
||||
async def close(self) -> None:
|
||||
self.alive = False
|
||||
try:
|
||||
await self.conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
class DynamicPool:
|
||||
"""动态连接池:按服务器维度统计连接数,每台服务器最多 per_server_cap 个连接。"""
|
||||
|
||||
def __init__(self, servers: list[str], *, port: int = DEFAULT_PORT,
|
||||
timeout: float = 10.0, min_size: int = 4, max_size: int = 12,
|
||||
per_server_cap: int = 1,
|
||||
health: HealthEngine | None = None,
|
||||
probe_factory=None,
|
||||
idle_ttl: float = 30.0,
|
||||
scale_down_grace: float = 5.0,
|
||||
reaper_interval: float = 5.0,
|
||||
auto_probe: bool = True) -> None:
|
||||
self.servers = list(dict.fromkeys(servers)) # 去重保序
|
||||
self.port = port
|
||||
self.timeout = timeout
|
||||
self.min_size = max(1, min_size)
|
||||
self.max_size = max(self.min_size, max_size)
|
||||
self.per_server_cap = max(1, per_server_cap) # 一台服务器最多几个连接(默认1=单连接)
|
||||
self.health = health or HealthEngine()
|
||||
self.probe_factory = probe_factory # MAC通道用真实命令探测可用性
|
||||
self.idle_ttl = idle_ttl
|
||||
self.scale_down_grace = scale_down_grace
|
||||
self.reaper_interval = reaper_interval
|
||||
self.auto_probe = auto_probe
|
||||
|
||||
self.stats = _Stats()
|
||||
self._idle: list[PooledConnection] = []
|
||||
self._conns: set[PooledConnection] = set()
|
||||
self._server_count: dict[str, int] = {} # 每服务器已建立连接数
|
||||
self._total = 0
|
||||
self._closed = False
|
||||
self._started = False
|
||||
self._lock = asyncio.Lock()
|
||||
self._cond = asyncio.Condition(self._lock)
|
||||
self._reaper_task: asyncio.Task | None = None
|
||||
|
||||
# ---------- 生命周期 ----------
|
||||
async def start(self) -> None:
|
||||
if self._started:
|
||||
return
|
||||
self._started = True
|
||||
|
||||
# 需求①:当日首次启动 → 后台全量测速入库(不阻塞主流程)
|
||||
if self.auto_probe:
|
||||
hosts = list(self.servers)
|
||||
asyncio.create_task(
|
||||
get_probe_cache().ensure_probe(hosts),
|
||||
name="pytdxdata-daily-probe",
|
||||
)
|
||||
|
||||
targets = self._pick_servers(self.min_size)
|
||||
for host in targets:
|
||||
if self._total >= self.max_size:
|
||||
break
|
||||
try:
|
||||
pc = await self._new_conn(host)
|
||||
self._idle.append(pc)
|
||||
self._total += 1
|
||||
except Exception as e:
|
||||
log.warning("初始连接失败 %s: %s", host, str(e)[:60])
|
||||
|
||||
if self._total == 0:
|
||||
log.warning("连接池初始连接全部失败,将在使用时按需重试")
|
||||
|
||||
# 启动 reaper:空闲连接超时后缩容回 min_size
|
||||
try:
|
||||
self._reaper_task = asyncio.create_task(self._reaper_loop())
|
||||
except RuntimeError:
|
||||
pass # 无事件循环时跳过
|
||||
|
||||
def _pick_servers(self, n: int) -> list[str]:
|
||||
ranked = self.health.rank(self.servers)
|
||||
out: list[str] = []
|
||||
counts = dict(self._server_count)
|
||||
for h in ranked:
|
||||
if len(out) >= n:
|
||||
break
|
||||
if counts.get(h, 0) >= self.per_server_cap:
|
||||
continue
|
||||
out.append(h)
|
||||
counts[h] = counts.get(h, 0) + 1
|
||||
if len(out) < n: # 兜底:服务器不足时允许突破 cap
|
||||
for h in ranked:
|
||||
if len(out) >= n:
|
||||
break
|
||||
out.append(h)
|
||||
return out
|
||||
|
||||
async def _new_conn(self, host: str) -> PooledConnection:
|
||||
conn = TdxConnection(host, self.port, self.timeout)
|
||||
await conn.connect()
|
||||
if self.probe_factory is not None:
|
||||
try:
|
||||
await conn.execute(self.probe_factory())
|
||||
except Exception:
|
||||
await conn.close()
|
||||
raise
|
||||
pc = PooledConnection(conn, host)
|
||||
self._conns.add(pc)
|
||||
self._server_count[host] = self._server_count.get(host, 0) + 1
|
||||
self.stats.created += 1
|
||||
self.stats.total = self._total + 1
|
||||
return pc
|
||||
|
||||
async def close(self) -> None:
|
||||
self._closed = True
|
||||
if self._reaper_task is not None:
|
||||
self._reaper_task.cancel()
|
||||
self._reaper_task = None
|
||||
async with self._lock:
|
||||
for pc in list(self._conns):
|
||||
try:
|
||||
await pc.close()
|
||||
except Exception:
|
||||
pass
|
||||
self._idle.clear()
|
||||
self._conns.clear()
|
||||
self._total = 0
|
||||
self._server_count.clear()
|
||||
self.stats.total = 0
|
||||
|
||||
# ---------- 借还 ----------
|
||||
async def acquire(self, timeout: float | None = 30.0) -> PooledConnection:
|
||||
"""借出一条连接。
|
||||
|
||||
单连接约束下,池大小 = 服务器数 × per_server_cap。当并发请求超过池大小时,
|
||||
超额协程在此**排队等待**(基于 Condition),待其他协程 release 后被唤醒,
|
||||
而非立即超时放弃。默认 timeout=30s 防止在坏服务器场景下永久挂起。
|
||||
"""
|
||||
deadline = None if timeout is None else (time.monotonic() + timeout)
|
||||
async with self._cond:
|
||||
while True:
|
||||
now = time.monotonic()
|
||||
# 1. 优先借用空闲连接(跳过冷却中的服务器,避免反复选中坏节点)
|
||||
while self._idle:
|
||||
pc = self._idle.pop(0)
|
||||
fresh = now - pc.last_used < self.idle_ttl
|
||||
if pc.alive and fresh and not self.health.in_cooldown(pc.host):
|
||||
self.stats.borrowed += 1
|
||||
return pc
|
||||
self._drop_locked(pc)
|
||||
|
||||
# 2. 可扩容(未达上限且有可用服务器)→ 新建连接
|
||||
if self._total < self.max_size:
|
||||
host = self._next_available_server()
|
||||
if host:
|
||||
try:
|
||||
pc = await self._new_conn(host)
|
||||
self._total += 1
|
||||
self.stats.borrowed += 1
|
||||
return pc
|
||||
except Exception as e:
|
||||
log.warning("扩容连接失败 %s: %s", host, str(e)[:60])
|
||||
self.health.record_failure(host)
|
||||
# 循环重试:可能换下一台服务器
|
||||
continue
|
||||
|
||||
# 3. 池满:排队等待 release 唤醒(或超时)
|
||||
if deadline is not None and now >= deadline:
|
||||
raise PoolTimeoutError("连接池获取超时(连接数已达上限且无空闲)")
|
||||
try:
|
||||
if deadline is None:
|
||||
await self._cond.wait()
|
||||
else:
|
||||
await asyncio.wait_for(self._cond.wait(), timeout=deadline - now)
|
||||
except asyncio.TimeoutError:
|
||||
raise PoolTimeoutError("连接池获取超时(连接数已达上限且无空闲)")
|
||||
|
||||
async def acquire_many(self, k: int) -> list[PooledConnection]:
|
||||
"""批量借出 k 个连接(分页并发用),超额请求排队等待不丢。"""
|
||||
out: list[PooledConnection] = []
|
||||
for _ in range(k):
|
||||
try:
|
||||
out.append(await self.acquire())
|
||||
except PoolTimeoutError:
|
||||
break
|
||||
return out
|
||||
|
||||
def _next_available_server(self) -> str | None:
|
||||
ranked = self.health.rank(self.servers)
|
||||
# 1. 优先选未冷却且未达并发上限的服务器
|
||||
for h in ranked:
|
||||
if self.health.in_cooldown(h):
|
||||
continue
|
||||
if self._server_count.get(h, 0) < self.per_server_cap:
|
||||
return h
|
||||
# 2. 兜底:仅从「未冷却」的已占用服务器里随机复用(避免反复打坏服务器)
|
||||
alive = [
|
||||
h for h in ranked
|
||||
if not self.health.in_cooldown(h) and self._server_count.get(h, 0) > 0
|
||||
]
|
||||
return random.choice(alive) if alive else None
|
||||
|
||||
async def release(self, pc: PooledConnection, *, broken: bool = False) -> None:
|
||||
if self.stats.borrowed > 0:
|
||||
self.stats.borrowed -= 1
|
||||
# self._cond 的底层锁即 self._lock,统一用 _cond 加锁避免重复加锁死锁
|
||||
async with self._cond:
|
||||
if broken or not pc.alive:
|
||||
self.health.record_failure(pc.host)
|
||||
self._drop_locked(pc)
|
||||
else:
|
||||
self.health.record_success(pc.host)
|
||||
if not self._closed and pc.alive:
|
||||
self._idle.append(pc)
|
||||
self._cond.notify_all()
|
||||
|
||||
def _drop_locked(self, pc: PooledConnection) -> None:
|
||||
self._conns.discard(pc)
|
||||
self._total = max(0, self._total - 1)
|
||||
self.stats.total = self._total
|
||||
if self._server_count.get(pc.host, 0) > 0:
|
||||
self._server_count[pc.host] -= 1
|
||||
self._idle = [c for c in self._idle if c is not pc]
|
||||
self.stats.dropped += 1
|
||||
asyncio.create_task(self._safe_close(pc))
|
||||
|
||||
@staticmethod
|
||||
async def _safe_close(pc: PooledConnection) -> None:
|
||||
try:
|
||||
await pc.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# ---------- reaper(空闲缩容) ----------
|
||||
async def _reaper_loop(self) -> None:
|
||||
try:
|
||||
while not self._closed:
|
||||
await asyncio.sleep(self.reaper_interval)
|
||||
async with self._lock:
|
||||
now = time.monotonic()
|
||||
kept = []
|
||||
while self._idle:
|
||||
pc = self._idle.pop(0)
|
||||
if (now - pc.last_used > self.idle_ttl
|
||||
and self._total > self.min_size):
|
||||
self._drop_locked(pc)
|
||||
self.stats.reaped += 1
|
||||
else:
|
||||
kept.append(pc)
|
||||
self._idle = kept
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
# ---------- 动态管理 ----------
|
||||
def add(self, host: str) -> None:
|
||||
if host not in self.servers:
|
||||
self.servers.append(host)
|
||||
|
||||
def remove(self, host: str) -> None:
|
||||
if host in self.servers:
|
||||
self.servers.remove(host)
|
||||
|
||||
@property
|
||||
def size(self) -> int:
|
||||
return self._total
|
||||
|
||||
@property
|
||||
def idle_count(self) -> int:
|
||||
return len(self._idle)
|
||||
@@ -0,0 +1,162 @@
|
||||
"""当日首次启动的全量服务器测速与入库缓存。
|
||||
|
||||
设计要点:
|
||||
- 仅在"当日首次启动"时触发一次后台全量测速(按本地日期判断是否已探测)。
|
||||
- 测速采用真实握手连通(TdxConnection.test),比单纯 ping 更能反映通达信可用性。
|
||||
- 入库规则:延迟排名前 FAST_POOL_TOP_N(10) **或** 延迟 < FAST_POOL_MAX_LATENCY(0.2s) 的服务器
|
||||
才标记为 status="ok" 并写入 servers.json,其余降权(保留但不优先)。
|
||||
- 结果持久化到 servers.json,供 config / 连接池直接使用。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
from ..config import _SERVERS_FILE
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# 入池阈值
|
||||
FAST_POOL_TOP_N = 10 # 延迟排名前 N
|
||||
FAST_POOL_MAX_LATENCY = 0.2 # 延迟 < 200ms(秒)也可入池
|
||||
PROBE_PORT = 7709
|
||||
PROBE_TIMEOUT = 3.0
|
||||
PROBE_CONCURRENCY = 24
|
||||
|
||||
|
||||
class ProbeCache:
|
||||
"""管理当日测速缓存与 servers.json 入库。
|
||||
|
||||
一台服务器不论协议(standard/mac/ex),测速结果共享:
|
||||
只要任一端口可达即视为该主机可用,并以最快端口延迟作为延迟基准。
|
||||
"""
|
||||
|
||||
def __init__(self, servers_file: Path | None = None) -> None:
|
||||
self._file = servers_file or _SERVERS_FILE
|
||||
self._lock = asyncio.Lock()
|
||||
self._last_probe_date: str | None = None
|
||||
self._probed_today = False
|
||||
|
||||
# ---- 公开 API ----
|
||||
def needs_probe_today(self) -> bool:
|
||||
"""是否有必要在今日执行一次全量测速(文件不存在 / 日期不符 / 无 ok 项)。"""
|
||||
if not self._file.exists():
|
||||
return True
|
||||
try:
|
||||
cfg = json.loads(self._file.read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
return True
|
||||
checked = str(cfg.get("checked_date", ""))
|
||||
today = date.today().isoformat()
|
||||
if checked != today:
|
||||
return True
|
||||
servers = cfg.get("servers", [])
|
||||
if not any(s.get("status") == "ok" for s in servers):
|
||||
return True
|
||||
return False
|
||||
|
||||
async def ensure_probe(self, hosts: list[str]) -> None:
|
||||
"""若今日尚未测速,则后台执行一次全量测速并入库(幂等)。"""
|
||||
async with self._lock:
|
||||
if self._probed_today:
|
||||
return
|
||||
if not self.needs_probe_today():
|
||||
self._probed_today = True
|
||||
return
|
||||
self._probed_today = True
|
||||
|
||||
# 在锁外执行(耗时网络 IO),允许并发进入的调用方共享本次结果
|
||||
try:
|
||||
await self._run_full_probe(hosts)
|
||||
except Exception as e: # 测速失败不能阻塞主流程
|
||||
log.warning("后台全量测速失败,沿用既有清单: %s", str(e)[:80])
|
||||
|
||||
# ---- 内部实现 ----
|
||||
async def _run_full_probe(self, hosts: list[str]) -> None:
|
||||
from .connection import TdxConnection
|
||||
|
||||
log.info("开始当日全量服务器测速 (%d 台)...", len(hosts))
|
||||
sem = asyncio.Semaphore(PROBE_CONCURRENCY)
|
||||
results: dict[str, float | None] = {}
|
||||
|
||||
async def _probe_one(host: str) -> None:
|
||||
async with sem:
|
||||
try:
|
||||
# 使用连接自带的 ping():建连+握手+发1条命令测往返延迟
|
||||
conn = TdxConnection(host, PROBE_PORT, PROBE_TIMEOUT)
|
||||
dt = await asyncio.wait_for(conn.ping(), timeout=PROBE_TIMEOUT + 1.0)
|
||||
results[host] = dt # 失败时为 None
|
||||
except Exception:
|
||||
results[host] = None
|
||||
|
||||
await asyncio.gather(*(_probe_one(h) for h in hosts))
|
||||
|
||||
ok_hosts = {h: lt for h, lt in results.items() if lt is not None}
|
||||
# 按延迟升序排名
|
||||
ranked = sorted(ok_hosts.items(), key=lambda kv: kv[1])
|
||||
fast = set()
|
||||
for i, (h, lt) in enumerate(ranked):
|
||||
if i < FAST_POOL_TOP_N or lt < FAST_POOL_MAX_LATENCY:
|
||||
fast.add(h)
|
||||
|
||||
log.info("测速完成: 可用 %d 台, 入池 %d 台 (前%d或<%dms)",
|
||||
len(ok_hosts), len(fast), FAST_POOL_TOP_N,
|
||||
int(FAST_POOL_MAX_LATENCY * 1000))
|
||||
self._write_results(results, fast)
|
||||
|
||||
def _write_results(self, results: dict[str, float | None], fast: set[str]) -> None:
|
||||
try:
|
||||
cfg = json.loads(self._file.read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
cfg = {"servers": []}
|
||||
servers = cfg.get("servers", [])
|
||||
by_ip = {s.get("ip"): s for s in servers}
|
||||
|
||||
today = date.today().isoformat()
|
||||
now = time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
checked_any = False
|
||||
for host, lt in results.items():
|
||||
entry = by_ip.get(host)
|
||||
if entry is None:
|
||||
entry = {"ip": host, "name": host, "protocols": ["standard"]}
|
||||
servers.append(entry)
|
||||
by_ip[host] = entry
|
||||
if lt is not None:
|
||||
checked_any = True
|
||||
entry["status"] = "ok" if host in fast else "slow"
|
||||
entry["latency"] = round(lt, 3)
|
||||
entry["fast_pool"] = host in fast
|
||||
else:
|
||||
# 探测不可达:不降级已有 ok(避免误杀),仅标记本次不可达
|
||||
entry.setdefault("status", "unknown")
|
||||
entry["latency"] = None
|
||||
entry["fast_pool"] = False
|
||||
entry["checked_date"] = today
|
||||
entry["checked_at"] = now
|
||||
|
||||
# 若有 ok 服务器写入可入池日期;否则保留今日以免反复重试
|
||||
cfg["checked_date"] = today
|
||||
cfg["note"] = (
|
||||
f"当日全量测速入库({today}): 入池={len(fast)}台 "
|
||||
f"(排名前{FAST_POOL_TOP_N}或延迟<{int(FAST_POOL_MAX_LATENCY*1000)}ms)"
|
||||
)
|
||||
if not checked_any:
|
||||
# 全部不可达时不写 checked_date,下次启动重试
|
||||
cfg.pop("checked_date", None)
|
||||
|
||||
self._file.write_text(
|
||||
json.dumps(cfg, ensure_ascii=False, indent=2), encoding="utf-8"
|
||||
)
|
||||
log.info("测速结果已写入 %s", self._file)
|
||||
|
||||
|
||||
# 进程级单例(跨池共享,确保当日仅测一次)
|
||||
_probe_cache = ProbeCache()
|
||||
|
||||
|
||||
def get_probe_cache() -> ProbeCache:
|
||||
return _probe_cache
|
||||
Whitespace-only changes.
@@ -0,0 +1,32 @@
|
||||
"""命令注册表:命令类型 → (命令类, 通道, 页大小) 元数据"""
|
||||
from __future__ import annotations
|
||||
|
||||
from .base import BaseCommand, TdxDecodeError, build_standard_request
|
||||
from .finance import GetFinanceInfoCmd
|
||||
from .kline import GetIndexBarsCmd, GetSecurityBarsCmd, PAGE_SIZE as KLINE_PAGE
|
||||
from .mac import MacKlineCmd, MAC_KLINE_PAGE
|
||||
from .minute import GetHistoryMinuteTimeDataCmd
|
||||
from .quotes import GetSecurityQuotesCmd, MAX_QUOTES
|
||||
from .security_list import GetSecurityCountCmd, GetSecurityListCmd, PAGE_SIZE as LIST_PAGE
|
||||
from .transaction import (
|
||||
GetHistoryTransactionDataCmd,
|
||||
GetTransactionDataCmd,
|
||||
MacTransactionCmd,
|
||||
MAC_PAGE_SIZE,
|
||||
STD_PAGE_SIZE,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"BaseCommand", "TdxDecodeError", "build_standard_request",
|
||||
"GetSecurityBarsCmd", "GetIndexBarsCmd", "KLINE_PAGE",
|
||||
"GetSecurityQuotesCmd", "MAX_QUOTES",
|
||||
"GetSecurityCountCmd", "GetSecurityListCmd", "LIST_PAGE",
|
||||
"GetTransactionDataCmd", "GetHistoryTransactionDataCmd", "MacTransactionCmd",
|
||||
"STD_PAGE_SIZE", "MAC_PAGE_SIZE",
|
||||
"GetHistoryMinuteTimeDataCmd",
|
||||
"GetXdxrInfoCmd", "GetFinanceInfoCmd",
|
||||
"MacKlineCmd", "MAC_KLINE_PAGE",
|
||||
]
|
||||
|
||||
# 循环依赖规避:xdxr 需要在此导入
|
||||
from .xdxr import GetXdxrInfoCmd # noqa: E402
|
||||
@@ -0,0 +1,38 @@
|
||||
"""命令基类:请求构造 + 响应解析(不含IO)"""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Generic, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class BaseCommand(ABC, Generic[T]):
|
||||
"""所有行情命令的基类。子类实现 build_request/parse_response。"""
|
||||
|
||||
@abstractmethod
|
||||
def build_request(self) -> bytes:
|
||||
"""构造请求包(含完整帧头)。"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def parse_response(self, body: bytes) -> T:
|
||||
"""解析解压后的响应 body,返回强类型结果。"""
|
||||
...
|
||||
|
||||
|
||||
class TdxDecodeError(Exception):
|
||||
"""协议解析错误(服务器截断/ret_count撒谎等)。"""
|
||||
|
||||
|
||||
def build_standard_request(
|
||||
cmd_id: int,
|
||||
method: int,
|
||||
payload: bytes,
|
||||
seq_id: int = 0x01016408,
|
||||
) -> bytes:
|
||||
"""构造标准通道请求帧:12字节头 + payload。"""
|
||||
import struct
|
||||
total = 12 + len(payload)
|
||||
header = struct.pack("<HIHHHH", cmd_id, seq_id, total, total, method, 0)
|
||||
return header + payload
|
||||
@@ -0,0 +1,171 @@
|
||||
"""EX扩展市场命令:商品列表/数量/逐笔(走EX服务器7727,12B EX帧头)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
from typing import Any
|
||||
|
||||
from ..._binary import slice_bytes
|
||||
from .base import BaseCommand
|
||||
from ..._binary import unpack_from
|
||||
|
||||
|
||||
def _ex_frame(cmd_id: int, body: bytes) -> bytes:
|
||||
"""EX类命令帧:01 + 4B customize + 01 + zipsize + unzipsize + cmd(LE)。"""
|
||||
inner_len = 2 + len(body)
|
||||
return struct.pack("<BIBHHH", 0x01, 0, 1, inner_len, inner_len, cmd_id) + body
|
||||
|
||||
|
||||
class GetExInstrumentInfoCmd(BaseCommand[list[dict]]):
|
||||
"""扩展市场商品列表(0x23F5,EX帧,64B/条)。"""
|
||||
|
||||
def __init__(self, market: int, start: int = 0, count: int = 100) -> None:
|
||||
self.market = market
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
return _ex_frame(0x23F5, struct.pack("<IH", self.start, self.count))
|
||||
|
||||
def parse_response(self, body: bytes) -> list[dict]:
|
||||
if len(body) < 6:
|
||||
return []
|
||||
n = len(body) - 6
|
||||
items: list[dict] = []
|
||||
off = 6
|
||||
for _ in range(n // 64):
|
||||
if off + 64 > len(body):
|
||||
break
|
||||
rec = body[off:off + 64]
|
||||
# 前40B: BB3s9s17s9s (category/market/3B/code9/name17/desc9)
|
||||
_, mkt, _, code_b, name_b, desc_b = struct.unpack("<BB3s9s17s9s", rec[:40])
|
||||
items.append({
|
||||
"category": 0,
|
||||
"market": mkt,
|
||||
"code": code_b.rstrip(b"\x00").decode("gbk", errors="replace"),
|
||||
"name": name_b.rstrip(b"\x00").decode("gbk", errors="replace"),
|
||||
"desc": desc_b.rstrip(b"\x00").decode("gbk", errors="replace"),
|
||||
})
|
||||
off += 64
|
||||
return items
|
||||
|
||||
|
||||
class GetExInstrumentCountCmd(BaseCommand[int]):
|
||||
"""扩展市场商品数量(0x23F0,EX帧,body空)。"""
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
return _ex_frame(0x23F0, b"")
|
||||
|
||||
def parse_response(self, body: bytes) -> int:
|
||||
if len(body) < 23:
|
||||
return 0
|
||||
(count,) = unpack_from("<I", body, 19, "ex instrument count")
|
||||
return int(count)
|
||||
|
||||
|
||||
class GetExTransactionDataCmd(BaseCommand[list[dict]]):
|
||||
"""扩展市场(港股)当日逐笔(0x23FC,EX帧,16B/条)。"""
|
||||
|
||||
def __init__(self, market: int, code: str, start: int = 0, count: int = 1800) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<B9siH", self.market, self.code.encode("utf-8"), self.start, self.count)
|
||||
return _ex_frame(0x23FC, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[dict]:
|
||||
if len(body) < 16:
|
||||
return []
|
||||
(_, _, _, num) = struct.unpack("<B9s4sH", body[:16])
|
||||
items: list[dict] = []
|
||||
off = 16
|
||||
for _ in range(num):
|
||||
if off + 16 > len(body):
|
||||
break
|
||||
minutes, price_int, vol, zengcang, direction = struct.unpack(
|
||||
"<HIIiH", body[off:off + 16])
|
||||
items.append({
|
||||
"minutes": minutes,
|
||||
"price": price_int / 1000.0, # 港股精确到0.001
|
||||
"vol": vol,
|
||||
"zengcang": zengcang,
|
||||
"direction": direction,
|
||||
})
|
||||
off += 16
|
||||
return items
|
||||
|
||||
|
||||
class GetExHistoryTransactionDataCmd(BaseCommand[list[dict]]):
|
||||
"""扩展市场(港股)历史逐笔(0x2406,EX帧)。"""
|
||||
|
||||
def __init__(self, ymd: int, market: int, code: str,
|
||||
start: int = 0, count: int = 1800) -> None:
|
||||
self.ymd = ymd
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<IB9siH", self.ymd, self.market,
|
||||
self.code.encode("utf-8"), self.start, self.count)
|
||||
return _ex_frame(0x2406, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[dict]:
|
||||
if len(body) < 16:
|
||||
return []
|
||||
(_, _, _, num) = struct.unpack("<B9s4sH", body[:16])
|
||||
items: list[dict] = []
|
||||
off = 16
|
||||
for _ in range(num):
|
||||
if off + 16 > len(body):
|
||||
break
|
||||
minutes, price_int, vol, zengcang, direction = struct.unpack(
|
||||
"<HIIiH", body[off:off + 16])
|
||||
items.append({
|
||||
"minutes": minutes,
|
||||
"price": price_int / 1000.0,
|
||||
"vol": vol,
|
||||
"zengcang": zengcang,
|
||||
"direction": direction,
|
||||
})
|
||||
off += 16
|
||||
return items
|
||||
|
||||
|
||||
class SymbolTickChartCmd(BaseCommand[list[dict]]):
|
||||
"""分时逐笔tick图(0x122D,MAC帧走EX通道,18B/tick)。"""
|
||||
|
||||
def __init__(self, market: int, code: str, ymd: int = 0) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.ymd = ymd
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
from .mac_ext import build_mac_request
|
||||
raw = self.code.encode("gbk")
|
||||
padded = (raw + b"\x00" * 22)[:22]
|
||||
body = struct.pack("<H22sI5H", self.market, padded, self.ymd, 1, 0, 0, 0, 0)
|
||||
return build_mac_request(0x122D, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[dict]:
|
||||
if len(body) < 35:
|
||||
return []
|
||||
(_, _, _, _, ref_price, count) = struct.unpack("<H22sIBfH", body[:35])
|
||||
items: list[dict] = []
|
||||
off = 35
|
||||
for _ in range(count):
|
||||
if off + 18 > len(body):
|
||||
break
|
||||
minutes, price, avg, vol, momentum = struct.unpack("<HffIf", body[off:off + 18])
|
||||
items.append({
|
||||
"minutes": minutes,
|
||||
"price": price,
|
||||
"avg": avg,
|
||||
"vol": vol,
|
||||
"momentum": momentum,
|
||||
})
|
||||
off += 18
|
||||
return items
|
||||
@@ -0,0 +1,125 @@
|
||||
"""pytdx EX扩展行情协议(setup握手 + 0x23xx命令,服务116.205.143.214等扩展市场服务器)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import unpack_from
|
||||
from ..datetime_ import get_datetime
|
||||
from .base import BaseCommand
|
||||
|
||||
# pytdx EX setup 握手命令(完整84B: 12B帧头+72B body, 0x2454)—— 与MAC EX login同ID不同body
|
||||
EX_SETUP_CMD = bytes.fromhex("0101486500015200520054241f32c6e5d53dfb411f32c6e5d53dfb411f32c6e5d53dfb411f32c6e5d53dfb411f32c6e5d53dfb411f32c6e5d53dfb411f32c6e5d53dfb411f32c6e5d53dfb41cce16dffd5ba3fb8cbc57a054f7748ea")
|
||||
|
||||
|
||||
def _ex_frame(cmd_id: int, body: bytes) -> bytes:
|
||||
"""EX类命令帧:01 + 4B customize + 01 + zipsize + unzipsize + cmd(LE)。"""
|
||||
inner_len = 2 + len(body)
|
||||
return struct.pack("<BIBHHH", 0x01, 0, 1, inner_len, inner_len, cmd_id) + body
|
||||
|
||||
|
||||
class ExSetupCmd(BaseCommand[bool]):
|
||||
"""pytdx EX 握手(完整84B原样发送, 0x2454)。"""
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
return EX_SETUP_CMD
|
||||
|
||||
def parse_response(self, body: bytes) -> bool:
|
||||
return len(body) >= 2
|
||||
|
||||
|
||||
class GetMarketsCmd(BaseCommand[list[dict]]):
|
||||
"""EX市场列表(0x23F4,请求无body)。"""
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
return _ex_frame(0x23F4, b"")
|
||||
|
||||
def parse_response(self, body: bytes) -> list[dict]:
|
||||
if len(body) < 2:
|
||||
return []
|
||||
(cnt,) = struct.unpack("<H", body[:2])
|
||||
items: list[dict] = []
|
||||
off = 2
|
||||
for _ in range(cnt):
|
||||
if off + 64 > len(body):
|
||||
break
|
||||
category, name_b, market, short_b, _, _ = struct.unpack(
|
||||
"<B32sB2s26s2s", body[off:off + 64])
|
||||
off += 64
|
||||
if category == 0 and market == 0:
|
||||
continue
|
||||
items.append({
|
||||
"market": market,
|
||||
"category": category,
|
||||
"name": name_b.rstrip(b"\x00").decode("gbk", errors="replace"),
|
||||
"short_name": short_b.rstrip(b"\x00").decode("gbk", errors="replace"),
|
||||
})
|
||||
return items
|
||||
|
||||
|
||||
class GetExInstrumentBarsCmd(BaseCommand[list[dict]]):
|
||||
"""EX商品K线(0x23FF, 32B/条: 4B时间戳+28B行情)。"""
|
||||
|
||||
def __init__(self, market: int, code: str, category: int = 0,
|
||||
start: int = 0, count: int = 700) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.category = category
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<B9sHHIH", self.market, self.code.encode("utf-8"),
|
||||
self.category, 1, self.start, self.count)
|
||||
return _ex_frame(0x23FF, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[dict]:
|
||||
if len(body) < 20:
|
||||
return []
|
||||
(ret_count,) = unpack_from("<H", body, 18, "ex bars count")
|
||||
items: list[dict] = []
|
||||
off = 20
|
||||
for _ in range(ret_count):
|
||||
if off + 32 > len(body):
|
||||
break
|
||||
# 时间4字节按category分格式:分钟级=2B zipday+2B tminutes;日线及以上=YYYYMMDD
|
||||
year, month, day, hour, minute, _ = get_datetime(self.category, body, off)
|
||||
open_, high, low, close, position, trade, _ = struct.unpack(
|
||||
"<ffffIIf", body[off + 4: off + 32])
|
||||
items.append({
|
||||
"year": year, "month": month, "day": day,
|
||||
"hour": hour, "minute": minute,
|
||||
"open": open_, "high": high, "low": low, "close": close,
|
||||
"position": position, "trade": trade,
|
||||
})
|
||||
off += 32
|
||||
return items
|
||||
|
||||
|
||||
class GetExInstrumentQuoteCmd(BaseCommand[dict | None]):
|
||||
"""EX商品报价(0x23FA, 150B行情)。"""
|
||||
|
||||
def __init__(self, market: int, code: str) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
return _ex_frame(0x23FA, struct.pack("<B9s", self.market, self.code.encode("utf-8")))
|
||||
|
||||
def parse_response(self, body: bytes) -> dict | None:
|
||||
if len(body) < 150:
|
||||
return None
|
||||
# <B9s + 4B未知 + 136B: pre_close/open/high/low/price(5f) + 9I(kaicang,_unk1,zongliang,xianliang,...) + 五档
|
||||
off = 14
|
||||
fields = struct.unpack("<5f", body[off:off + 20])
|
||||
off += 20
|
||||
ints = struct.unpack("<9I", body[off:off + 36])
|
||||
off += 36
|
||||
zongliang, xianliang = ints[2], ints[3]
|
||||
price = fields[4]
|
||||
return {
|
||||
"market": self.market,
|
||||
"code": self.code,
|
||||
"pre_close": fields[0], "open": fields[1],
|
||||
"high": fields[2], "low": fields[3], "price": price,
|
||||
"zongliang": zongliang, "xianliang": xianliang,
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
"""财务快照命令(标准通道 0x1F1876,35字段固定struct)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import slice_bytes, unpack_from
|
||||
from ...models import FinanceRecord
|
||||
from .base import BaseCommand
|
||||
|
||||
_FIN_FMT = "<fHHII" + "f" * 30
|
||||
_FIN_SIZE = struct.calcsize(_FIN_FMT)
|
||||
_SCALE = 10000.0 # 单位:万元/万股
|
||||
|
||||
|
||||
class GetFinanceInfoCmd(BaseCommand[FinanceRecord | None]):
|
||||
"""获取单只股票最新财务数据。"""
|
||||
|
||||
def __init__(self, market: int, code: str) -> None:
|
||||
self.market = market
|
||||
self.code = code.encode("utf-8")
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
header = bytes.fromhex("0c1f187600010b000b0010000100".replace(" ", ""))
|
||||
return header + struct.pack("<B6s", self.market, self.code)
|
||||
|
||||
def parse_response(self, body: bytes) -> FinanceRecord | None:
|
||||
pos = 2
|
||||
market_b, code_b = unpack_from("<B6s", body, pos, "finance header")
|
||||
pos += 7
|
||||
if pos + _FIN_SIZE > len(body):
|
||||
return None
|
||||
fields = struct.unpack(_FIN_FMT, slice_bytes(body, pos, _FIN_SIZE, "finance body"))
|
||||
(
|
||||
liutong_guben, province, industry, updated_date, ipo_date,
|
||||
zong_guben, guojia_gu, faqiren_faren_gu, faren_gu, b_gu,
|
||||
h_gu, zhigong_gu, zong_zichan, liudong_zichan, guding_zichan,
|
||||
wuxing_zichan, gudong_renshu, liudong_fuzhai, changqi_fuzhai, ziben_gongjijin,
|
||||
jing_zichan, zhuying_shouru, zhuying_lirun, yingshou_zhangkuan, yingye_lirun,
|
||||
touzi_shouyu, jingying_xianjinliu, zong_xianjinliu, cunhuo, lirun_zonghe,
|
||||
shuihou_lirun, jing_lirun, weifen_lirun, meigujing_zichan, _reserve2,
|
||||
) = fields
|
||||
return FinanceRecord(
|
||||
market=market_b, code=code_b.decode("utf-8").rstrip("\x00"),
|
||||
liutong_guben=liutong_guben * _SCALE, zong_guben=zong_guben * _SCALE,
|
||||
guojia_gu=guojia_gu * _SCALE, faqiren_faren_gu=faqiren_faren_gu * _SCALE,
|
||||
faren_gu=faren_gu * _SCALE, b_gu=b_gu * _SCALE, h_gu=h_gu * _SCALE,
|
||||
zhigong_gu=zhigong_gu * _SCALE,
|
||||
province=int(province), industry=int(industry),
|
||||
updated_date=int(updated_date), ipo_date=int(ipo_date),
|
||||
gudong_renshu=gudong_renshu,
|
||||
zong_zichan=zong_zichan * _SCALE, liudong_zichan=liudong_zichan * _SCALE,
|
||||
guding_zichan=guding_zichan * _SCALE, wuxing_zichan=wuxing_zichan * _SCALE,
|
||||
liudong_fuzhai=liudong_fuzhai * _SCALE, changqi_fuzhai=changqi_fuzhai * _SCALE,
|
||||
ziben_gongjijin=ziben_gongjijin * _SCALE,
|
||||
jing_zichan=jing_zichan * _SCALE, zhuying_shouru=zhuying_shouru * _SCALE,
|
||||
zhuying_lirun=zhuying_lirun * _SCALE, yingshou_zhangkuan=yingshou_zhangkuan * _SCALE,
|
||||
yingye_lirun=yingye_lirun * _SCALE, touzi_shouyu=touzi_shouyu * _SCALE,
|
||||
jingying_xianjinliu=jingying_xianjinliu * _SCALE,
|
||||
zong_xianjinliu=zong_xianjinliu * _SCALE,
|
||||
cunhuo=cunhuo * _SCALE, lirun_zonghe=lirun_zonghe * _SCALE,
|
||||
shuihou_lirun=shuihou_lirun * _SCALE, jing_lirun=jing_lirun * _SCALE,
|
||||
weifen_lirun=weifen_lirun * _SCALE, meigujing_zichan=meigujing_zichan,
|
||||
reserve2=_reserve2,
|
||||
)
|
||||
@@ -0,0 +1,115 @@
|
||||
"""K线命令(标准通道 0x010C,含差分还原解析;指数K线多4字节跳过)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
from typing import Any
|
||||
|
||||
from ..._binary import unpack_from
|
||||
from ...models import IndexBar, KlinePeriod, SecurityBar
|
||||
from ..datetime_ import get_datetime
|
||||
from ..price import get_price
|
||||
from ..volume import get_volume
|
||||
from .base import BaseCommand, TdxDecodeError
|
||||
|
||||
PAGE_SIZE = 800
|
||||
|
||||
|
||||
class GetSecurityBarsCmd(BaseCommand[list[SecurityBar]]):
|
||||
"""获取指定股票K线(分页,最多800/次)。"""
|
||||
|
||||
def __init__(self, market: int, code: str, category: KlinePeriod,
|
||||
start: int, count: int = 800) -> None:
|
||||
self.market = market
|
||||
self.code = code.encode("utf-8")
|
||||
self.category = int(category)
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
return struct.pack(
|
||||
"<HIHHHH6sHHHHIIH",
|
||||
0x010C, 0x01016408, 0x001C, 0x001C, 0x052D,
|
||||
self.market, self.code, self.category, 1, self.start, self.count, 0, 0, 0,
|
||||
)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[SecurityBar]:
|
||||
(ret_count,) = unpack_from("<H", body, 0, "kline header")
|
||||
pos = 2
|
||||
bars: list[SecurityBar] = []
|
||||
pre_diff_base = 0
|
||||
cat = self.category
|
||||
|
||||
for i in range(ret_count):
|
||||
try:
|
||||
year, month, day, hour, minute, pos = get_datetime(cat, body, pos)
|
||||
open_diff, pos = get_price(body, pos)
|
||||
close_diff, pos = get_price(body, pos)
|
||||
high_diff, pos = get_price(body, pos)
|
||||
low_diff, pos = get_price(body, pos)
|
||||
vol, pos = get_volume(body, pos)
|
||||
amount, pos = get_volume(body, pos)
|
||||
except (ValueError, IndexError) as e:
|
||||
# 服务器截断/ret_count撒谎:返回已解析部分
|
||||
if i == 0 and not bars:
|
||||
raise TdxDecodeError(
|
||||
f"K线响应为空(声称{ret_count}条但首条解析失败:{e})"
|
||||
) from e
|
||||
break
|
||||
|
||||
open_abs = open_diff + pre_diff_base
|
||||
close_abs = open_abs + close_diff
|
||||
high_abs = open_abs + high_diff
|
||||
low_abs = open_abs + low_diff
|
||||
pre_diff_base = open_abs + close_diff
|
||||
|
||||
bars.append(SecurityBar(
|
||||
market=self.market, code=self.code.decode("utf-8"),
|
||||
open=open_abs / 1000.0, close=close_abs / 1000.0,
|
||||
high=high_abs / 1000.0, low=low_abs / 1000.0,
|
||||
vol=vol, amount=amount,
|
||||
year=year, month=month, day=day, hour=hour, minute=minute,
|
||||
))
|
||||
return bars
|
||||
|
||||
|
||||
class GetIndexBarsCmd(GetSecurityBarsCmd):
|
||||
"""指数K线:响应每条 vol+amount 后多4字节(涨/跌家数),需跳过。"""
|
||||
|
||||
def parse_response(self, body: bytes) -> list[IndexBar]:
|
||||
(ret_count,) = unpack_from("<H", body, 0, "index kline header")
|
||||
pos = 2
|
||||
bars: list[IndexBar] = []
|
||||
pre_diff_base = 0
|
||||
cat = self.category
|
||||
|
||||
for i in range(ret_count):
|
||||
try:
|
||||
year, month, day, hour, minute, pos = get_datetime(cat, body, pos)
|
||||
open_diff, pos = get_price(body, pos)
|
||||
close_diff, pos = get_price(body, pos)
|
||||
high_diff, pos = get_price(body, pos)
|
||||
low_diff, pos = get_price(body, pos)
|
||||
vol, pos = get_volume(body, pos)
|
||||
amount, pos = get_volume(body, pos)
|
||||
pos += 4 # 指数额外:上涨家数+下跌家数 uint16×2
|
||||
except (ValueError, IndexError) as e:
|
||||
if i == 0 and not bars:
|
||||
raise TdxDecodeError(
|
||||
f"指数K线响应为空(声称{ret_count}条但首条解析失败:{e})"
|
||||
) from e
|
||||
break
|
||||
|
||||
open_abs = open_diff + pre_diff_base
|
||||
close_abs = open_abs + close_diff
|
||||
high_abs = open_abs + high_diff
|
||||
low_abs = open_abs + low_diff
|
||||
pre_diff_base = open_abs + close_diff
|
||||
|
||||
bars.append(IndexBar(
|
||||
market=self.market, code=self.code.decode("utf-8"),
|
||||
open=open_abs / 1000.0, close=close_abs / 1000.0,
|
||||
high=high_abs / 1000.0, low=low_abs / 1000.0,
|
||||
vol=vol, amount=amount,
|
||||
year=year, month=month, day=day, hour=hour, minute=minute,
|
||||
))
|
||||
return bars
|
||||
@@ -0,0 +1,80 @@
|
||||
"""MAC通道 K线命令(0x122E,支持复权;每条36B: ymd+time+7float)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import unpack_from
|
||||
from ...models import Adjust, KlinePeriod, SecurityBar
|
||||
from ..mac_frame import build_mac_request
|
||||
from .base import BaseCommand
|
||||
|
||||
MAC_KLINE_PAGE = 700
|
||||
|
||||
# MAC Period 与标准 KlineCategory 同值(DAILY=4)
|
||||
_MAC_PERIOD = {
|
||||
KlinePeriod.MIN_5: 0, KlinePeriod.MIN_15: 1, KlinePeriod.MIN_30: 2,
|
||||
KlinePeriod.MIN_60: 3, KlinePeriod.DAY: 4, KlinePeriod.WEEK: 5,
|
||||
KlinePeriod.MONTH: 6, KlinePeriod.MIN_1: 7, KlinePeriod.MIN_3: 8,
|
||||
KlinePeriod.YEAR: 11, KlinePeriod.SEASON: 10,
|
||||
}
|
||||
|
||||
|
||||
class MacKlineCmd(BaseCommand[list[SecurityBar]]):
|
||||
"""MAC通道K线(0x122E)。"""
|
||||
|
||||
def __init__(self, market: int, code: str, period: KlinePeriod, adjust: Adjust,
|
||||
start: int, count: int = 700, times: int = 1) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.period = period
|
||||
self.adjust = adjust
|
||||
self.start = start
|
||||
self.count = count
|
||||
self.times = times
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
# 与 easy-tdx SymbolBarCmd 一致: <H22sHHIHHbbbbH4s
|
||||
body = struct.pack(
|
||||
"<H22sHHIHHbbbbH4s",
|
||||
self.market,
|
||||
self.code.encode("gbk"),
|
||||
_MAC_PERIOD.get(self.period, 4),
|
||||
self.times,
|
||||
self.start,
|
||||
self.count,
|
||||
int(self.adjust),
|
||||
1, 1, 0, 1, 0,
|
||||
b"",
|
||||
)
|
||||
return build_mac_request(0x122E, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[SecurityBar]:
|
||||
# 头部: market(2)+code(22)+category(2)+flag(1)+count(2)+start(4)=33B; count@offset24 <HBHI
|
||||
if len(body) < 33:
|
||||
return []
|
||||
_cat, _flag, count, _start = unpack_from("<HBHI", body, 24, "mac kline header")
|
||||
count = min(count, (len(body) - 33) // 36)
|
||||
if count < 0:
|
||||
count = 0
|
||||
is_intraday = self.period.is_minute
|
||||
bars: list[SecurityBar] = []
|
||||
for i in range(count):
|
||||
offset = 33 + i * 36
|
||||
if offset + 36 > len(body):
|
||||
break
|
||||
ymd, time_num, open_, high, low, close, amount, vol, _fs = unpack_from(
|
||||
"<II7f", body, offset, "mac kline bar")
|
||||
if ymd < 19900101 or ymd > 20991231:
|
||||
continue
|
||||
year, month, day = ymd // 10000, (ymd % 10000) // 100, ymd % 100
|
||||
hour = minute = 15
|
||||
if is_intraday and time_num:
|
||||
hour = time_num // 3600
|
||||
minute = (time_num % 3600) // 60
|
||||
bars.append(SecurityBar(
|
||||
market=self.market, code=self.code,
|
||||
open=open_, close=close, high=high, low=low,
|
||||
vol=vol, amount=amount,
|
||||
year=year, month=month, day=day, hour=hour, minute=minute,
|
||||
))
|
||||
return bars
|
||||
@@ -0,0 +1,508 @@
|
||||
"""MAC 扩展命令:板块/集合竞价/异动/个股快照/扩展市场/服务器信息/资金流"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import slice_bytes, unpack_from
|
||||
from ...models import (
|
||||
AuctionItem, BelongBoard, BoardInfo, CapitalFlow, GoodsItem, MemberQuote,
|
||||
ServerInfo, SymbolSnapshot, UnusualItem,
|
||||
)
|
||||
from ..mac_frame import build_mac_request
|
||||
from .base import BaseCommand
|
||||
|
||||
# 板块成分股常用字段位(FieldBit: 0x00昨收 0x01开 0x02高 0x03低 0x04收 0x05量 0x06量比 0x07额)
|
||||
_MEMBER_BASIC_BITS = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
|
||||
|
||||
|
||||
def _build_bitmap(bits: list[int], size: int = 16) -> bytes:
|
||||
bm = bytearray(size)
|
||||
for b in bits:
|
||||
bm[b // 8] |= 1 << (b % 8)
|
||||
return bytes(bm)
|
||||
|
||||
|
||||
def _field_fmt(bit: int) -> str:
|
||||
"""按位返回解析格式(<f浮点 / <I无符号 / <i有符号)。"""
|
||||
if bit in (0x05, 0x08, 0x09, 0x0A, 0x0B, 0x13, 0x14, 0x15, 0x18, 0x19, 0x1A):
|
||||
return "<I"
|
||||
if bit in (0x2E,):
|
||||
return "<i"
|
||||
return "<f"
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 板块列表 0x1231
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacBoardListCmd(BaseCommand[list[BoardInfo]]):
|
||||
def __init__(self, board_type: int = 0, start: int = 0, page_size: int = 150) -> None:
|
||||
self.board_type = board_type # 0=全部 1=行业 2=概念 ...
|
||||
self.start = start
|
||||
self.page_size = page_size
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<HHBBHH8x", self.page_size, self.board_type, 0, 0, self.start, 1)
|
||||
return build_mac_request(0x1231, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[BoardInfo]:
|
||||
results: list[BoardInfo] = []
|
||||
pos = 4 # 跳过2B count_all + 2B total
|
||||
n = (len(body) - pos) // 160
|
||||
for _ in range(n):
|
||||
# 每160B记录=板块段(80B) + 领涨股段(80B),合并为一条
|
||||
b_seg = slice_bytes(body, pos, 80, "board segment")
|
||||
market, code_b, _pad, name_b, price, rise, pre = struct.unpack("<H6s16s44sfff", b_seg)
|
||||
s_seg = slice_bytes(body, pos + 80, 80, "symbol segment")
|
||||
s_mkt, s_code, _pad2, s_name, s_price, s_rise, s_pre = struct.unpack("<H6s16s44sfff", s_seg)
|
||||
results.append(BoardInfo(
|
||||
market=market,
|
||||
code=code_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
name=name_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
price=price, rise_speed=rise, pre_close=pre,
|
||||
symbol_market=s_mkt,
|
||||
symbol_code=s_code.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
symbol_name=s_name.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
symbol_price=s_price, symbol_rise_speed=s_rise, symbol_pre_close=s_pre,
|
||||
))
|
||||
pos += 160
|
||||
return results
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 板块成分报价 0x122C
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacBoardMembersQuotesCmd(BaseCommand[list[MemberQuote]]):
|
||||
def __init__(self, board_code: int, *, sort_type: int = 0, start: int = 0,
|
||||
page_size: int = 80, bits: list[int] | None = None) -> None:
|
||||
self.board_code = board_code
|
||||
self.sort_type = sort_type
|
||||
self.start = start
|
||||
self.page_size = page_size
|
||||
self.bits = bits if bits is not None else list(_MEMBER_BASIC_BITS)
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
bitmap = _build_bitmap(self.bits)
|
||||
body = (struct.pack("<I9xHIHBB", self.board_code, self.sort_type,
|
||||
self.start, self.page_size, 0, 0)
|
||||
+ bitmap + struct.pack("<BBBB", 0, 0, 0, 1))
|
||||
return build_mac_request(0x122C, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[MemberQuote]:
|
||||
# 响应: 20B响应位图 + <IH total,row_count
|
||||
resp_bitmap = body[:20]
|
||||
pos = 20
|
||||
_total, row_count = unpack_from("<IH", body, pos, "members header")
|
||||
pos += 6
|
||||
results: list[MemberQuote] = []
|
||||
for _ in range(row_count):
|
||||
market, code_b, name_b = unpack_from("<H22s44s", body, pos, "member row")
|
||||
pos += 68
|
||||
fields: dict[str, float] = {}
|
||||
for i in range(160):
|
||||
if resp_bitmap[i // 8] & (1 << (i % 8)):
|
||||
fmt = _field_fmt(i)
|
||||
(v,) = unpack_from(fmt, body, pos, f"member field {i}")
|
||||
fields[hex(i)] = float(v)
|
||||
pos += struct.calcsize(fmt)
|
||||
results.append(MemberQuote(
|
||||
market=market,
|
||||
code=code_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
name=name_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
fields=fields,
|
||||
))
|
||||
return results
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 个股所属板块 0x1218 (head=1)
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacBelongBoardCmd(BaseCommand[list[BelongBoard]]):
|
||||
def __init__(self, market: int, code: str) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<H8s16x21s", self.market, self.code.encode("gbk"), b"Stock_GLHQ")
|
||||
return build_mac_request(0x1218, body, head_flag=1)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[BelongBoard]:
|
||||
# 响应: 27B头 + GBK JSON 数组
|
||||
json_part = body[27:].decode("gbk", errors="replace")
|
||||
results: list[BelongBoard] = []
|
||||
try:
|
||||
import json
|
||||
rows = json.loads(json_part)
|
||||
for row in rows:
|
||||
if len(row) < 6:
|
||||
continue
|
||||
results.append(BelongBoard(
|
||||
market=int(row[1]), board_code=str(row[2]),
|
||||
board_name=str(row[3]),
|
||||
close=float(row[4] or 0), pre_close=float(row[5] or 0),
|
||||
))
|
||||
except Exception:
|
||||
pass
|
||||
return results
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 集合竞价 0x123D
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacAuctionCmd(BaseCommand[list[AuctionItem]]):
|
||||
def __init__(self, market: int, code: str, start: int = 0, count: int = 100) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<H22sII10x", self.market, self.code.encode("gbk"),
|
||||
self.start, self.count)
|
||||
return build_mac_request(0x123D, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[AuctionItem]:
|
||||
(count,) = unpack_from("<I", body, 24, "auction count")
|
||||
results: list[AuctionItem] = []
|
||||
for i in range(count):
|
||||
off = 36 + i * 16
|
||||
try:
|
||||
time_sec, price, matched, unmatched = unpack_from("<IfIi", body, off, "auction item")
|
||||
except ValueError:
|
||||
break
|
||||
h, m, s = time_sec // 3600, time_sec % 3600 // 60, time_sec % 60
|
||||
results.append(AuctionItem(
|
||||
time=f"{h:02d}:{m:02d}:{s:02d}", price=price,
|
||||
matched=matched, unmatched=unmatched,
|
||||
))
|
||||
return results
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 市场异动 0x1237
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacUnusualCmd(BaseCommand[list[UnusualItem]]):
|
||||
def __init__(self, market: int, start: int = 0, count: int = 600) -> None:
|
||||
self.market = market
|
||||
self.start = start
|
||||
self.count = min(count, 600)
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<HH2xH2xH5H", self.market, self.start, self.count,
|
||||
1, 200, 30, 40, 50, 200)
|
||||
return build_mac_request(0x1237, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[UnusualItem]:
|
||||
(count,) = unpack_from("<H", body, 0, "unusual count")
|
||||
results: list[UnusualItem] = []
|
||||
for i in range(count):
|
||||
off = 2 + i * 32
|
||||
if off + 32 > len(body):
|
||||
break
|
||||
# <H6sBBBHH = 7字段: market, code, pad, type, pad, index, z
|
||||
market, code_b, _p1, utype, _p2, _idx, _z = unpack_from(
|
||||
"<H6sBBBHH", body, off, "unusual record")
|
||||
desc, value = self._describe(utype, body[off + 15: off + 28])
|
||||
hour, minute_sec = unpack_from("<BH", body, off + 29, "unusual time")
|
||||
results.append(UnusualItem(
|
||||
market=market,
|
||||
code=code_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
name="", type=utype, desc=desc, value=value,
|
||||
time=f"{hour:02d}:{minute_sec // 100:02d}:{minute_sec % 100:02d}",
|
||||
))
|
||||
# 文本段: 股票名
|
||||
text = body[2 + count * 32:].decode("gbk", errors="ignore").strip(",").split(",")
|
||||
for i, item in enumerate(results):
|
||||
if i < len(text):
|
||||
item.name = text[i]
|
||||
return results
|
||||
|
||||
@staticmethod
|
||||
def _describe(utype: int, data: bytes) -> tuple[str, float]:
|
||||
"""异动类型描述。"""
|
||||
if len(data) < 13:
|
||||
return "", 0.0
|
||||
v1, v2, v3, v4 = struct.unpack("<B2fI", data)
|
||||
names = {
|
||||
0x03: "主力买卖", 0x04: "加速拉升", 0x05: "加速下跌", 0x06: "低位反弹",
|
||||
0x07: "高位回落", 0x08: "撑杆跳高", 0x09: "平台跳水", 0x0A: "单笔冲涨跌",
|
||||
0x0B: "区间放量", 0x0C: "区间缩量", 0x10: "大单托盘", 0x11: "大单压盘",
|
||||
0x12: "大单锁盘", 0x13: "竞价试买", 0x14: "竞价涨跌",
|
||||
}
|
||||
return names.get(utype, str(utype)), float(v2)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 个股特征快照 0x122A
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacSymbolInfoCmd(BaseCommand[SymbolSnapshot]):
|
||||
def __init__(self, market: int, code: str) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<H22sI12x", self.market, self.code.encode("gbk"), 1)
|
||||
return build_mac_request(0x122A, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> SymbolSnapshot:
|
||||
_market, code_b, name_b = unpack_from("<H22s44s", body, 8, "symbol info head")
|
||||
date, t, activity, pre_close, open_, high, low, close, momentum, vol, amount, in_vol, out_vol = \
|
||||
unpack_from("<III5ffIfII", body, 96, "symbol info body")
|
||||
turnover, avg_price = unpack_from("<If", body, 148, "symbol info tail")
|
||||
return SymbolSnapshot(
|
||||
market=self.market,
|
||||
code=code_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
name=name_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
date=date, time=t, activity=activity,
|
||||
pre_close=pre_close, open=open_, high=high, low=low, close=close,
|
||||
vol=vol, amount=amount, turnover=turnover, avg_price=avg_price,
|
||||
)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 扩展市场商品 0x2562
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacGoodsListCmd(BaseCommand[list[GoodsItem]]):
|
||||
def __init__(self, market: int, start: int = 0, count: int = 600) -> None:
|
||||
self.market = market
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<HII", self.market, self.start, self.count)
|
||||
return build_mac_request(0x2562, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[GoodsItem]:
|
||||
(count,) = unpack_from("<H", body, 0, "goods count")
|
||||
results: list[GoodsItem] = []
|
||||
for i in range(count):
|
||||
off = 2 + i * 48
|
||||
try:
|
||||
category, name_b, _u, index, switch, v1, v2, v3, c1, c2 = \
|
||||
unpack_from("<H23sHIBfffHH", body, off, "goods item")
|
||||
except ValueError:
|
||||
break
|
||||
results.append(GoodsItem(
|
||||
category=category,
|
||||
name=name_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
code=v1, switch=switch, index=index,
|
||||
))
|
||||
return results
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 服务器信息 0x120F
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacServerInfoCmd(BaseCommand[ServerInfo]):
|
||||
def build_request(self) -> bytes:
|
||||
body = bytes.fromhex("04002d31" + "00" * 8 + "0027060e" + "00" * 52)
|
||||
return build_mac_request(0x120F, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> ServerInfo:
|
||||
if len(body) < 87:
|
||||
return ServerInfo(date=0, last_trade_date=0)
|
||||
pos = 0
|
||||
pos += 2 # count
|
||||
pos += 8 # flags
|
||||
pos += 3 # tag
|
||||
pos += 9 # reserved
|
||||
(date,) = unpack_from("<I", body, pos, "server date")
|
||||
pos += 4 + 4 # date + ts1
|
||||
sessions: list[tuple[int, int]] = []
|
||||
for _ in range(2):
|
||||
times = unpack_from("<8H", body, pos, "server session")
|
||||
# 每对(H开,H收)是分钟数(570=09:30, 690=11:30)
|
||||
sessions.append((times[0], times[1]))
|
||||
pos += 16
|
||||
pos += 1 # flag
|
||||
(last_date,) = unpack_from("<I", body, pos, "server last date")
|
||||
return ServerInfo(date=date, last_trade_date=last_date, sessions=sessions)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# K线偏移 0x124A
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacKlineOffsetCmd(BaseCommand[tuple[int, int]]):
|
||||
def __init__(self, offset: int = 0, count: int = 128000) -> None:
|
||||
self.offset = offset
|
||||
self.count = min(count, 128000)
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<II5x", self.offset, self.count)
|
||||
return build_mac_request(0x124A, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> tuple[int, int]:
|
||||
total = struct.unpack(">I", body[:4])[0] # 大端
|
||||
(returned,) = unpack_from("<I", body, 4, "kline offset returned")
|
||||
return total, returned
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 资金流向 0x1218 (head=2)
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacCapitalFlowCmd(BaseCommand[CapitalFlow | None]):
|
||||
def __init__(self, market: int, code: str) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack("<H8s16x21s", self.market, self.code.encode("gbk"), b"Stock_ZJLX")
|
||||
return build_mac_request(0x1218, body, head_flag=2)
|
||||
|
||||
def parse_response(self, body: bytes) -> CapitalFlow | None:
|
||||
json_part = body[27:].decode("gbk", errors="replace")
|
||||
try:
|
||||
import json
|
||||
rows = json.loads(json_part)
|
||||
if len(rows) < 2:
|
||||
return None
|
||||
r0 = rows[0] # [主买,主卖,散户买,散户卖]
|
||||
r1 = rows[1] # [5日买,5日卖,超大,大,中,小]
|
||||
return CapitalFlow(
|
||||
market=self.market, code=self.code,
|
||||
main_buy=float(r0[0]), main_sell=float(r0[1]),
|
||||
retail_buy=float(r0[2]), retail_sell=float(r0[3]),
|
||||
buy5=float(r1[0]), sell5=float(r1[1]),
|
||||
big5=float(r1[3]), mid5=float(r1[4]),
|
||||
)
|
||||
except Exception:
|
||||
return None
|
||||
class MacSymbolQuotesCmd(BaseCommand[list[MemberQuote]]):
|
||||
"""批量获取自定义字段报价(最多80只/次)。"""
|
||||
|
||||
def __init__(self, stocks: list[tuple[int, str]], bits: list[int] | None = None) -> None:
|
||||
self.stocks = stocks
|
||||
self.bits = bits if bits is not None else list(range(0x00, 0x08))
|
||||
|
||||
@staticmethod
|
||||
def _build_bitmap(bits: list[int]) -> bytes:
|
||||
bm = bytearray(20)
|
||||
for b in bits:
|
||||
bm[b // 8] |= 1 << (b % 8)
|
||||
return bytes(bm)
|
||||
|
||||
@staticmethod
|
||||
def _field_fmt(bit: int) -> str:
|
||||
if bit in (0x05, 0x08, 0x09, 0x0A, 0x0B, 0x13, 0x14, 0x15, 0x18, 0x19, 0x1A):
|
||||
return "<I"
|
||||
if bit in (0x2E,):
|
||||
return "<i"
|
||||
return "<f"
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = self._build_bitmap(self.bits) + struct.pack("<H", len(self.stocks))
|
||||
for mkt, code in self.stocks:
|
||||
body += struct.pack("<H22s", mkt, code.encode("gbk"))
|
||||
return build_mac_request(0x122B, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[MemberQuote]:
|
||||
resp_bitmap = body[:20]
|
||||
pos = 20 # 20B响应位图
|
||||
_total, row_count = unpack_from("<IH", body, pos, "quotes header")
|
||||
pos += 6
|
||||
results: list[MemberQuote] = []
|
||||
for _ in range(row_count):
|
||||
market, code_b, name_b = unpack_from("<H22s44s", body, pos, "quote row")
|
||||
pos += 68
|
||||
fields: dict[str, float] = {}
|
||||
for i in range(160):
|
||||
if resp_bitmap[i // 8] & (1 << (i % 8)):
|
||||
fmt = self._field_fmt(i)
|
||||
(v,) = unpack_from(fmt, body, pos, f"quote field {i}")
|
||||
fields[hex(i)] = float(v)
|
||||
pos += struct.calcsize(fmt)
|
||||
results.append(MemberQuote(
|
||||
market=market,
|
||||
code=code_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
name=name_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
fields=fields,
|
||||
))
|
||||
return results
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 远程文件 0x1215 / 0x1217
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class FileMeta:
|
||||
__slots__ = ("offset", "size", "flag", "md5")
|
||||
|
||||
def __init__(self, offset: int, size: int, flag: int, md5: str) -> None:
|
||||
self.offset = offset
|
||||
self.size = size
|
||||
self.flag = flag
|
||||
self.md5 = md5
|
||||
|
||||
|
||||
class MacFileListCmd(BaseCommand[FileMeta]):
|
||||
"""远程文件元信息 0x1215。"""
|
||||
|
||||
def __init__(self, filename: str) -> None:
|
||||
self.filename = filename
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
fn = self.filename.encode("gbk", errors="replace")[:70].ljust(70, b"\x00")
|
||||
body = struct.pack("<I", 0) + fn + b"\x00" * 30
|
||||
return build_mac_request(0x1215, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> FileMeta:
|
||||
offset, size, flag = unpack_from("<IIb", body, 0, "file meta")
|
||||
md5 = body[9:41].decode("ascii", errors="replace").rstrip("\x00")
|
||||
return FileMeta(offset=offset, size=size, flag=flag, md5=md5)
|
||||
|
||||
|
||||
class MacFileDownloadCmd(BaseCommand[bytes]):
|
||||
"""远程文件分块下载 0x1217。"""
|
||||
|
||||
def __init__(self, filename: str, index: int, offset: int, size: int) -> None:
|
||||
self.filename = filename
|
||||
self.index = index
|
||||
self.offset = offset
|
||||
self.size = size
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
fn = self.filename.encode("gbk", errors="replace")[:70].ljust(70, b"\x00")
|
||||
body = struct.pack("<III", self.index, self.offset, self.size) + fn + b"\x00" * 30
|
||||
return build_mac_request(0x1217, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> bytes:
|
||||
return body[8:]
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 分时缩略采样 0x254D
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MacChartSamplingCmd(BaseCommand[list[float]]):
|
||||
"""分时缩略采样价格点(约240个)。"""
|
||||
|
||||
def __init__(self, market: int, code: str) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
raw = self.code.encode("gbk")
|
||||
padded = (raw + b"\x00" * 22)[:22]
|
||||
body = struct.pack("<H22sHH9x", self.market, padded, 1, 20)
|
||||
return build_mac_request(0x254D, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[float]:
|
||||
if len(body) < 42:
|
||||
return []
|
||||
(count,) = unpack_from("<H", body, 40, "chart sampling count")
|
||||
prices: list[float] = []
|
||||
for i in range(count):
|
||||
off = 42 + i * 4
|
||||
if off + 4 > len(body):
|
||||
break
|
||||
(p,) = unpack_from("<f", body, off, "chart sampling price")
|
||||
prices.append(p)
|
||||
return prices
|
||||
@@ -0,0 +1,71 @@
|
||||
"""MAC通道 分时命令(0x122D 单日分时图)。
|
||||
|
||||
对应标准通道 0x0130 历史分钟分时,但走MAC连接池。
|
||||
请求/响应格式参照 easy-tdx SymbolTickChartCmd。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
from datetime import date, time as dtime
|
||||
|
||||
from ..._binary import unpack_from
|
||||
from ...models import MinuteBar
|
||||
from ..mac_frame import build_mac_request
|
||||
from .base import BaseCommand
|
||||
|
||||
_MSG_ID_TICK_CHART = 0x122D
|
||||
|
||||
|
||||
class MacTickChartCmd(BaseCommand[list[MinuteBar]]):
|
||||
"""MAC 单日分时图(0x122D)。
|
||||
|
||||
Args:
|
||||
market: 市场代码(0=深市, 1=沪市)。
|
||||
code: 6 位股票代码。
|
||||
date: 查询日期 YYYYMMDD(0 表示今天)。
|
||||
"""
|
||||
|
||||
def __init__(self, market: int, code: str, date: int = 0) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.date = date
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack(
|
||||
"<H22sI5H",
|
||||
self.market,
|
||||
self.code.encode("gbk"),
|
||||
self.date,
|
||||
1, # flag
|
||||
0, 0, 0, 0,
|
||||
)
|
||||
return build_mac_request(_MSG_ID_TICK_CHART, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[MinuteBar]:
|
||||
# 头部: market(2) + code(22) + query_date(4) + reserved(1) + ref_price(4) + count(2) = 35
|
||||
if len(body) < 35:
|
||||
return []
|
||||
(_market, _code_raw, _query_date, _reserved, _ref_price, count) = unpack_from(
|
||||
"<H22sIBfH", body, 0, "tick_chart header"
|
||||
)
|
||||
# 限制最大条数,防止异常
|
||||
count = min(count, 240, (len(body) - 35) // 18)
|
||||
if count <= 0:
|
||||
return []
|
||||
|
||||
bars: list[MinuteBar] = []
|
||||
for i in range(count):
|
||||
offset = 35 + i * 18
|
||||
if offset + 18 > len(body):
|
||||
break
|
||||
(minutes, price, avg, vol, _momentum) = unpack_from(
|
||||
"<HffIf", body, offset, f"tick_chart tick[{i}]"
|
||||
)
|
||||
bars.append(MinuteBar(
|
||||
price=price,
|
||||
vol=float(vol),
|
||||
avg_price=avg,
|
||||
hour=minutes // 60 % 24,
|
||||
minute=minutes % 60,
|
||||
))
|
||||
return bars
|
||||
@@ -0,0 +1,45 @@
|
||||
"""分时命令(标准通道 0x013000;0x1b08 当日分时在当前服务器格式已变,统一用历史命令传当天)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import unpack_from
|
||||
from ...models import MinuteBar
|
||||
from ..price import get_price
|
||||
from .base import BaseCommand
|
||||
|
||||
|
||||
class GetHistoryMinuteTimeDataCmd(BaseCommand[list[MinuteBar]]):
|
||||
"""某日分时(date=YYYYMMDD;当日分时也用它,date 传当天)。"""
|
||||
|
||||
def __init__(self, market: int, code: str, date: int) -> None:
|
||||
self.market = market
|
||||
self.code = code.encode("utf-8")
|
||||
self.date = date
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
header = bytes.fromhex("0c01300001010d000d00b40f".replace(" ", ""))
|
||||
return header + struct.pack("<IB6s", self.date, self.market, self.code)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[MinuteBar]:
|
||||
return _parse_minute_body(body, skip=6)
|
||||
|
||||
|
||||
def _parse_minute_body(body: bytes, skip: int) -> list[MinuteBar]:
|
||||
(num,) = unpack_from("<H", body, 0, "minute header")
|
||||
pos = skip
|
||||
last_price = 0
|
||||
bars: list[MinuteBar] = []
|
||||
for idx in range(num):
|
||||
price_diff, pos = get_price(body, pos)
|
||||
_unknown, pos = get_price(body, pos) # 协议第2字段(unknown, easy-tdx也丢弃)
|
||||
vol, pos = get_price(body, pos)
|
||||
last_price += price_diff
|
||||
# 合成时间: 前120条=09:30~11:29, 后120条=13:00~14:59 (与easy-tdx一致)
|
||||
if idx < 120:
|
||||
hour, minute = 9 + (30 + idx) // 60, (30 + idx) % 60
|
||||
else:
|
||||
hour, minute = 13 + (idx - 120) // 60, (idx - 120) % 60
|
||||
bars.append(MinuteBar(price=last_price / 100.0, vol=float(vol),
|
||||
hour=hour, minute=minute))
|
||||
return bars
|
||||
@@ -0,0 +1,122 @@
|
||||
"""五档报价命令(标准通道 0x010C/method 0x05053E,最多80只/次)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import unpack_from
|
||||
from ...models import SecurityQuote
|
||||
from ..price import get_price
|
||||
from ..volume import get_volume
|
||||
from .base import BaseCommand
|
||||
|
||||
MAX_QUOTES = 80
|
||||
|
||||
|
||||
def price_decimal_digits(market: int, code: str) -> int:
|
||||
"""推断报价有效小数位:股票2位(/100)、指数/ETF/基金/转债/国债3位(/1000)。"""
|
||||
code = (code or "").strip().rstrip("\x00")
|
||||
if market == 1: # SH
|
||||
if code.startswith(("5", "000", "8")):
|
||||
return 3
|
||||
return 2
|
||||
if market == 0: # SZ
|
||||
if code.startswith("1"):
|
||||
return 3
|
||||
return 2
|
||||
return 2
|
||||
|
||||
|
||||
def format_server_time(raw: int) -> str:
|
||||
"""服务器时间编码:小时 + 百万分之一小时。14999212 -> 14:59:57.163"""
|
||||
hours, fractional_hour = divmod(raw, 1_000_000)
|
||||
total_millis = fractional_hour * 3600 // 1000
|
||||
minutes, remainder = divmod(total_millis, 60_000)
|
||||
seconds, millis = divmod(remainder, 1000)
|
||||
return f"{hours:02d}:{minutes:02d}:{seconds:02d}.{millis:03d}"
|
||||
|
||||
|
||||
class GetSecurityQuotesCmd(BaseCommand[list[SecurityQuote]]):
|
||||
"""批量获取实时五档行情(最多80只/次)。"""
|
||||
|
||||
def __init__(self, stocks: list[tuple[int, str]]) -> None:
|
||||
if not stocks:
|
||||
raise ValueError("stocks 不能为空")
|
||||
if len(stocks) > MAX_QUOTES:
|
||||
raise ValueError(f"单次最多查询{MAX_QUOTES}只")
|
||||
self.stocks = stocks
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
n = len(self.stocks)
|
||||
payload_len = n * 7 + 12
|
||||
header = struct.pack(
|
||||
"<HIHHIIHH", 0x010C, 0x02006320, payload_len, payload_len, 0x0005053E, 0, 0, n
|
||||
)
|
||||
body = bytearray(header)
|
||||
for market, code in self.stocks:
|
||||
body.extend(struct.pack("<B6s", int(market), code.encode("utf-8")))
|
||||
return bytes(body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[SecurityQuote]:
|
||||
pos = 2 # 跳过2字节魔数
|
||||
(num,) = unpack_from("<H", body, pos, "quotes header")
|
||||
pos += 2
|
||||
results: list[SecurityQuote] = []
|
||||
|
||||
for _ in range(num):
|
||||
market_b, code_b, _active1 = unpack_from("<B6sH", body, pos, "quote record header")
|
||||
pos += 9
|
||||
|
||||
price_raw, pos = get_price(body, pos)
|
||||
last_close_diff, pos = get_price(body, pos)
|
||||
open_diff, pos = get_price(body, pos)
|
||||
high_diff, pos = get_price(body, pos)
|
||||
low_diff, pos = get_price(body, pos)
|
||||
unknown_0, pos = get_price(body, pos) # server_time原始
|
||||
_unknown_1, pos = get_price(body, pos)
|
||||
vol, pos = get_price(body, pos)
|
||||
cur_vol, pos = get_price(body, pos)
|
||||
amount, _pos2 = get_volume(body, pos)
|
||||
pos = _pos2 # get_volume 已推进4字节
|
||||
s_vol, pos = get_price(body, pos)
|
||||
b_vol, pos = get_price(body, pos)
|
||||
_unknown_2, pos = get_price(body, pos)
|
||||
_unknown_3, pos = get_price(body, pos)
|
||||
|
||||
bid: list[tuple[float, float]] = []
|
||||
ask: list[tuple[float, float]] = []
|
||||
for _d in range(5):
|
||||
bid_d, pos = get_price(body, pos)
|
||||
ask_d, pos = get_price(body, pos)
|
||||
bv, pos = get_price(body, pos)
|
||||
av, pos = get_price(body, pos)
|
||||
bid.append((bid_d, float(bv)))
|
||||
ask.append((ask_d, float(av)))
|
||||
|
||||
(trading_status,) = unpack_from("<H", body, pos, "quote tail")
|
||||
pos += 2
|
||||
for _d in range(4):
|
||||
_, pos = get_price(body, pos)
|
||||
_rise_speed, _active2 = unpack_from("<hH", body, pos, "quote tail2")
|
||||
pos += 4
|
||||
|
||||
code = code_b.decode("utf-8").rstrip("\x00")
|
||||
divisor = 10 ** price_decimal_digits(market_b, code)
|
||||
p = price_raw / divisor
|
||||
results.append(SecurityQuote(
|
||||
market=market_b, code=code, price=p,
|
||||
cur_vol=float(cur_vol),
|
||||
rise_speed=_rise_speed / 100.0,
|
||||
decimal_point=price_decimal_digits(market_b, code),
|
||||
open_amount=float(_unknown_3),
|
||||
pre_close=(price_raw + last_close_diff) / divisor,
|
||||
open=(price_raw + open_diff) / divisor,
|
||||
high=(price_raw + high_diff) / divisor,
|
||||
low=(price_raw + low_diff) / divisor,
|
||||
vol=float(vol), amount=amount, s_vol=float(s_vol), b_vol=float(b_vol),
|
||||
bid=[((price_raw + d) / divisor, v) for d, v in bid],
|
||||
ask=[((price_raw + d) / divisor, v) for d, v in ask],
|
||||
server_time=format_server_time(unknown_0),
|
||||
trading_status=trading_status,
|
||||
))
|
||||
return results
|
||||
return results
|
||||
@@ -0,0 +1,60 @@
|
||||
"""证券列表命令(标准通道 0x011864,1000条/页)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import slice_bytes, unpack_from
|
||||
from ...models import SecurityInfo
|
||||
from ..volume import decode_volume
|
||||
from .base import BaseCommand
|
||||
|
||||
PAGE_SIZE = 1000
|
||||
_RECORD_SIZE = 29
|
||||
|
||||
|
||||
class GetSecurityCountCmd(BaseCommand[int]):
|
||||
"""获取市场证券数量。"""
|
||||
|
||||
def __init__(self, market: int) -> None:
|
||||
self.market = market
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
# 与 easy-tdx 一致: 0c0c186c 头 + market(H) + 4字节尾部
|
||||
header = bytes.fromhex("0c0c186c0001080008004e04".replace(" ", ""))
|
||||
return header + struct.pack("<H", self.market) + b"\x75\xc7\x33\x01"
|
||||
|
||||
def parse_response(self, body: bytes) -> int:
|
||||
(count,) = unpack_from("<H", body, 0, "security count")
|
||||
return int(count)
|
||||
|
||||
|
||||
class GetSecurityListCmd(BaseCommand[list[SecurityInfo]]):
|
||||
"""分页获取证券列表。"""
|
||||
|
||||
def __init__(self, market: int, start: int) -> None:
|
||||
self.market = market
|
||||
self.start = start
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
header = bytes.fromhex("0c0118640101060006005004".replace(" ", ""))
|
||||
return header + struct.pack("<HHH", self.market, self.start, 0)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[SecurityInfo]:
|
||||
(num,) = unpack_from("<H", body, 0, "security list header")
|
||||
pos = 2
|
||||
results: list[SecurityInfo] = []
|
||||
for _ in range(num):
|
||||
raw = slice_bytes(body, pos, _RECORD_SIZE, "security list record")
|
||||
(code_bytes, volunit, name_bytes, _u1, decimal_point, pre_close_raw, _u2) = (
|
||||
struct.unpack("<6sH8s4sBI4s", raw)
|
||||
)
|
||||
results.append(SecurityInfo(
|
||||
market=self.market,
|
||||
code=code_bytes.decode("utf-8", errors="replace").rstrip("\x00"),
|
||||
name=name_bytes.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
volunit=volunit,
|
||||
decimal_point=decimal_point,
|
||||
pre_close=decode_volume(pre_close_raw),
|
||||
))
|
||||
pos += _RECORD_SIZE
|
||||
return results
|
||||
@@ -0,0 +1,9 @@
|
||||
"""握手命令原始字节(连接建立后必须按序发送3条并丢弃响应)"""
|
||||
from __future__ import annotations
|
||||
|
||||
SETUP_CMD1 = bytes.fromhex("0c0218930001030003000d0001")
|
||||
SETUP_CMD2 = bytes.fromhex("0c0218940001030003000d0002")
|
||||
SETUP_CMD3 = bytes.fromhex(
|
||||
"0c031899000120002000db0fd5d0c9ccd6a4a8af0000008fc22540130000d500c9ccbdf0d7ea00000002"
|
||||
)
|
||||
SETUP_COMMANDS: tuple[bytes, ...] = (SETUP_CMD1, SETUP_CMD2, SETUP_CMD3)
|
||||
@@ -0,0 +1,195 @@
|
||||
"""标准通道扩展命令:公司信息(F10)/板块文件/市场统计/涨跌停价"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import slice_bytes, unpack_from
|
||||
from ...models import Market
|
||||
from .base import BaseCommand
|
||||
|
||||
_FMT_HDR = "<HIHHHH"
|
||||
|
||||
|
||||
def _std_req(head_hex: str, payload: bytes) -> bytes:
|
||||
return bytes.fromhex(head_hex) + payload
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 公司信息 F10 0x109B / 0x109C
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class CompanyInfoCategory:
|
||||
"""F10目录条目"""
|
||||
__slots__ = ("name", "filename", "start", "length")
|
||||
|
||||
def __init__(self, name: str, filename: str, start: int, length: int) -> None:
|
||||
self.name = name
|
||||
self.filename = filename
|
||||
self.start = start
|
||||
self.length = length
|
||||
|
||||
|
||||
class GetCompanyInfoCategoryCmd(BaseCommand[list[CompanyInfoCategory]]):
|
||||
def __init__(self, market: Market, code: str) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
return _std_req("0c0f109b00010e000e00cf02",
|
||||
struct.pack("<H6sI", int(self.market), self.code.encode("utf-8"), 0))
|
||||
|
||||
def parse_response(self, body: bytes) -> list[CompanyInfoCategory]:
|
||||
(num,) = unpack_from("<H", body, 0, "f10 category count")
|
||||
out: list[CompanyInfoCategory] = []
|
||||
for i in range(num):
|
||||
off = 2 + i * 152
|
||||
name_b, filename_b, start, length = struct.unpack("<64s80sII", slice_bytes(body, off, 152, "f10 cat"))
|
||||
out.append(CompanyInfoCategory(
|
||||
name=name_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
filename=filename_b.decode("gbk", errors="replace").rstrip("\x00"),
|
||||
start=start, length=length,
|
||||
))
|
||||
return out
|
||||
|
||||
|
||||
class GetCompanyInfoContentCmd(BaseCommand[str]):
|
||||
def __init__(self, market: Market, code: str, filename: str,
|
||||
offset: int = 0, length: int = 65535) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.filename = filename
|
||||
self.offset = offset
|
||||
self.length = length
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
fn = self.filename.encode("gbk")[:80].ljust(80, b"\x00")
|
||||
return _std_req("0c07109c000168006800d002",
|
||||
struct.pack("<H6sH80sIII", int(self.market),
|
||||
self.code.encode("utf-8"), 0, fn,
|
||||
self.offset, self.length, 0))
|
||||
|
||||
def parse_response(self, body: bytes) -> str:
|
||||
# 12B头(10s+H len) + len字节GBK文本
|
||||
(length,) = unpack_from("<H", body, 10, "f10 content len")
|
||||
text = body[12:12 + length].decode("gbk", errors="replace")
|
||||
return text
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 板块文件 0x1869 / 0x186A
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class BlockInfo:
|
||||
__slots__ = ("size", "md5", "filename")
|
||||
|
||||
def __init__(self, size: int, md5: str, filename: str) -> None:
|
||||
self.size = size
|
||||
self.md5 = md5
|
||||
self.filename = filename
|
||||
|
||||
|
||||
class GetBlockInfoMetaCmd(BaseCommand[BlockInfo]):
|
||||
def __init__(self, filename: str) -> None:
|
||||
self.filename = filename
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
fn = self.filename.encode("ascii", errors="replace")[:40].ljust(40, b"\x00")
|
||||
return _std_req("0c39186900012a002a00c502", fn)
|
||||
|
||||
def parse_response(self, body: bytes) -> BlockInfo:
|
||||
size, _v1, md5_b, _v2 = struct.unpack("<I1s32s1s", body[:38])
|
||||
return BlockInfo(size=size, md5=md5_b.decode("ascii", errors="replace"),
|
||||
filename=self.filename)
|
||||
|
||||
|
||||
class GetBlockInfoCmd(BaseCommand[bytes]):
|
||||
"""下载板块文件内容(分块)。"""
|
||||
|
||||
def __init__(self, filename: str, start: int = 0, length: int = 0) -> None:
|
||||
self.filename = filename
|
||||
self.start = start
|
||||
self.length = length
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
fn = self.filename.encode("ascii", errors="replace")[:100].ljust(100, b"\x00")
|
||||
return _std_req("0c37186a00016e006e00b906",
|
||||
struct.pack("<II", self.start, self.length) + fn)
|
||||
|
||||
def parse_response(self, body: bytes) -> bytes:
|
||||
return body[4:] # 跳过前4字节返回原始二进制
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 涨跌停价(本地计算,无网络命令)
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def compute_price_limits(market: Market, code: str, name: str,
|
||||
pre_close: float, listed_days: int = 9999) -> tuple[float | None, float | None]:
|
||||
"""计算涨跌停价(参照 easy-tdx codec/price_rules)。指数/基金无涨跌停。"""
|
||||
code = (code or "").strip()
|
||||
if pre_close is None or pre_close <= 0:
|
||||
return None, None
|
||||
# 指数类无涨跌停
|
||||
if market == Market.SH and (code.startswith(("000", "880", "999")) or "指数" in (name or "")):
|
||||
return None, None
|
||||
if market == Market.SZ and (code.startswith(("395", "399")) or "指数" in (name or "")):
|
||||
return None, None
|
||||
# 涨跌幅
|
||||
if market == Market.BJ:
|
||||
pct = 0.30
|
||||
elif code.startswith(("688", "300", "301")):
|
||||
pct = 0.20
|
||||
elif "ST" in (name or "").upper():
|
||||
pct = 0.05
|
||||
else:
|
||||
pct = 0.10
|
||||
# 上市初期不设限(沪/深前5日, 北交首日)
|
||||
if listed_days <= 5 and market != Market.BJ:
|
||||
return None, None
|
||||
if listed_days <= 0:
|
||||
return None, None
|
||||
up = round(pre_close * (1 + pct) + 0.00001, 2)
|
||||
down = round(pre_close * (1 - pct) + 0.00001, 2)
|
||||
return up, down
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 市场统计(用报价接口查上证指数880005/880001/880006组合,由api层实现)
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class MarketStat:
|
||||
__slots__ = ("up_count", "down_count", "flat_count", "total_count",
|
||||
"total_amount", "total_vol", "total_market_cap",
|
||||
"limit_up_count", "limit_down_count")
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.up_count = 0
|
||||
self.down_count = 0
|
||||
self.flat_count = 0
|
||||
self.total_count = 0
|
||||
self.total_amount = 0.0
|
||||
self.total_vol = 0.0
|
||||
self.total_market_cap = 0.0
|
||||
self.limit_up_count = 0
|
||||
self.limit_down_count = 0
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# 报告/财务文件 0x06B9(分块下载,get_report_file/get_financial_file 共用)
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
class GetReportFileCmd(BaseCommand[bytes]):
|
||||
"""下载远程报告/财务文件分块(tdxfin/gpcw.txt、gpcw*.zip 等)。"""
|
||||
|
||||
def __init__(self, filename: str, start: int = 0, length: int = 30000) -> None:
|
||||
self.filename = filename
|
||||
self.start = start
|
||||
self.length = length
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
fn = self.filename.encode("ascii", errors="replace")[:100].ljust(100, b"\x00")
|
||||
return _std_req("0c37186a00016e006e00b906",
|
||||
struct.pack("<II", self.start, self.length) + fn)
|
||||
|
||||
def parse_response(self, body: bytes) -> bytes:
|
||||
return body[4:] # 跳过前4字节返回原始文件块
|
||||
@@ -0,0 +1,126 @@
|
||||
"""逐笔成交命令:标准通道(当日0x170801含笔数 / 历史0x013001)+ MAC通道(0x122F含trade_count)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import unpack_from
|
||||
from ...models import TransactionRecord
|
||||
from ..datetime_ import get_time
|
||||
from ..mac_frame import build_mac_request
|
||||
from ..price import get_price
|
||||
from .base import BaseCommand
|
||||
|
||||
STD_PAGE_SIZE = 800
|
||||
MAC_PAGE_SIZE = 1000
|
||||
|
||||
|
||||
class GetTransactionDataCmd(BaseCommand[list[TransactionRecord]]):
|
||||
"""标准通道当日逐笔(0x170801)。协议含成交笔数(_num_orders),此处显式解析暴露。"""
|
||||
|
||||
def __init__(self, market: int, code: str, start: int, count: int = 800) -> None:
|
||||
self.market = market
|
||||
self.code = code.encode("utf-8")
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
header = bytes.fromhex("0c17080101010e000e00c50f".replace(" ", ""))
|
||||
return header + struct.pack("<H6sHH", self.market, self.code, self.start, self.count)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[TransactionRecord]:
|
||||
(num,) = unpack_from("<H", body, 0, "std transaction header")
|
||||
pos = 2
|
||||
last_price = 0
|
||||
records: list[TransactionRecord] = []
|
||||
for _ in range(num):
|
||||
hour, minute, pos = get_time(body, pos)
|
||||
price_diff, pos = get_price(body, pos)
|
||||
vol, pos = get_price(body, pos)
|
||||
num_orders, pos = get_price(body, pos) # 成交笔数(当日独有)
|
||||
bs_flag, pos = get_price(body, pos)
|
||||
_unknown, pos = get_price(body, pos)
|
||||
last_price += price_diff
|
||||
records.append(TransactionRecord(
|
||||
market=self.market, code=self.code.decode("utf-8"),
|
||||
time=f"{hour:02d}:{minute:02d}:00",
|
||||
price=last_price / 100.0, vol=float(vol),
|
||||
trade_count=int(num_orders), bs_flag=bs_flag,
|
||||
))
|
||||
return records
|
||||
|
||||
|
||||
class GetHistoryTransactionDataCmd(BaseCommand[list[TransactionRecord]]):
|
||||
"""标准通道历史逐笔(0x013001,无笔数字段)。"""
|
||||
|
||||
def __init__(self, market: int, code: str, date: int, start: int, count: int = 800) -> None:
|
||||
self.market = market
|
||||
self.code = code.encode("utf-8")
|
||||
self.date = date
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
header = bytes.fromhex("0c013001000112001200b50f".replace(" ", ""))
|
||||
return header + struct.pack("<IH6sHH", self.date, self.market, self.code, self.start, self.count)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[TransactionRecord]:
|
||||
(num,) = unpack_from("<H", body, 0, "hist transaction header")
|
||||
pos = 6 # 2(num) + 4(skip)
|
||||
last_price = 0
|
||||
records: list[TransactionRecord] = []
|
||||
for _ in range(num):
|
||||
hour, minute, pos = get_time(body, pos)
|
||||
price_diff, pos = get_price(body, pos)
|
||||
vol, pos = get_price(body, pos)
|
||||
bs_flag, pos = get_price(body, pos)
|
||||
_unknown, pos = get_price(body, pos)
|
||||
last_price += price_diff
|
||||
records.append(TransactionRecord(
|
||||
market=self.market, code=self.code.decode("utf-8"),
|
||||
time=f"{hour:02d}:{minute:02d}:00",
|
||||
price=last_price / 100.0, vol=float(vol),
|
||||
trade_count=0, bs_flag=bs_flag, # 历史无笔数
|
||||
))
|
||||
return records
|
||||
|
||||
|
||||
class MacTransactionCmd(BaseCommand[list[TransactionRecord]]):
|
||||
"""MAC通道逐笔(0x122F,每条18B固定:time+price+vol+trade_count+bs_flag,含成交笔数)。"""
|
||||
|
||||
def __init__(self, market: int, code: str, ymd: int, start: int, count: int = 1000) -> None:
|
||||
self.market = market
|
||||
self.code = code
|
||||
self.ymd = ymd
|
||||
self.start = start
|
||||
self.count = count
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
body = struct.pack(
|
||||
"<H22sIIH10x",
|
||||
self.market,
|
||||
self.code.encode("gbk"),
|
||||
self.ymd,
|
||||
self.start,
|
||||
self.count,
|
||||
)
|
||||
return build_mac_request(0x122F, body)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[TransactionRecord]:
|
||||
(count,) = unpack_from("<H", body, 29, "mac transaction count")
|
||||
records: list[TransactionRecord] = []
|
||||
for i in range(count):
|
||||
offset = 39 + i * 18
|
||||
try:
|
||||
time_sec, price, volume, trade_count, bs_flag = unpack_from(
|
||||
"<IfIIH", body, offset, "mac transaction item"
|
||||
)
|
||||
except ValueError:
|
||||
break
|
||||
h, m, s = time_sec // 3600, time_sec % 3600 // 60, time_sec % 60
|
||||
records.append(TransactionRecord(
|
||||
market=self.market, code=self.code,
|
||||
time=f"{h:02d}:{m:02d}:{s:02d}",
|
||||
price=price, vol=float(volume),
|
||||
trade_count=int(trade_count), bs_flag=bs_flag,
|
||||
))
|
||||
return records
|
||||
@@ -0,0 +1,85 @@
|
||||
"""除权除息命令(标准通道 0x1F1876,仅标准通道提供——MAC主机池不响应)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
from ..._binary import slice_bytes, unpack_from
|
||||
from ...models import XdxrRecord
|
||||
from ..datetime_ import get_datetime
|
||||
from ..volume import decode_volume
|
||||
from .base import BaseCommand
|
||||
|
||||
# 与 easy-tdx 一致的分类名称映射(通达信协议标准)
|
||||
XDXR_CATEGORY_NAMES = {
|
||||
1: "除权除息",
|
||||
2: "送配股上市",
|
||||
3: "非流通股上市",
|
||||
4: "未知股本变动",
|
||||
5: "股本变化",
|
||||
6: "增发新股",
|
||||
7: "股份回购",
|
||||
8: "增发新股上市",
|
||||
9: "转配股上市",
|
||||
10: "可转债上市",
|
||||
11: "扩缩股",
|
||||
12: "非流通股缩股",
|
||||
13: "送认购权证",
|
||||
14: "送认沽权证",
|
||||
}
|
||||
|
||||
|
||||
class GetXdxrInfoCmd(BaseCommand[list[XdxrRecord]]):
|
||||
"""获取除权除息历史记录。"""
|
||||
|
||||
def __init__(self, market: int, code: str) -> None:
|
||||
self.market = market
|
||||
self.code = code.encode("utf-8")
|
||||
|
||||
def build_request(self) -> bytes:
|
||||
header = bytes.fromhex("0c1f187600010b000b000f000100".replace(" ", ""))
|
||||
return header + struct.pack("<B6s", self.market, self.code)
|
||||
|
||||
def parse_response(self, body: bytes) -> list[XdxrRecord]:
|
||||
if len(body) < 11:
|
||||
raise ValueError("xdxr body 过短")
|
||||
pos = 9
|
||||
(num,) = unpack_from("<H", body, pos, "xdxr header")
|
||||
pos += 2
|
||||
records: list[XdxrRecord] = []
|
||||
for _ in range(num):
|
||||
market_b, code_b = unpack_from("<B6s", body, pos, "xdxr record header")
|
||||
pos += 7
|
||||
pos += 1 # padding
|
||||
year, month, day, _h, _m, pos = get_datetime(9, body, pos)
|
||||
(category,) = unpack_from("<B", body, pos, "xdxr category")
|
||||
pos += 1
|
||||
chunk = slice_bytes(body, pos, 16, "xdxr body")
|
||||
pos += 16
|
||||
|
||||
rec = XdxrRecord(
|
||||
market=market_b, code=code_b.decode("utf-8").rstrip("\x00"),
|
||||
year=year, month=month, day=day, category=category,
|
||||
name=XDXR_CATEGORY_NAMES.get(category, str(category)),
|
||||
)
|
||||
if category == 1:
|
||||
fenhong, peigujia, songzhuangu, peigu = struct.unpack("<ffff", chunk)
|
||||
rec.fenhong = fenhong / 10.0
|
||||
rec.peigujia = peigujia
|
||||
rec.songzhuangu = songzhuangu / 10.0
|
||||
rec.peigu = peigu / 10.0
|
||||
elif category in (11, 12):
|
||||
_, _, suogu, _ = struct.unpack("<IIfI", chunk)
|
||||
rec.suogu = suogu
|
||||
elif category in (13, 14):
|
||||
xingquanjia, _, fenshu, _ = struct.unpack("<fIfI", chunk)
|
||||
rec.xingquanjia = xingquanjia
|
||||
rec.fenshu = fenshu
|
||||
else:
|
||||
# 股本变动类: 4个uint32 = 前流通/前总股本/后流通/后总股本(自定义浮点, 万股)
|
||||
ql, qz, hl, hz = struct.unpack("<IIII", chunk)
|
||||
rec.panqian_liutong = decode_volume(ql)
|
||||
rec.qian_zongguben = decode_volume(qz)
|
||||
rec.panhou_liutong = decode_volume(hl)
|
||||
rec.hou_zongguben = decode_volume(hz)
|
||||
records.append(rec)
|
||||
return records
|
||||
@@ -0,0 +1,28 @@
|
||||
"""日期时间解码(通达信 TCP 两种格式:分钟级 zipday / 日线级 YYYYMMDD)"""
|
||||
from __future__ import annotations
|
||||
|
||||
from .._binary import unpack_from
|
||||
|
||||
|
||||
def get_datetime(category: int, data: bytes | bytearray | memoryview, pos: int) -> tuple[int, int, int, int, int, int]:
|
||||
"""按K线周期解析4字节时间,返回 (year, month, day, hour, minute, new_pos)。
|
||||
|
||||
分钟级(category<4 或 ==7/8):2字节压缩日期+2字节分钟数,year=(>>11)+2004
|
||||
日线及以上:4字节 YYYYMMDD,hour=15, minute=0(收盘时间)
|
||||
"""
|
||||
if category < 4 or category in (7, 8):
|
||||
zipday, tminutes = unpack_from("<HH", data, pos, "minute datetime")
|
||||
year = (zipday >> 11) + 2004
|
||||
month = (zipday % 2048) // 100
|
||||
day = (zipday % 2048) % 100
|
||||
hour = tminutes // 60
|
||||
minute = tminutes % 60
|
||||
return year, month, day, hour, minute, pos + 4
|
||||
(zipday,) = unpack_from("<I", data, pos, "day datetime")
|
||||
return zipday // 10000, (zipday % 10000) // 100, zipday % 100, 15, 0, pos + 4
|
||||
|
||||
|
||||
def get_time(data: bytes | bytearray | memoryview, pos: int) -> tuple[int, int, int]:
|
||||
"""解析2字节时间(分钟数),返回 (hour, minute, new_pos)。"""
|
||||
(tminutes,) = unpack_from("<H", data, pos, "trade time")
|
||||
return tminutes // 60, tminutes % 60, pos + 2
|
||||
@@ -0,0 +1,43 @@
|
||||
"""响应帧头解析 + zlib 按需解压(16字节固定头,标准/MAC两通道共用)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import zlib
|
||||
from dataclasses import dataclass
|
||||
|
||||
from .._binary import unpack_from
|
||||
|
||||
HEADER_SIZE = 16
|
||||
_HEADER_FMT = "<IIIHH"
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class FrameHeader:
|
||||
magic: int # 恒为 7654321 (0x0074CBB1)
|
||||
seq_id: int
|
||||
method: int
|
||||
zipsize: int # body 实际长度
|
||||
unzipsize: int # 解压后长度;相等=未压缩
|
||||
|
||||
|
||||
def parse_header(buf: bytes | bytearray | memoryview) -> FrameHeader:
|
||||
magic, seq_id, method, zipsize, unzipsize = unpack_from(_HEADER_FMT, buf, 0, "frame header")
|
||||
return FrameHeader(magic, seq_id, method, zipsize, unzipsize)
|
||||
|
||||
|
||||
def decompress_body(header: FrameHeader, raw_body: bytes) -> bytes:
|
||||
"""按需 zlib 解压 body(zipsize==unzipsize 直接返回)。"""
|
||||
if len(raw_body) != header.zipsize:
|
||||
raise ValueError(
|
||||
f"frame body 长度不符: header={header.zipsize}, actual={len(raw_body)}"
|
||||
)
|
||||
if header.zipsize == header.unzipsize:
|
||||
return raw_body
|
||||
try:
|
||||
body = zlib.decompress(raw_body)
|
||||
except zlib.error as e:
|
||||
raise ValueError(f"zlib 解压失败: {e}") from e
|
||||
if len(body) != header.unzipsize:
|
||||
raise ValueError(
|
||||
f"解压长度不符: header={header.unzipsize}, actual={len(body)}"
|
||||
)
|
||||
return body
|
||||
@@ -0,0 +1,22 @@
|
||||
"""MAC 协议请求帧构建(10字节头 + 2字节命令ID + body)"""
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
_MAC_HEADER_FMT = "<BIBHH"
|
||||
_MAC_HEADER_SIZE = 10
|
||||
_MAC_HEAD_FLAG = 0x1C
|
||||
|
||||
|
||||
def build_mac_request(msg_id: int, body: bytes, *, head_flag: int = _MAC_HEAD_FLAG) -> bytes:
|
||||
"""构建 MAC 协议请求帧。"""
|
||||
inner = struct.pack("<H", msg_id) + body
|
||||
header = struct.pack(
|
||||
_MAC_HEADER_FMT,
|
||||
head_flag,
|
||||
0, # customize
|
||||
1, # version
|
||||
len(inner), # zipsize
|
||||
len(inner), # unzipsize(MAC不压缩请求)
|
||||
)
|
||||
return header + inner
|
||||
@@ -0,0 +1,67 @@
|
||||
"""变长有符号整数编解码(通达信价格编码)+ OHLC 差分还原"""
|
||||
from __future__ import annotations
|
||||
|
||||
from .._binary import unpack_from
|
||||
|
||||
|
||||
def get_price(data: bytes | bytearray | memoryview, pos: int) -> tuple[int, int]:
|
||||
"""解码一个变长有符号整数,返回 (value, new_pos)。
|
||||
|
||||
协议规则:首字节 bit7=继续, bit6=符号(1负), bit5~0=低6位;后续字节 bit7=继续, bit6~0=7位。
|
||||
"""
|
||||
start = pos
|
||||
try:
|
||||
b = data[pos]
|
||||
except IndexError:
|
||||
raise ValueError(f"price varint 截断 @{start}")
|
||||
value = b & 0x3F
|
||||
negative = bool(b & 0x40)
|
||||
if b & 0x80:
|
||||
bit_shift = 6
|
||||
while True:
|
||||
pos += 1
|
||||
b = data[pos]
|
||||
value |= (b & 0x7F) << bit_shift
|
||||
bit_shift += 7
|
||||
if not (b & 0x80):
|
||||
break
|
||||
pos += 1
|
||||
return (-value if negative else value), pos
|
||||
|
||||
|
||||
def put_price(value: int) -> bytes:
|
||||
"""将整数编码为变长格式(构造请求包用)。"""
|
||||
negative = value < 0
|
||||
value = abs(value)
|
||||
first = value & 0x3F
|
||||
value >>= 6
|
||||
if negative:
|
||||
first |= 0x40
|
||||
if value:
|
||||
first |= 0x80
|
||||
result = bytearray([first])
|
||||
while value:
|
||||
b = value & 0x7F
|
||||
value >>= 7
|
||||
if value:
|
||||
b |= 0x80
|
||||
result.append(b)
|
||||
return bytes(result)
|
||||
|
||||
|
||||
def decode_ohlc_diffs(data: bytes | bytearray | memoryview, pos: int) -> tuple[float, float, float, float, int]:
|
||||
"""解码 K线单条记录的 OHLC 差分(相对基准),返回 (open, close, high, low, new_pos)。
|
||||
|
||||
差分还原(与 pytdx/easy-tdx 一致):
|
||||
open_abs = open_diff + pre_diff_base
|
||||
close_abs = open_abs + close_diff
|
||||
high_abs = open_abs + high_diff
|
||||
low_abs = open_abs + low_diff
|
||||
pre_diff_base 更新为 open_abs + close_diff(由调用方维护)
|
||||
价格 /1000 还原为元。
|
||||
"""
|
||||
open_diff, pos = get_price(data, pos)
|
||||
close_diff, pos = get_price(data, pos)
|
||||
high_diff, pos = get_price(data, pos)
|
||||
low_diff, pos = get_price(data, pos)
|
||||
return open_diff, close_diff, high_diff, low_diff, pos
|
||||
@@ -0,0 +1,46 @@
|
||||
"""通达信 4 字节自定义浮点解码(成交量/股本专用)"""
|
||||
from __future__ import annotations
|
||||
|
||||
from .._binary import unpack_from
|
||||
|
||||
|
||||
def get_volume(data: bytes | bytearray | memoryview, pos: int) -> tuple[float, int]:
|
||||
"""从 data[pos:pos+4] 解码成交量,返回 (volume_float, new_pos)。"""
|
||||
(ivol,) = unpack_from("<I", data, pos, "volume")
|
||||
return _decode_volume(ivol), pos + 4
|
||||
|
||||
|
||||
def decode_volume(raw: int) -> float:
|
||||
"""解码单个自定义浮点整数。"""
|
||||
return _decode_volume(raw)
|
||||
|
||||
|
||||
def _decode_volume(ivol: int) -> float:
|
||||
if ivol == 0:
|
||||
return 0.0
|
||||
logpoint = (ivol >> 24) & 0xFF
|
||||
hleax = (ivol >> 16) & 0xFF
|
||||
lheax = (ivol >> 8) & 0xFF
|
||||
lleax = ivol & 0xFF
|
||||
|
||||
exp = logpoint * 2 - 0x7F
|
||||
base = _pow2(exp)
|
||||
|
||||
exp_h = logpoint * 2 - 0x86
|
||||
if hleax > 0x80:
|
||||
hi = _pow2(exp_h) * 128 + (hleax & 0x7F) * _pow2(exp_h + 1)
|
||||
else:
|
||||
hi = _pow2(exp_h) * hleax
|
||||
|
||||
mid = _pow2(logpoint * 2 - 0x8E) * lheax
|
||||
lo = _pow2(logpoint * 2 - 0x96) * lleax
|
||||
if hleax & 0x80:
|
||||
mid *= 2.0
|
||||
lo *= 2.0
|
||||
return base + hi + mid + lo
|
||||
|
||||
|
||||
def _pow2(exp: int) -> float:
|
||||
if exp >= 0:
|
||||
return float(1 << exp) if exp < 63 else 2.0**exp
|
||||
return 1.0 / (1 << (-exp)) if -exp < 63 else 2.0**exp
|
||||
@@ -0,0 +1,20 @@
|
||||
"""通道选路:命令类型 → 标准/MAC"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from .models import Adjust, KlinePeriod
|
||||
|
||||
Route = Literal["standard", "mac"]
|
||||
|
||||
|
||||
def route_for(cmd_type: str, *, adjust: Adjust = Adjust.NONE,
|
||||
period: KlinePeriod | None = None) -> Route:
|
||||
"""返回命令应走的通道。"""
|
||||
if cmd_type == "kline":
|
||||
if period == KlinePeriod.MIN_1:
|
||||
return "mac" # MIN_1 统一走 MAC (0x122E),支持深度分页无缺失
|
||||
return "mac" if adjust != Adjust.NONE else "standard"
|
||||
if cmd_type == "transaction":
|
||||
return "mac" # MAC 0x122F 含 trade_count 且支持历史日期
|
||||
return "standard" # quote/list/count/minute/xdxr/finance 均走标准
|
||||
Whitespace-only changes.
@@ -0,0 +1,155 @@
|
||||
"""并发分页调度器:单请求拆N页,用至多 concurrency 个连接并行拉取,按序拼接。
|
||||
|
||||
v5 设计(回归 v1 稳定模型 + 修复连接复用 bug):
|
||||
- 预分配 k 个连接,每个 worker 复用同一连接(避免每页 acquire 开销)。
|
||||
- 单页失败时:释放坏连接 → 获取新连接 → 更新 worker 持有的连接引用,
|
||||
避免跨页使用已关闭连接导致的连锁失败。
|
||||
- 不额外加重试轮(避免引入更多 acquire 命中坏服务器的机会)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Callable
|
||||
|
||||
from ..pool.pool import DynamicPool, PooledConnection, PoolTimeoutError
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class PageSpec:
|
||||
page_size: int = 800
|
||||
total: int | None = None # 已知总量(None=用 stop_when_short)
|
||||
stop_when_short: bool = True # 某页返回 < page_size 即停
|
||||
max_pages: int | None = None
|
||||
ordered: bool = True # 按 start 升序拼接
|
||||
min_page: int = 100 # 规避标准K线 count<100 空响应坑
|
||||
start_offset: int = 0 # 起始偏移(从该偏移开始分页,避免拉取历史全量)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class PageResult:
|
||||
start: int
|
||||
count: int
|
||||
items: list[Any]
|
||||
|
||||
|
||||
class Paginator:
|
||||
"""用至多 concurrency 个连接并行拉取分页数据。"""
|
||||
|
||||
def __init__(self, pool: DynamicPool, *, concurrency: int = 3,
|
||||
retries: int = 2, strict: bool = True) -> None:
|
||||
self.pool = pool
|
||||
self.concurrency = max(1, concurrency)
|
||||
self.retries = retries
|
||||
self.strict = strict
|
||||
|
||||
async def fetch_all(
|
||||
self,
|
||||
cmd_factory: Callable[[int, int], Any], # (start, count) -> BaseCommand
|
||||
spec: PageSpec,
|
||||
*,
|
||||
concurrency: int | None = None,
|
||||
) -> list[Any]:
|
||||
"""按序拼接后的完整结果。"""
|
||||
jobs = self._plan_starts(spec)
|
||||
if not jobs:
|
||||
return []
|
||||
eff_conc = min(
|
||||
self.concurrency if concurrency is None else concurrency,
|
||||
len(jobs),
|
||||
)
|
||||
conns = await self.pool.acquire_many(eff_conc)
|
||||
if not conns:
|
||||
raise PoolTimeoutError("连接池获取超时(无可用连接)")
|
||||
try:
|
||||
stop = asyncio.Event()
|
||||
out: list[PageResult] = []
|
||||
out_lock = asyncio.Lock()
|
||||
k = len(conns)
|
||||
workers = [
|
||||
asyncio.create_task(
|
||||
self._worker(wid, conns[wid % k], jobs[wid::k], cmd_factory,
|
||||
stop, out, out_lock, spec)
|
||||
)
|
||||
for wid in range(k)
|
||||
]
|
||||
await asyncio.gather(*workers)
|
||||
finally:
|
||||
for pc in conns:
|
||||
await self.pool.release(pc)
|
||||
return self._assemble(out, spec.ordered)
|
||||
|
||||
def _plan_starts(self, spec: PageSpec) -> list[tuple[int, int]]:
|
||||
"""切页:返回 [(start, count), ...]。"""
|
||||
jobs: list[tuple[int, int]] = []
|
||||
if spec.total is not None:
|
||||
pos = spec.start_offset
|
||||
pages = 0
|
||||
while pos < spec.total:
|
||||
if spec.max_pages is not None and pages >= spec.max_pages:
|
||||
break
|
||||
cnt = min(spec.page_size, spec.total - pos)
|
||||
# 已知 total 时直接使用精确 cnt,不强制 min_page(避免过量请求)
|
||||
jobs.append((pos, cnt))
|
||||
pos += cnt
|
||||
pages += 1
|
||||
else:
|
||||
for page in range(spec.max_pages if spec.max_pages is not None else 1024):
|
||||
jobs.append((spec.start_offset + page * spec.page_size, spec.page_size))
|
||||
return jobs
|
||||
|
||||
async def _worker(
|
||||
self, wid: int, conn: PooledConnection, jobs: list[tuple[int, int]],
|
||||
cmd_factory: Callable[[int, int], Any], stop: asyncio.Event,
|
||||
out: list[PageResult], out_lock: asyncio.Lock, spec: PageSpec,
|
||||
) -> None:
|
||||
current = conn
|
||||
for start, count in jobs:
|
||||
if stop.is_set():
|
||||
return
|
||||
items, current = await self._fetch_one(
|
||||
current, cmd_factory(start, count), count)
|
||||
if items is None:
|
||||
continue # 该页失败(strict=False 时留空)
|
||||
async with out_lock:
|
||||
out.append(PageResult(start=start, count=count, items=items))
|
||||
if spec.stop_when_short and len(items) < count:
|
||||
stop.set()
|
||||
return
|
||||
|
||||
async def _fetch_one(self, conn: PooledConnection, cmd, page_size: int):
|
||||
"""单页拉取,失败换连接重试。
|
||||
|
||||
返回 (items, current_conn) — current_conn 始终指向当前可用连接,
|
||||
worker 据此更新自己的连接引用,避免跨页使用已关闭连接。
|
||||
"""
|
||||
current = conn
|
||||
for attempt in range(self.retries + 1):
|
||||
try:
|
||||
result = await current.execute(cmd)
|
||||
return (list(result) if result is not None else []), current
|
||||
except Exception as e:
|
||||
if attempt >= self.retries:
|
||||
if self.strict:
|
||||
raise
|
||||
log.warning("分页拉取失败(strict=False留空): %s", e)
|
||||
return None, current
|
||||
# 换连接重试
|
||||
await self.pool.release(current, broken=True)
|
||||
try:
|
||||
current = await self.pool.acquire(timeout=3.0)
|
||||
except PoolTimeoutError:
|
||||
log.warning("分页重试获取连接失败,跳过该页: %s", e)
|
||||
return None, current
|
||||
return None, current
|
||||
|
||||
def _assemble(self, results: list[PageResult], ordered: bool) -> list[Any]:
|
||||
if ordered:
|
||||
results.sort(key=lambda r: r.start)
|
||||
out: list[Any] = []
|
||||
for r in results:
|
||||
out.extend(r.items)
|
||||
return out
|
||||
@@ -0,0 +1,148 @@
|
||||
"""服务器清单导入+自动探测工具
|
||||
|
||||
读 servers.json → 并发探测每台可用性与延时(标准/MAC用7709、EX用7727)→ 写回状态。
|
||||
|
||||
用法:
|
||||
python -m pytdxdata.server_probe # 全量探测
|
||||
python -m pytdxdata.server_probe --group ex # 只测EX
|
||||
python -m pytdxdata.server_probe --refresh # 重新探测(status=ok的)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from .pool.connection import TdxConnection
|
||||
from .pool.ex_connection import ExTdxConnection
|
||||
from .protocol.commands.kline import GetSecurityBarsCmd
|
||||
from .protocol.commands.mac import MacKlineCmd
|
||||
from .protocol.commands.ex_proto import GetExInstrumentBarsCmd
|
||||
from .models import KlinePeriod, Adjust
|
||||
|
||||
SERVERS_FILE = Path(__file__).resolve().parent / "servers.json"
|
||||
|
||||
|
||||
def load_servers() -> dict:
|
||||
if not SERVERS_FILE.exists():
|
||||
raise FileNotFoundError(f"配置文件不存在: {SERVERS_FILE}")
|
||||
return json.loads(SERVERS_FILE.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def save_servers(cfg: dict) -> None:
|
||||
SERVERS_FILE.write_text(json.dumps(cfg, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
|
||||
|
||||
async def probe_standard(ip: str, timeout: float = 5.0) -> tuple[bool, float]:
|
||||
conn = TdxConnection(ip, 7709, timeout)
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
await conn.connect()
|
||||
bars = await conn.execute(GetSecurityBarsCmd(1, "600000", 5, 0, 100))
|
||||
return (bool(bars), time.monotonic() - t0)
|
||||
except Exception:
|
||||
return (False, time.monotonic() - t0)
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
|
||||
async def probe_mac(ip: str, timeout: float = 5.0) -> tuple[bool, float]:
|
||||
conn = TdxConnection(ip, 7709, timeout)
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
await conn.connect()
|
||||
bars = await conn.execute(MacKlineCmd(1, "600000", KlinePeriod.DAY, Adjust.NONE, 0, 100))
|
||||
return (bool(bars), time.monotonic() - t0)
|
||||
except Exception:
|
||||
return (False, time.monotonic() - t0)
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
|
||||
async def probe_ex(ip: str, handshake: str, timeout: float = 5.0) -> tuple[bool, float]:
|
||||
conn = ExTdxConnection(ip, 7727, timeout, handshake)
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
await conn.connect()
|
||||
bars = await conn.execute(GetExInstrumentBarsCmd(31, "00700", 0, 0, 5))
|
||||
return (bool(bars), time.monotonic() - t0)
|
||||
except Exception:
|
||||
return (False, time.monotonic() - t0)
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
|
||||
async def probe_one(server: dict, timeout: float) -> dict:
|
||||
ip = server["ip"]
|
||||
protocols = server.get("protocols", [])
|
||||
best_lat = None
|
||||
ok = False
|
||||
# 逐协议探测(一台多协议: standard/mac同端口可复用连接——简化逐协议测)
|
||||
for proto in protocols:
|
||||
if proto == "ex":
|
||||
r, lat = await probe_ex(ip, server.get("handshake", "setup"), timeout)
|
||||
elif proto == "mac":
|
||||
r, lat = await probe_mac(ip, timeout)
|
||||
else:
|
||||
r, lat = await probe_standard(ip, timeout)
|
||||
if r:
|
||||
ok = True
|
||||
best_lat = lat if best_lat is None else min(best_lat, lat)
|
||||
server["status"] = "ok" if ok else "fail"
|
||||
server["latency"] = round(best_lat, 3) if best_lat is not None else None
|
||||
server["checked_at"] = time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
return server
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
ap = argparse.ArgumentParser(description="服务器清单导入+自动探测")
|
||||
ap.add_argument("--group", choices=["standard", "mac", "ex"], default=None)
|
||||
ap.add_argument("--timeout", type=float, default=5.0)
|
||||
ap.add_argument("--refresh", action="store_true", help="只重测status=ok的")
|
||||
args = ap.parse_args()
|
||||
|
||||
cfg = load_servers()
|
||||
servers = cfg["servers"]
|
||||
if args.group:
|
||||
servers = [s for s in servers if args.group in s.get("protocols", [])]
|
||||
if args.refresh:
|
||||
servers = [s for s in servers if s.get("status") == "ok"]
|
||||
if not servers:
|
||||
print("没有待探测的服务器")
|
||||
return
|
||||
|
||||
print("=" * 90)
|
||||
print("探测 %d 台服务器 (标准/MAC:7709, EX:7727)..." % len(servers))
|
||||
print("=" * 90)
|
||||
t0 = time.time()
|
||||
results = await asyncio.gather(*[probe_one(s, args.timeout) for s in servers])
|
||||
elapsed = time.time() - t0
|
||||
|
||||
# 写回
|
||||
updated = {s["ip"]: s for s in results}
|
||||
for s in cfg["servers"]:
|
||||
if s["ip"] in updated:
|
||||
s.update(updated[s["ip"]])
|
||||
save_servers(cfg)
|
||||
|
||||
ok_list = [s for s in results if s["status"] == "ok"]
|
||||
ok_list.sort(key=lambda s: s["latency"])
|
||||
fail_list = [s for s in results if s["status"] == "fail"]
|
||||
print("\n可用: %d/%d (%.1fs)" % (len(ok_list), len(results), elapsed))
|
||||
print("-" * 90)
|
||||
for s in ok_list:
|
||||
print(" [OK] %-16s %-14s %-28s %.3fs %s" % (
|
||||
s["ip"], "/".join(s.get("protocols", [])), s["name"],
|
||||
s["latency"], s.get("handshake") or ""))
|
||||
if fail_list:
|
||||
print("\n不可用: %d台" % len(fail_list))
|
||||
for s in fail_list:
|
||||
print(" [X] %-16s %s" % (s["ip"], "/".join(s.get("protocols", []))))
|
||||
print("\n已写回 %s" % SERVERS_FILE)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,488 @@
|
||||
{
|
||||
"version": "2026-08-12",
|
||||
"note": "当日全量测速入库(2026-09-08): 入池=9台 (排名前10或延迟<200ms)",
|
||||
"servers": [
|
||||
{
|
||||
"ip": "139.9.52.158",
|
||||
"name": "国泰君安深圳",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": null,
|
||||
"checked_at": "2026-08-21 11:23:14",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-21"
|
||||
},
|
||||
{
|
||||
"ip": "103.251.85.94",
|
||||
"name": "国泰君安上海",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.481,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "58.67.221.146",
|
||||
"name": "中信短线宝21",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.254,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "59.36.5.11",
|
||||
"name": "安信",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.254,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "103.221.142.82",
|
||||
"name": "国泰君安江苏",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac",
|
||||
"ex"
|
||||
],
|
||||
"handshake": "setup",
|
||||
"status": "slow",
|
||||
"latency": 0.487,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "218.106.92.182",
|
||||
"name": "华林",
|
||||
"protocols": [
|
||||
"standard"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.371,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "60.12.136.251",
|
||||
"name": "中信短线宝22",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.37,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "218.106.92.183",
|
||||
"name": "华林",
|
||||
"protocols": [
|
||||
"standard"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.377,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "60.12.136.250",
|
||||
"name": "中信短线宝26",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.373,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "115.238.90.170",
|
||||
"name": "中信短线宝23",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.482,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "220.178.55.86",
|
||||
"name": "华林",
|
||||
"protocols": [
|
||||
"standard"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.479,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "220.178.55.71",
|
||||
"name": "华林",
|
||||
"protocols": [
|
||||
"standard"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.47,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "180.153.18.170",
|
||||
"name": "中信短线宝1",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.438,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "115.238.90.165",
|
||||
"name": "杭州J4",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.476,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "60.191.117.167",
|
||||
"name": "杭州J1",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.362,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "218.75.126.9",
|
||||
"name": "杭州J3",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.481,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "115.238.56.198",
|
||||
"name": "杭州J2",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.362,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.16",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 1.575,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "159.75.55.232",
|
||||
"name": "通达信深圳9",
|
||||
"protocols": [
|
||||
"standard"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.244,
|
||||
"checked_at": "2026-09-08 10:05:12",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": true,
|
||||
"checked_date": "2026-09-08"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.17",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 1.573,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.14",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.508,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.15",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.636,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.13",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.608,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.27",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 1.138,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.18",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.969,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.20",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard",
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "slow",
|
||||
"latency": 0.526,
|
||||
"checked_at": "2026-08-19 15:37:35",
|
||||
"source": "probe-2026-08-12",
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19"
|
||||
},
|
||||
{
|
||||
"ip": "117.34.114.30",
|
||||
"name": "国泰君安",
|
||||
"protocols": [
|
||||
"standard"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "fail",
|
||||
"latency": null,
|
||||
"checked_at": "2026-08-12 18:36:21",
|
||||
"source": "probe-2026-08-12"
|
||||
},
|
||||
{
|
||||
"ip": "121.37.207.165",
|
||||
"name": "MAC专用",
|
||||
"protocols": [
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.249,
|
||||
"checked_at": "2026-08-12 18:36:17",
|
||||
"source": "probe-2026-08-12"
|
||||
},
|
||||
{
|
||||
"ip": "121.36.248.138",
|
||||
"name": "MAC专用",
|
||||
"protocols": [
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.436,
|
||||
"checked_at": "2026-08-12 18:36:17",
|
||||
"source": "probe-2026-08-12"
|
||||
},
|
||||
{
|
||||
"ip": "123.60.47.136",
|
||||
"name": "MAC专用",
|
||||
"protocols": [
|
||||
"mac"
|
||||
],
|
||||
"handshake": null,
|
||||
"status": "ok",
|
||||
"latency": 0.476,
|
||||
"checked_at": "2026-08-12 18:36:17",
|
||||
"source": "probe-2026-08-12"
|
||||
},
|
||||
{
|
||||
"ip": "116.205.135.205",
|
||||
"name": "MAC EX专用",
|
||||
"protocols": [
|
||||
"ex"
|
||||
],
|
||||
"handshake": "login",
|
||||
"status": "ok",
|
||||
"latency": 0.192,
|
||||
"checked_at": "2026-08-12 18:36:17",
|
||||
"source": "probe-2026-08-12"
|
||||
},
|
||||
{
|
||||
"ip": "121.37.232.167",
|
||||
"name": "MAC EX专用",
|
||||
"protocols": [
|
||||
"ex"
|
||||
],
|
||||
"handshake": "login",
|
||||
"status": "ok",
|
||||
"latency": 0.194,
|
||||
"checked_at": "2026-08-12 18:36:17",
|
||||
"source": "probe-2026-08-12"
|
||||
},
|
||||
{
|
||||
"ip": "116.205.143.214",
|
||||
"name": "通达信扩展市场广州1",
|
||||
"protocols": [
|
||||
"ex"
|
||||
],
|
||||
"handshake": "setup",
|
||||
"status": "ok",
|
||||
"latency": 0.192,
|
||||
"checked_at": "2026-08-12 18:36:17",
|
||||
"source": "probe-2026-08-12"
|
||||
},
|
||||
{
|
||||
"ip": "s1",
|
||||
"name": "s1",
|
||||
"protocols": [
|
||||
"standard"
|
||||
],
|
||||
"status": "unknown",
|
||||
"latency": null,
|
||||
"fast_pool": false,
|
||||
"checked_date": "2026-08-19",
|
||||
"checked_at": "2026-08-19 15:34:45"
|
||||
}
|
||||
],
|
||||
"checked_date": "2026-09-08"
|
||||
}
|
||||
Reference in new issue
Block a user