#!/usr/bin/env python3
"""价格哨兵 - 纯脚本不烧token，只在关键突破时输出警报(否则静默)"""
import requests, json, time, os

BASE = "https://www.okx.com"
INST = "BTC-USDT-SWAP"
# 关键位(每轮由格蕾丝更新)
HI = float(os.environ.get("WATCH_HI", "63269"))
LO = float(os.environ.get("WATCH_LO", "62953"))
THRESH = float(os.environ.get("WATCH_THRESH", "0.003"))  # 15m波动>0.3%算异常

try:
    r = requests.get(f"{BASE}/api/v5/market/ticker?instId={INST}", timeout=10)
    d = r.json()["data"][0]
    price = float(d["last"])
    ts = time.strftime("%m-%d %H:%M")
    msgs = []
    if price > HI:
        msgs.append(f"🔺 破阻力! 价{price:.0f} > {HI} (近2日高)")
    elif price < LO:
        msgs.append(f"🔻 破支撑! 价{price:.0f} < {LO} (1H摆动低)")
    if msgs:
        print(f"[{ts}] BTC {price:.0f} 警报: " + " | ".join(msgs))
except Exception as e:
    # 脚本自身错误不打扰，记到文件即可
    with open("/root/quant_pipeline/price_watch.err", "a") as f:
        f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} {e}\n")
