#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 比特币 K 线行情客户端 —— 免费、无需密钥的公开接口,按顺序尝试多个源,首个成功即返回。 源(均为公开 REST,无鉴权): 1. OKX GET /api/v5/market/candles?instId=BTC-USDT&bar=<1H|1D> —— 主源,本地直连可达、数据最新 2. Coinbase GET /products/BTC-USD/candles?granularity=<3600|86400> —— 备源(USD 计价,与 USDT 数值几乎一致) 3. Huobi GET /market/history/kline?period=<60min|1day>&symbol=btcusdt —— 备源 (Binance 在本地区返回 restricted location,不列入。) 周期用统一代号 bar:"1H"(时 K)/ "1D"(日 K),由 BARS 映射到各源的写法。 统一输出:按时间**升序**的 list[dict],每根 {"ts": epoch 秒, "o", "h", "l", "c"}(float),最后一根为当前未收盘的那根。 任何源失败抛异常并尝试下一个;全部失败抛 RuntimeError,由 data 层决定沿用缓存或占位。 """ import json import urllib.request TIMEOUT = 8 UA = "eink-push/1.0 (+https://github.com/)" # 周期代号 → 各源参数:(OKX bar, Coinbase granularity 秒, Huobi period) BARS = {"1H": ("1H", 3600, "60min"), "1D": ("1D", 86400, "1day")} def _get_json(url): req = urllib.request.Request(url, headers={"User-Agent": UA, "Accept": "application/json"}) with urllib.request.urlopen(req, timeout=TIMEOUT) as resp: return json.loads(resp.read().decode("utf-8")) def _okx(bar, limit): j = _get_json(f"https://www.okx.com/api/v5/market/candles?instId=BTC-USDT&bar={BARS[bar][0]}&limit={limit}") if str(j.get("code")) != "0": raise RuntimeError(f"okx code={j.get('code')} msg={j.get('msg')}") rows = j["data"] # 最新在前:[ts_ms, o, h, l, c, vol, ...] out = [{"ts": int(r[0]) // 1000, "o": float(r[1]), "h": float(r[2]), "l": float(r[3]), "c": float(r[4])} for r in rows] return sorted(out, key=lambda x: x["ts"]) def _coinbase(bar, limit): rows = _get_json(f"https://api.exchange.coinbase.com/products/BTC-USD/candles?granularity={BARS[bar][1]}") # 最新在前:[time, low, high, open, close, volume] out = [{"ts": int(r[0]), "o": float(r[3]), "h": float(r[2]), "l": float(r[1]), "c": float(r[4])} for r in rows] return sorted(out, key=lambda x: x["ts"])[-limit:] def _huobi(bar, limit): j = _get_json(f"https://api.huobi.pro/market/history/kline?period={BARS[bar][2]}&size={limit}&symbol=btcusdt") if j.get("status") != "ok": raise RuntimeError(f"huobi status={j.get('status')} err={j.get('err-msg')}") out = [{"ts": int(r["id"]), "o": float(r["open"]), "h": float(r["high"]), "l": float(r["low"]), "c": float(r["close"])} for r in j["data"]] return sorted(out, key=lambda x: x["ts"]) SOURCES = (("okx", _okx), ("coinbase", _coinbase), ("huobi", _huobi)) def get_candles(bar="1H", limit=36): """返回 (源名, 升序 K 线列表)。bar 为 "1H"/"1D"。逐源尝试,全部失败抛 RuntimeError。""" if bar not in BARS: raise ValueError(f"不支持的周期 {bar!r},可选:{list(BARS)}") errors = [] for name, fn in SOURCES: try: rows = fn(bar, limit) if len(rows) >= 2: return name, rows[-limit:] errors.append(f"{name}: 数据不足({len(rows)})") except Exception as e: # noqa: BLE001 —— 逐源兜底,任何异常都换下一个源 errors.append(f"{name}: {e}") raise RuntimeError(f"BTC {bar} K 线全部源失败:" + ";".join(errors)) if __name__ == "__main__": src, rows = get_candles("1H", 5) print(src) for r in rows: print(r)