Files
Grid_Trading/src/grid_trading/market/tencent.py

148 lines
5.0 KiB
Python
Raw Normal View History

2026-07-08 21:59:17 +08:00
from __future__ import annotations
import re
from decimal import Decimal, InvalidOperation
from typing import Callable, Iterable
2026-07-08 22:06:39 +08:00
from urllib.error import HTTPError, URLError
2026-07-08 21:59:17 +08:00
from urllib.request import urlopen
from grid_trading.domain.models import Instrument, QuoteSnapshot
TENCENT_ENDPOINT = "http://qt.gtimg.cn/q="
_RESPONSE_RE = re.compile(r"v_([a-z]{2}\d{6})=\"([^\"]*)\";?")
class QuoteFetchError(RuntimeError):
pass
def infer_tencent_symbol(code: str) -> str:
normalized = code.strip().lower()
if re.fullmatch(r"(sh|sz|bj)\d{6}", normalized):
return normalized
if not re.fullmatch(r"\d{6}", normalized):
raise QuoteFetchError(f"Unsupported Tencent quote code: {code}")
if normalized.startswith(("6", "5", "9")):
return f"sh{normalized}"
if normalized.startswith(("0", "1", "2", "3")):
return f"sz{normalized}"
if normalized.startswith(("4", "8")):
return f"bj{normalized}"
raise QuoteFetchError(f"Cannot infer Tencent market prefix for code: {code}")
def parse_tencent_response(raw: bytes) -> list[QuoteSnapshot]:
text = _decode_response(raw)
quotes: list[QuoteSnapshot] = []
for match in _RESPONSE_RE.finditer(text):
symbol = match.group(1)
fields = match.group(2).split("~")
quotes.append(_parse_quote_fields(symbol, fields))
if not quotes:
raise QuoteFetchError("Tencent quote response did not contain quote records")
return quotes
class TencentQuoteProvider:
def __init__(
self,
*,
endpoint: str = TENCENT_ENDPOINT,
timeout: float = 5.0,
retries: int = 1,
2026-07-08 21:59:17 +08:00
fetcher: Callable[[str, float], bytes] | None = None,
):
self.endpoint = endpoint
self.timeout = timeout
self.retries = retries
2026-07-08 21:59:17 +08:00
self._fetcher = fetcher or _default_fetcher
@property
def uses_default_fetcher(self) -> bool:
"""Whether the provider can be driven by Qt's asynchronous network API."""
return self._fetcher is _default_fetcher
def build_request_url(self, instruments: Iterable[Instrument]) -> str | None:
2026-07-08 21:59:17 +08:00
symbols = [infer_tencent_symbol(instrument.code) for instrument in instruments]
if not symbols:
return None
return f"{self.endpoint}{','.join(symbols)}"
@staticmethod
def parse_response(raw: bytes) -> dict[str, QuoteSnapshot]:
return {quote.code: quote for quote in parse_tencent_response(raw)}
def fetch_quotes(self, instruments: Iterable[Instrument]) -> dict[str, QuoteSnapshot]:
url = self.build_request_url(instruments)
if url is None:
2026-07-08 21:59:17 +08:00
return {}
raw = self._fetch_with_retry(url)
return self.parse_response(raw)
2026-07-08 21:59:17 +08:00
def _fetch_with_retry(self, url: str) -> bytes:
last_error: Exception | None = None
for _ in range(self.retries + 1):
try:
return self._fetcher(url, self.timeout)
except (OSError, HTTPError, URLError) as exc:
last_error = exc
raise QuoteFetchError(f"Tencent quote request failed: {last_error}") from last_error
2026-07-08 21:59:17 +08:00
def _default_fetcher(url: str, timeout: float) -> bytes:
with urlopen(url, timeout=timeout) as response:
return response.read()
def _decode_response(raw: bytes) -> str:
for encoding in ("gbk", "gb18030", "utf-8"):
try:
return raw.decode(encoding)
except UnicodeDecodeError:
continue
raise QuoteFetchError("Tencent quote response encoding is unsupported")
def _parse_quote_fields(symbol: str, fields: list[str]) -> QuoteSnapshot:
if len(fields) > 3 and not fields[3].strip():
raise QuoteFetchError(f"Tencent quote {symbol} price is missing")
if len(fields) < 35:
raise QuoteFetchError(f"Tencent quote record for {symbol} is incomplete")
price = _required_decimal(fields[3], f"{symbol} price")
if price <= 0:
raise QuoteFetchError(f"Tencent quote record for {symbol} price is empty or zero")
code = fields[2].strip() or symbol[2:]
name = fields[1].strip() or code
return QuoteSnapshot(
symbol=symbol,
code=code,
name=name,
price=price,
source="tencent",
quote_time=fields[30].strip() or None,
previous_close=_optional_decimal(fields[4]),
open_price=_optional_decimal(fields[5]),
high=_optional_decimal(fields[33]),
low=_optional_decimal(fields[34]),
change=_optional_decimal(fields[31]),
change_percent=_optional_decimal(fields[32]),
)
def _required_decimal(raw: str, label: str) -> Decimal:
value = _optional_decimal(raw)
if value is None:
raise QuoteFetchError(f"Tencent quote {label} is missing")
return value
def _optional_decimal(raw: str) -> Decimal | None:
text = raw.strip()
if not text:
return None
try:
return Decimal(text)
except InvalidOperation as exc:
raise QuoteFetchError(f"Tencent quote value is not numeric: {raw}") from exc