Files
Trade/crypto_watch.py
T
2026-08-26 12:25:21 +08:00

84 lines
2.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
币圈自动盯盘脚本 (复用股票框架)
- 数据源: 币安公开接口 data-api.binance.vision (免key)
- 每5分钟检查各币种现价,命中预警价则弹 macOS 通知
- 7x24 运行(币圈无休)
"""
import subprocess
import datetime
import sys
# ===== 监控配置: 在下面加币种和预警价 =====
# symbol 用币安格式 (BTCUSDT), alert 为你要监控的价位与提示
WATCH = [
{
"symbol": "BTCUSDT", "name": "BTC",
"alerts": [
{"price": 80000, "tag": "突破", "tip": "站上8万,关注趋势延续"},
{"price": 75000, "tag": "回调", "tip": "跌破7.5万,注意支撑"},
],
},
{
"symbol": "ETHUSDT", "name": "ETH",
"alerts": [
{"price": 4000, "tag": "突破", "tip": "站上4000,强势"},
{"price": 3500, "tag": "回调", "tip": "跌破3500,减仓观察"},
],
},
# 复制上面结构可继续添加, 例如:
# {"symbol": "SOLUSDT", "name": "SOL",
# "alerts": [{"price": 200, "tag": "突破", "tip": "SOL站上200"}]},
]
ALERTED = set()
def price_of(symbol):
try:
url = f"https://data-api.binance.vision/api/v3/ticker/24hr?symbol={symbol}"
out = subprocess.run(
["curl", "-s", "--max-time", "8", url],
capture_output=True, text=True, timeout=12
).stdout
if '"lastPrice"' not in out:
return None
# 简单解析 lastPrice
import json
d = json.loads(out)
return float(d["lastPrice"])
except Exception:
return None
def notify(title, msg):
subprocess.run(["osascript", "-e",
f'display notification "{msg}" with title "{title}" sound name "Glass"'])
def main():
today = datetime.date.today().isoformat()
# 跨日重置
if ALERTED and not any(k.endswith(today) for k in ALERTED):
ALERTED.clear()
for c in WATCH:
p = price_of(c["symbol"])
if p is None:
continue
for a in c["alerts"]:
key = f"{c['symbol']}_{a['price']}_{today}"
hit = (p >= a["price"]) if a.get("above", True) else (p <= a["price"])
# 默认 above=True: 价格>=预警价触发; 想做"跌破"提醒就设 above=False
if "above" in a and a["above"] is False:
hit = p <= a["price"]
if hit and key not in ALERTED:
ALERTED.add(key)
notify(f"[币圈 {a['tag']}] {c['name']}",
f"{c['name']} 现价 {p:.2f} USDT | {a['tip']}")
if __name__ == "__main__":
main()