# -*- coding: utf-8 -*-
"""一次性对照所有品种的历史价 vs 实时价，看精度是否一致。"""
from datasource import TdxSource
from config import load_symbols

rows = load_symbols()
# 按 code 去重
seen = {}
for r in rows:
    seen.setdefault(r["code"], r)

src = TdxSource()
if not src.connect():
    print("连不上行情服务器")
    raise SystemExit(1)

print("=" * 66)
print("%-8s %-16s %10s %10s %8s" % ("代码", "名称", "历史收盘", "实时报价", "倍率"))
print("=" * 66)
for code, r in seen.items():
    m = r["market"]
    try:
        bars = src.get_history_5min(m, code, 3)
        hist = bars[-1].close if bars else 0
        q = src.get_quotes([(m, code)])
        live = q.get(code, {}).get("price", 0)
    except Exception as e:
        print("%-8s %-16s  取数失败: %s" % (code, r["name"], e))
        continue
    ratio = (live / hist) if hist else 0
    # 判断倍率
    tag = ""
    for s in (100, 10):
        if hist and abs(ratio - s) / s < 0.15:
            tag = "放大%d倍" % s
            break
    if not tag and hist and abs(ratio - 1) < 0.15:
        tag = "正常"
    print("%-8s %-16s %10.4f %10.4f %8s  %s"
          % (code, r["name"], hist, live, "%.1f" % ratio if hist else "-", tag))
src.close()
print("=" * 66)
print("『正常』的品种现价本来就对；『放大N倍』的会被自动校准。")
